feat: v0.2
This commit is contained in:
@@ -1,3 +1,9 @@
|
|||||||
|
use std::fs;
|
||||||
|
use std::os::unix::fs::PermissionsExt;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use rust_util::util_os::get_user_home;
|
||||||
|
|
||||||
const SCRIPT_PATTERN: &'static str = "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/$NAME/src/main.rs";
|
const SCRIPT_PATTERN: &'static str = "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/$NAME/src/main.rs";
|
||||||
|
|
||||||
pub fn install_script(args: Vec<&String>) {
|
pub fn install_script(args: Vec<&String>) {
|
||||||
@@ -13,17 +19,23 @@ pub fn install_script(args: Vec<&String>) {
|
|||||||
failure_and_exit!("Invalid script name: {}", script_name);
|
failure_and_exit!("Invalid script name: {}", script_name);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let script_file_name = normalized_script_name
|
||||||
|
.chars().take(normalized_script_name.len() - 3).collect::<String>() + ".rs";
|
||||||
|
let user_home = get_user_home().expect("Get user home failed!");
|
||||||
|
let output_file_name = PathBuf::from(&user_home).join("bin").join(&script_file_name);
|
||||||
|
if output_file_name.exists() {
|
||||||
|
warning!("Script file: {} exists", output_file_name.to_string_lossy());
|
||||||
|
}
|
||||||
|
|
||||||
let script_url = SCRIPT_PATTERN.replace("$NAME", &normalized_script_name);
|
let script_url = SCRIPT_PATTERN.replace("$NAME", &normalized_script_name);
|
||||||
information!("Script URL: {}", &script_url);
|
information!("Script URL: {}", &script_url);
|
||||||
|
|
||||||
let get_script_response_result = reqwest::blocking::get(script_url);
|
let get_script_response_result = reqwest::blocking::get(&script_url);
|
||||||
debugging!("Get script response: {:#?}", &get_script_response_result);
|
debugging!("Get script response: {:#?}", &get_script_response_result);
|
||||||
|
|
||||||
let get_script_response = match get_script_response_result {
|
let get_script_response = match get_script_response_result {
|
||||||
Err(e) => {
|
Err(e) => failure_and_exit!("Get script failed: {}", e),
|
||||||
failure_and_exit!("Get script failed: {}", e);
|
Ok(response) => response
|
||||||
}
|
|
||||||
Ok(response) => { response }
|
|
||||||
};
|
};
|
||||||
let get_script_response_status = get_script_response.status().as_u16();
|
let get_script_response_status = get_script_response.status().as_u16();
|
||||||
if get_script_response_status == 404 {
|
if get_script_response_status == 404 {
|
||||||
@@ -31,5 +43,24 @@ pub fn install_script(args: Vec<&String>) {
|
|||||||
} else if get_script_response_status != 200 {
|
} else if get_script_response_status != 200 {
|
||||||
failure_and_exit!("Get script failed: {}", get_script_response_status);
|
failure_and_exit!("Get script failed: {}", get_script_response_status);
|
||||||
}
|
}
|
||||||
success!("{}", get_script_response.text().unwrap());
|
|
||||||
|
let text = match get_script_response.text() {
|
||||||
|
Err(e) => failure_and_exit!("Get script: {} failed: {}", &script_url, e),
|
||||||
|
Ok(text) => text
|
||||||
|
};
|
||||||
|
if let Ok(script_content) = fs::read_to_string(&output_file_name) {
|
||||||
|
if text == script_content {
|
||||||
|
success!("File is extract same, skip write file: {}", output_file_name.to_string_lossy());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let Err(e) = fs::write(&output_file_name, text) {
|
||||||
|
failure_and_exit!("Write script: {} failed: {}", output_file_name.to_string_lossy(), e)
|
||||||
|
}
|
||||||
|
|
||||||
|
success!("Write file: {} success", output_file_name.to_string_lossy());
|
||||||
|
match fs::set_permissions(&output_file_name, PermissionsExt::from_mode(0o755)) {
|
||||||
|
Err(e) => failure_and_exit!("Chmod script: {} permission failed: {}", output_file_name.to_string_lossy(), e),
|
||||||
|
Ok(_) => success!("Chmod script: {} permission succeed", output_file_name.to_string_lossy())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user