73 lines
2.5 KiB
Rust
73 lines
2.5 KiB
Rust
use crate::install::install_scripts;
|
||
use crate::util;
|
||
use std::fs;
|
||
use std::path::PathBuf;
|
||
|
||
pub fn list_scripts(
|
||
script_repo: &Option<String>,
|
||
filter: Option<&String>,
|
||
update_listed_scripts: bool,
|
||
) {
|
||
let script_meta_map = util::fetch_script_meta_or_die(script_repo);
|
||
|
||
let mut matched_need_update_scripts = vec![];
|
||
let mut messages = vec![];
|
||
for script_meta in script_meta_map.values() {
|
||
let script_name = &script_meta.script_name;
|
||
if let Some(filter) = filter {
|
||
if !script_name.contains(filter) {
|
||
// NOT CONTAINS `filter`, SKIP
|
||
continue;
|
||
}
|
||
}
|
||
|
||
let user_home = util::get_user_home_or_die();
|
||
let full_script_path = PathBuf::from(&user_home).join("bin").join(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 - script_name.len();
|
||
messages.push(format!(
|
||
"- {} {} {} {}",
|
||
iff!(is_script_exists, iff!(is_script_matches, "✅", "✔️ "), "➖"),
|
||
script_name,
|
||
".".repeat(iff!(padding_length < 1, 1, padding_length)),
|
||
script_size,
|
||
));
|
||
if is_script_exists && !is_script_matches {
|
||
matched_need_update_scripts.push(script_meta.script_name.clone());
|
||
}
|
||
}
|
||
if filter.is_some() {
|
||
messages.insert(
|
||
0,
|
||
format!(
|
||
"Total {} script(s), found {} script(s):",
|
||
script_meta_map.len(),
|
||
messages.len()
|
||
),
|
||
);
|
||
} else {
|
||
messages.insert(0, format!("Found {} script(s):", script_meta_map.len()));
|
||
}
|
||
success!("{}", messages.join("\n"));
|
||
|
||
if update_listed_scripts {
|
||
if matched_need_update_scripts.len() > 0 {
|
||
information!(
|
||
"Update listed scripts ON:\n- {}\n",
|
||
matched_need_update_scripts.join("\n- ")
|
||
);
|
||
install_scripts(script_repo, &matched_need_update_scripts);
|
||
} else {
|
||
information!("No update listed scripts");
|
||
}
|
||
}
|
||
}
|