feat: v0.2.4, list supports file-meta.json

This commit is contained in:
2024-08-18 11:31:54 +08:00
parent b75e350db1
commit affcb233bd
4 changed files with 66 additions and 31 deletions

View File

@@ -1,4 +1,17 @@
const FILE_META: &'static str = "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/file-meta.txt";
use rust_util::util_os::get_user_home;
use serde::Deserialize;
use std::collections::BTreeMap;
use std::fs;
use std::path::PathBuf;
const FILE_META: &'static str = "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/script-meta.json";
#[derive(Deserialize)]
struct ScriptMeta {
script_name: String,
script_length: u64,
script_sha256: String,
}
pub fn list_scripts() {
debugging!("Loading URL: {}", FILE_META);
@@ -14,18 +27,35 @@ pub fn list_scripts() {
});
debugging!("Response text: {}", &text);
let mut scripts: Vec<&str> = text.trim().split("\n").collect();
scripts.sort();
let script_meta_map: BTreeMap<String, ScriptMeta> = serde_json::from_str(&text).expect("Parse script-meta.json failed.");
success!("Found {} script(s):\n{}",
scripts.len(),
scripts.iter().map(|script| {
let script = if script.ends_with("-rs") {
script.chars().take(script.len() - 3).collect::<String>() + ".rs"
} else {
script.to_string()
};
format!("- {}", script)
}).collect::<Vec<_>>().join("\n")
);
let mut messages = vec![];
messages.push(format!("Found {} script(s):", script_meta_map.len()));
for (_, script_meta) in &script_meta_map {
let script_name = &script_meta.script_name;
let real_script_name = if script_name.ends_with("-rs") {
script_name.chars().take(script_name.len() - 3).collect::<String>() + ".rs"
} else {
script_name.to_string()
};
let user_home = get_user_home().expect("Get user home failed!");
let full_script_path = PathBuf::from(&user_home).join("bin").join(&real_script_name);
let is_script_exists = full_script_path.is_file();
let script_sha256 = if is_script_exists {
let script_content = fs::read(&full_script_path).unwrap_or(vec![]);
sha256::digest(&script_content)
} else {
"".to_string()
};
let is_script_matches = script_sha256 == script_meta.script_sha256;
let script_size = rust_util::util_size::get_display_size(script_meta.script_length as i64);
let padding_length = 40 - real_script_name.len();
messages.push(format!("- {} {} {} {}",
iff!(is_script_exists, iff!(is_script_matches, "", "✔️ "), ""),
real_script_name,
".".repeat(iff!(padding_length < 1, 1, padding_length)),
script_size,
));
}
success!("{}", messages.join("\n"));
}

View File

@@ -1,9 +1,9 @@
use std::{env, fs, process};
use rust_util::util_cmd;
use rust_util::util_env::is_env_on;
use std::path::PathBuf;
use std::process::Command;
use std::time::SystemTime;
use rust_util::util_cmd;
use rust_util::util_env::is_env_on;
use std::{env, fs, process};
pub fn build_script_command(rust_script: PathBuf, script_file: &str, script_sha256: &str, cache_script_bin_name: &str) -> Command {