refactor: refactor main.rs
This commit is contained in:
86
src/main.rs
86
src/main.rs
@@ -9,21 +9,9 @@ use rust_util::{util_cmd, util_msg};
|
|||||||
use rust_util::util_msg::MessageType;
|
use rust_util::util_msg::MessageType;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let home = env::var("HOME").unwrap_or_else(|_|
|
let home = get_user_home();
|
||||||
failure_and_exit!("$HOME not found!")
|
let rust_script = get_run_script_bin_name(&home);
|
||||||
);
|
|
||||||
let rust_script = PathBuf::from(format!("{}/.cargo/bin/rust-script", home));
|
|
||||||
if !rust_script.exists() {
|
|
||||||
warning!("rust-script not found, install it...");
|
|
||||||
let mut cargo_install_rust_script = Command::new("cargo");
|
|
||||||
cargo_install_rust_script.args(&["install", "--git", "https://git.hatter.ink/hatter/runrs", "rust-script"]);
|
|
||||||
debugging!("Run command: {:?}", cargo_install_rust_script);
|
|
||||||
match util_cmd::run_command_and_wait(&mut cargo_install_rust_script) {
|
|
||||||
Err(e) => failure_and_exit!("Install rust-script failed: {}", e),
|
|
||||||
Ok(exist_status) if !exist_status.success() => failure!("Install rust-script not success: {}", exist_status),
|
|
||||||
Ok(_) => (),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let args = env::args().skip(1).collect::<Vec<_>>();
|
let args = env::args().skip(1).collect::<Vec<_>>();
|
||||||
if args.is_empty() {
|
if args.is_empty() {
|
||||||
failure_and_exit!("runrs v{}, need arguments, e.g.\n\nrunrs <script.rs> [arguments]\n",
|
failure_and_exit!("runrs v{}, need arguments, e.g.\n\nrunrs <script.rs> [arguments]\n",
|
||||||
@@ -33,18 +21,7 @@ fn main() {
|
|||||||
let first_argument = args.get(0).unwrap_or_else(|| failure_and_exit!("Must assign file name"));
|
let first_argument = args.get(0).unwrap_or_else(|| failure_and_exit!("Must assign file name"));
|
||||||
|
|
||||||
if first_argument == "--help" {
|
if first_argument == "--help" {
|
||||||
println!(r##"{} v{} - {}
|
print_help();
|
||||||
|
|
||||||
Help:
|
|
||||||
runrs --help
|
|
||||||
|
|
||||||
Run Rust Script:
|
|
||||||
runrs <script.rs> [arguments]
|
|
||||||
"##,
|
|
||||||
env!("CARGO_PKG_NAME"),
|
|
||||||
env!("CARGO_PKG_VERSION"),
|
|
||||||
env!("CARGO_PKG_DESCRIPTION")
|
|
||||||
);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,14 +53,55 @@ runrs <script.rs> [arguments]
|
|||||||
match util_cmd::run_command_and_wait(&mut run_script_cmd) {
|
match util_cmd::run_command_and_wait(&mut run_script_cmd) {
|
||||||
Err(e) => failure_and_exit!("Run rust-script failed: {}", e),
|
Err(e) => failure_and_exit!("Run rust-script failed: {}", e),
|
||||||
Ok(exit_status) => {
|
Ok(exit_status) => {
|
||||||
if let Ok(Some(canonicalized_script_file)) = PathBuf::from(script_file)
|
write_script_file_to_src(script_file, cache_script_bin_name);
|
||||||
.canonicalize().map(|f| f.to_str().map(|f| f.to_string())) {
|
|
||||||
let cache_script_bin_name_src = format!("{}.src", cache_script_bin_name);
|
|
||||||
if let Ok(_) = fs::write(&cache_script_bin_name_src, &format!("{}\n", canonicalized_script_file)) {
|
|
||||||
debugging!("Write {} to {}", canonicalized_script_file, cache_script_bin_name_src);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
process::exit(exit_status.code().unwrap_or_else(|| 0))
|
process::exit(exit_status.code().unwrap_or_else(|| 0))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn write_script_file_to_src(script_file: &String, cache_script_bin_name: String) {
|
||||||
|
if let Ok(Some(canonicalized_script_file)) = PathBuf::from(script_file)
|
||||||
|
.canonicalize().map(|f| f.to_str().map(|f| f.to_string())) {
|
||||||
|
let cache_script_bin_name_src = format!("{}.src", cache_script_bin_name);
|
||||||
|
if let Ok(_) = fs::write(&cache_script_bin_name_src, &format!("{}\n", canonicalized_script_file)) {
|
||||||
|
debugging!("Write {} to {}", canonicalized_script_file, cache_script_bin_name_src);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn print_help() {
|
||||||
|
println!(r##"{} v{} - {}
|
||||||
|
|
||||||
|
Help:
|
||||||
|
runrs --help
|
||||||
|
|
||||||
|
Run Rust Script:
|
||||||
|
runrs <script.rs> [arguments]
|
||||||
|
"##,
|
||||||
|
env!("CARGO_PKG_NAME"),
|
||||||
|
env!("CARGO_PKG_VERSION"),
|
||||||
|
env!("CARGO_PKG_DESCRIPTION")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_run_script_bin_name(home: &str) -> PathBuf {
|
||||||
|
let rust_script = PathBuf::from(format!("{}/.cargo/bin/rust-script", home));
|
||||||
|
if !rust_script.exists() {
|
||||||
|
warning!("rust-script not found, install it...");
|
||||||
|
let mut cargo_install_rust_script = Command::new("cargo");
|
||||||
|
cargo_install_rust_script.args(&["install", "--git", "https://git.hatter.ink/hatter/runrs", "rust-script"]);
|
||||||
|
debugging!("Run command: {:?}", cargo_install_rust_script);
|
||||||
|
match util_cmd::run_command_and_wait(&mut cargo_install_rust_script) {
|
||||||
|
Err(e) => failure_and_exit!("Install rust-script failed: {}", e),
|
||||||
|
Ok(exist_status) if !exist_status.success() => failure!("Install rust-script not success: {}", exist_status),
|
||||||
|
Ok(_) => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rust_script
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_user_home() -> String {
|
||||||
|
env::var("HOME").unwrap_or_else(|_|
|
||||||
|
failure_and_exit!("$HOME not found!")
|
||||||
|
)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user