feat: v0.3.0, use argh
This commit is contained in:
145
src/main.rs
145
src/main.rs
@@ -1,9 +1,10 @@
|
||||
#[macro_use]
|
||||
extern crate rust_util;
|
||||
|
||||
use std::env;
|
||||
|
||||
use argh::FromArgs;
|
||||
use rust_util::util_os::get_user_home;
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
use std::{env, fs};
|
||||
|
||||
use crate::install::install_script;
|
||||
use crate::list::list_scripts;
|
||||
@@ -11,14 +12,14 @@ use crate::util::{
|
||||
build_script_command, get_run_script_bin_name, read_file_and_digest, run_script_command,
|
||||
};
|
||||
|
||||
#[cfg(feature = "switch-rust-lang")]
|
||||
mod help_rs;
|
||||
#[cfg(feature = "switch-dart-lang")]
|
||||
mod help_dart;
|
||||
#[cfg(feature = "switch-go-lang")]
|
||||
mod help_go;
|
||||
#[cfg(feature = "switch-js-lang")]
|
||||
mod help_js;
|
||||
#[cfg(feature = "switch-dart-lang")]
|
||||
mod help_dart;
|
||||
#[cfg(feature = "switch-rust-lang")]
|
||||
mod help_rs;
|
||||
mod install;
|
||||
mod list;
|
||||
mod util;
|
||||
@@ -32,58 +33,84 @@ const CMD_NAME: &str = "runjs";
|
||||
#[cfg(feature = "switch-dart-lang")]
|
||||
const CMD_NAME: &str = "rundart";
|
||||
|
||||
#[derive(FromArgs, PartialEq, Debug)]
|
||||
/// Run script
|
||||
struct RunScriptArgs {
|
||||
/// help message
|
||||
#[argh(switch, short = 'h')]
|
||||
help_message: bool,
|
||||
/// script template
|
||||
#[argh(switch, short = 't')]
|
||||
template: bool,
|
||||
/// list scripts
|
||||
#[argh(switch, short = 'l')]
|
||||
list: bool,
|
||||
/// install script
|
||||
#[argh(switch, short = 'i')]
|
||||
install: bool,
|
||||
/// output
|
||||
#[argh(option, short = 'o')]
|
||||
output: Option<String>,
|
||||
/// arguments
|
||||
#[argh(positional, greedy)]
|
||||
arguments: Vec<String>,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args = env::args().skip(1).collect::<Vec<_>>();
|
||||
if args.is_empty() {
|
||||
let rs_args: RunScriptArgs = argh::from_env();
|
||||
// println!("{:#?}", rs_args);
|
||||
|
||||
if env::args().len() == 1 {
|
||||
failure_and_exit!(
|
||||
"{} v{}, need arguments, `{} -h` or `{} --help` for help",
|
||||
"{} v{}, need arguments, `{} -h` or `{} --help-message` for help",
|
||||
CMD_NAME,
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
CMD_NAME,
|
||||
CMD_NAME,
|
||||
);
|
||||
}
|
||||
let first_argument = args
|
||||
.get(0)
|
||||
.unwrap_or_else(|| failure_and_exit!("Must assign a script file name"));
|
||||
if first_argument == "--help" || first_argument == "-h" {
|
||||
#[cfg(feature = "switch-rust-lang")]
|
||||
help_rs::print_help();
|
||||
#[cfg(feature = "switch-go-lang")]
|
||||
help_go::print_help();
|
||||
#[cfg(feature = "switch-js-lang")]
|
||||
help_js::print_help();
|
||||
#[cfg(feature = "switch-dart-lang")]
|
||||
help_dart::print_help();
|
||||
|
||||
if rs_args.help_message {
|
||||
print_help();
|
||||
return;
|
||||
}
|
||||
if first_argument == "--template" || first_argument == "-t" {
|
||||
#[cfg(feature = "switch-rust-lang")]
|
||||
println!("{}", include_str!("script.template.rs"));
|
||||
#[cfg(feature = "switch-go-lang")]
|
||||
println!("{}", include_str!("script.template.go"));
|
||||
#[cfg(feature = "switch-js-lang")]
|
||||
println!("{}", include_str!("script.template.js"));
|
||||
#[cfg(feature = "switch-dart-lang")]
|
||||
println!("{}", include_str!("script.template.dart"));
|
||||
if rs_args.template {
|
||||
print_template(&rs_args.output);
|
||||
return;
|
||||
}
|
||||
if first_argument == "--list" || first_argument == "-l" {
|
||||
let filter = args.get(1);
|
||||
list_scripts(filter);
|
||||
if rs_args.list {
|
||||
list_scripts(rs_args.arguments.get(1));
|
||||
return;
|
||||
}
|
||||
if first_argument == "--install" || first_argument == "-i" {
|
||||
install_script(args.iter().skip(1).collect());
|
||||
if rs_args.install {
|
||||
if rs_args.arguments.is_empty() {
|
||||
failure_and_exit!("No scripts assigned.");
|
||||
}
|
||||
for (i, script_name) in rs_args.arguments.iter().enumerate() {
|
||||
information!(
|
||||
"Installing script: {} [ {} / {} ]",
|
||||
script_name,
|
||||
(i + 1),
|
||||
rs_args.arguments.len()
|
||||
);
|
||||
install_script(script_name);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "switch-go-lang", feature = "switch-js-lang", feature = "switch-dart-lang"))]
|
||||
#[cfg(any(
|
||||
feature = "switch-go-lang",
|
||||
feature = "switch-js-lang",
|
||||
feature = "switch-dart-lang"
|
||||
))]
|
||||
if true {
|
||||
failure_and_exit!("Only rust supports run by runrs.");
|
||||
}
|
||||
|
||||
let script_file = first_argument;
|
||||
if rs_args.arguments.is_empty() {
|
||||
failure_and_exit!("Must assign a script file name");
|
||||
}
|
||||
let script_file = &rs_args.arguments[0];
|
||||
let (_, script_sha256) = read_file_and_digest(script_file);
|
||||
debugging!("File {} -> sha256: {}", script_file, script_sha256);
|
||||
|
||||
@@ -96,12 +123,54 @@ fn main() {
|
||||
&script_sha256,
|
||||
&cache_script_bin_name,
|
||||
);
|
||||
for arg in args.iter().skip(1) {
|
||||
for arg in rs_args.arguments.iter().skip(1) {
|
||||
run_script_cmd.arg(arg);
|
||||
}
|
||||
run_script_command(script_file, &cache_script_bin_name, &mut run_script_cmd)
|
||||
}
|
||||
|
||||
fn print_help() {
|
||||
#[cfg(feature = "switch-rust-lang")]
|
||||
help_rs::print_help();
|
||||
#[cfg(feature = "switch-go-lang")]
|
||||
help_go::print_help();
|
||||
#[cfg(feature = "switch-js-lang")]
|
||||
help_js::print_help();
|
||||
#[cfg(feature = "switch-dart-lang")]
|
||||
help_dart::print_help();
|
||||
}
|
||||
|
||||
fn print_template(output: &Option<String>) {
|
||||
#[cfg(feature = "switch-rust-lang")]
|
||||
let script = include_str!("script.template.rs");
|
||||
#[cfg(feature = "switch-go-lang")]
|
||||
let script = include_str!("script.template.go");
|
||||
#[cfg(feature = "switch-js-lang")]
|
||||
let script = include_str!("script.template.js");
|
||||
#[cfg(feature = "switch-dart-lang")]
|
||||
let script = include_str!("script.template.dart");
|
||||
match output {
|
||||
None => {
|
||||
println!("{}", script);
|
||||
}
|
||||
Some(output_file) => {
|
||||
if fs::metadata(output_file).is_ok() {
|
||||
failure!("Output script file exists: {}", output_file);
|
||||
} else {
|
||||
match fs::write(output_file, script) {
|
||||
Ok(_) => {
|
||||
success!("Write script file success: {}", output_file);
|
||||
let _ = fs::set_permissions(&output_file, PermissionsExt::from_mode(0o755));
|
||||
}
|
||||
Err(e) => {
|
||||
failure!("Write script file: {}, failed: {}", output_file, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_cache_script_bin_name(user_home: &str, script_sha256: &str) -> String {
|
||||
#[cfg(target_os = "macos")]
|
||||
let cache_script_bin_name = format!(
|
||||
|
||||
Reference in New Issue
Block a user