feat: v0.3.0, use argh
This commit is contained in:
886
Cargo.lock
generated
886
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "runrs"
|
name = "runrs"
|
||||||
version = "0.2.8"
|
version = "0.3.0"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
license = "MIT/Apache-2.0"
|
license = "MIT/Apache-2.0"
|
||||||
description = "A Tool for Run Rust Scripts"
|
description = "A Tool for Run Rust Scripts"
|
||||||
@@ -21,3 +21,4 @@ rust_util = "0.6.41"
|
|||||||
sha256 = "1.0.3"
|
sha256 = "1.0.3"
|
||||||
serde = { version = "1.0.193", features = ["derive"] }
|
serde = { version = "1.0.193", features = ["derive"] }
|
||||||
serde_json = "1.0.125"
|
serde_json = "1.0.125"
|
||||||
|
argh = "0.1.13"
|
||||||
|
|||||||
12
justfile
12
justfile
@@ -4,6 +4,18 @@ _:
|
|||||||
install:
|
install:
|
||||||
cargo install --path .
|
cargo install --path .
|
||||||
|
|
||||||
|
run-rust *args:
|
||||||
|
cargo run -- {{args}}
|
||||||
|
|
||||||
|
run-go *args:
|
||||||
|
cargo run --no-default-features --features switch-go-lang -- {{args}}
|
||||||
|
|
||||||
|
run-js *args:
|
||||||
|
cargo run --no-default-features --features switch-js-lang -- {{args}}
|
||||||
|
|
||||||
|
run-dart *args:
|
||||||
|
cargo run --no-default-features --features switch-dart-lang -- {{args}}
|
||||||
|
|
||||||
build-run-rust:
|
build-run-rust:
|
||||||
cargo build --release
|
cargo build --release
|
||||||
|
|
||||||
|
|||||||
@@ -2,35 +2,45 @@ use std::fs;
|
|||||||
use std::os::unix::fs::PermissionsExt;
|
use std::os::unix::fs::PermissionsExt;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use rust_util::util_os::get_user_home;
|
|
||||||
use crate::util::{SCRIPT_DOT_EXT, SCRIPT_HYPHEN_EXT};
|
use crate::util::{SCRIPT_DOT_EXT, SCRIPT_HYPHEN_EXT};
|
||||||
|
use rust_util::util_os::get_user_home;
|
||||||
|
|
||||||
#[cfg(feature = "switch-rust-lang")]
|
#[cfg(feature = "switch-rust-lang")]
|
||||||
const SCRIPT_PATTERN: &str = "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/$NAME/src/main.rs";
|
const SCRIPT_PATTERN: &str =
|
||||||
|
"https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/$NAME/src/main.rs";
|
||||||
#[cfg(feature = "switch-go-lang")]
|
#[cfg(feature = "switch-go-lang")]
|
||||||
const SCRIPT_PATTERN: &str = "https://git.hatter.ink/hatter/go-scripts/raw/branch/main/$NAME/main.go";
|
const SCRIPT_PATTERN: &str =
|
||||||
|
"https://git.hatter.ink/hatter/go-scripts/raw/branch/main/$NAME/main.go";
|
||||||
#[cfg(feature = "switch-js-lang")]
|
#[cfg(feature = "switch-js-lang")]
|
||||||
const SCRIPT_PATTERN: &str = "https://git.hatter.ink/hatter/js-scripts/raw/branch/main/$NAME/main.js";
|
const SCRIPT_PATTERN: &str =
|
||||||
|
"https://git.hatter.ink/hatter/js-scripts/raw/branch/main/$NAME/main.js";
|
||||||
#[cfg(feature = "switch-dart-lang")]
|
#[cfg(feature = "switch-dart-lang")]
|
||||||
const SCRIPT_PATTERN: &str = "https://git.hatter.ink/hatter/dart-scripts/raw/branch/main/$NAME/main.dart";
|
const SCRIPT_PATTERN: &str =
|
||||||
|
"https://git.hatter.ink/hatter/dart-scripts/raw/branch/main/$NAME/main.dart";
|
||||||
|
|
||||||
pub fn install_script(args: Vec<&String>) {
|
pub fn install_script(script_name: &str) {
|
||||||
if args.is_empty() {
|
|
||||||
failure_and_exit!("No script name assigned!");
|
|
||||||
}
|
|
||||||
let script_name = &args[0];
|
|
||||||
let normalized_script_name = if script_name.ends_with(SCRIPT_DOT_EXT) {
|
let normalized_script_name = if script_name.ends_with(SCRIPT_DOT_EXT) {
|
||||||
script_name.chars().take(script_name.len() - SCRIPT_DOT_EXT.len()).collect::<String>() + SCRIPT_HYPHEN_EXT
|
script_name
|
||||||
|
.chars()
|
||||||
|
.take(script_name.len() - SCRIPT_DOT_EXT.len())
|
||||||
|
.collect::<String>()
|
||||||
|
+ SCRIPT_HYPHEN_EXT
|
||||||
} else if script_name.ends_with(SCRIPT_HYPHEN_EXT) {
|
} else if script_name.ends_with(SCRIPT_HYPHEN_EXT) {
|
||||||
script_name.to_string()
|
script_name.to_string()
|
||||||
} else {
|
} else {
|
||||||
failure_and_exit!("Invalid script name: {}", script_name);
|
failure!("Invalid script name: {}", script_name);
|
||||||
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
let script_file_name = normalized_script_name
|
let script_file_name = normalized_script_name
|
||||||
.chars().take(normalized_script_name.len() - SCRIPT_HYPHEN_EXT.len()).collect::<String>() + SCRIPT_DOT_EXT;
|
.chars()
|
||||||
|
.take(normalized_script_name.len() - SCRIPT_HYPHEN_EXT.len())
|
||||||
|
.collect::<String>()
|
||||||
|
+ SCRIPT_DOT_EXT;
|
||||||
let user_home = get_user_home().expect("Get user home failed!");
|
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);
|
let output_file_name = PathBuf::from(&user_home)
|
||||||
|
.join("bin")
|
||||||
|
.join(&script_file_name);
|
||||||
if output_file_name.exists() {
|
if output_file_name.exists() {
|
||||||
warning!("Script file: {} exists", output_file_name.to_string_lossy());
|
warning!("Script file: {} exists", output_file_name.to_string_lossy());
|
||||||
}
|
}
|
||||||
@@ -42,33 +52,56 @@ pub fn install_script(args: Vec<&String>) {
|
|||||||
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) => failure_and_exit!("Get script failed: {}", e),
|
Err(e) => {
|
||||||
Ok(response) => response
|
failure!("Get script failed: {}", e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
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 {
|
||||||
failure_and_exit!("Script not found!");
|
failure!("Script not found!");
|
||||||
|
return;
|
||||||
} 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!("Get script failed: {}", get_script_response_status);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let text = match get_script_response.text() {
|
let text = match get_script_response.text() {
|
||||||
Err(e) => failure_and_exit!("Get script: {} failed: {}", &script_url, e),
|
Err(e) => {
|
||||||
Ok(text) => text
|
failure!("Get script: {} failed: {}", &script_url, e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Ok(text) => text,
|
||||||
};
|
};
|
||||||
if let Ok(script_content) = fs::read_to_string(&output_file_name) {
|
if let Ok(script_content) = fs::read_to_string(&output_file_name) {
|
||||||
if text == script_content {
|
if text == script_content {
|
||||||
success!("File is extract same, skip write file: {}", output_file_name.to_string_lossy());
|
success!(
|
||||||
|
"File is extract same, skip write file: {}",
|
||||||
|
output_file_name.to_string_lossy()
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Err(e) = fs::write(&output_file_name, text) {
|
if let Err(e) = fs::write(&output_file_name, text) {
|
||||||
failure_and_exit!("Write script: {} failed: {}", output_file_name.to_string_lossy(), e)
|
failure!(
|
||||||
|
"Write script: {} failed: {}",
|
||||||
|
output_file_name.to_string_lossy(),
|
||||||
|
e
|
||||||
|
);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
success!("Write file: {} success", output_file_name.to_string_lossy());
|
success!("Write file: {} success", output_file_name.to_string_lossy());
|
||||||
match fs::set_permissions(&output_file_name, PermissionsExt::from_mode(0o755)) {
|
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),
|
Err(e) => failure!(
|
||||||
Ok(_) => success!("Chmod script: {} permission succeed", output_file_name.to_string_lossy())
|
"Chmod script: {} permission failed: {}",
|
||||||
|
output_file_name.to_string_lossy(),
|
||||||
|
e
|
||||||
|
),
|
||||||
|
Ok(_) => success!(
|
||||||
|
"Chmod script: {} permission succeed",
|
||||||
|
output_file_name.to_string_lossy()
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
145
src/main.rs
145
src/main.rs
@@ -1,9 +1,10 @@
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate rust_util;
|
extern crate rust_util;
|
||||||
|
|
||||||
use std::env;
|
use argh::FromArgs;
|
||||||
|
|
||||||
use rust_util::util_os::get_user_home;
|
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::install::install_script;
|
||||||
use crate::list::list_scripts;
|
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,
|
build_script_command, get_run_script_bin_name, read_file_and_digest, run_script_command,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(feature = "switch-rust-lang")]
|
#[cfg(feature = "switch-dart-lang")]
|
||||||
mod help_rs;
|
mod help_dart;
|
||||||
#[cfg(feature = "switch-go-lang")]
|
#[cfg(feature = "switch-go-lang")]
|
||||||
mod help_go;
|
mod help_go;
|
||||||
#[cfg(feature = "switch-js-lang")]
|
#[cfg(feature = "switch-js-lang")]
|
||||||
mod help_js;
|
mod help_js;
|
||||||
#[cfg(feature = "switch-dart-lang")]
|
#[cfg(feature = "switch-rust-lang")]
|
||||||
mod help_dart;
|
mod help_rs;
|
||||||
mod install;
|
mod install;
|
||||||
mod list;
|
mod list;
|
||||||
mod util;
|
mod util;
|
||||||
@@ -32,58 +33,84 @@ const CMD_NAME: &str = "runjs";
|
|||||||
#[cfg(feature = "switch-dart-lang")]
|
#[cfg(feature = "switch-dart-lang")]
|
||||||
const CMD_NAME: &str = "rundart";
|
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() {
|
fn main() {
|
||||||
let args = env::args().skip(1).collect::<Vec<_>>();
|
let rs_args: RunScriptArgs = argh::from_env();
|
||||||
if args.is_empty() {
|
// println!("{:#?}", rs_args);
|
||||||
|
|
||||||
|
if env::args().len() == 1 {
|
||||||
failure_and_exit!(
|
failure_and_exit!(
|
||||||
"{} v{}, need arguments, `{} -h` or `{} --help` for help",
|
"{} v{}, need arguments, `{} -h` or `{} --help-message` for help",
|
||||||
CMD_NAME,
|
CMD_NAME,
|
||||||
env!("CARGO_PKG_VERSION"),
|
env!("CARGO_PKG_VERSION"),
|
||||||
CMD_NAME,
|
CMD_NAME,
|
||||||
CMD_NAME,
|
CMD_NAME,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
let first_argument = args
|
|
||||||
.get(0)
|
if rs_args.help_message {
|
||||||
.unwrap_or_else(|| failure_and_exit!("Must assign a script file name"));
|
print_help();
|
||||||
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();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if first_argument == "--template" || first_argument == "-t" {
|
if rs_args.template {
|
||||||
#[cfg(feature = "switch-rust-lang")]
|
print_template(&rs_args.output);
|
||||||
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"));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if first_argument == "--list" || first_argument == "-l" {
|
if rs_args.list {
|
||||||
let filter = args.get(1);
|
list_scripts(rs_args.arguments.get(1));
|
||||||
list_scripts(filter);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if first_argument == "--install" || first_argument == "-i" {
|
if rs_args.install {
|
||||||
install_script(args.iter().skip(1).collect());
|
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;
|
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 {
|
if true {
|
||||||
failure_and_exit!("Only rust supports run by runrs.");
|
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);
|
let (_, script_sha256) = read_file_and_digest(script_file);
|
||||||
debugging!("File {} -> sha256: {}", script_file, script_sha256);
|
debugging!("File {} -> sha256: {}", script_file, script_sha256);
|
||||||
|
|
||||||
@@ -96,12 +123,54 @@ fn main() {
|
|||||||
&script_sha256,
|
&script_sha256,
|
||||||
&cache_script_bin_name,
|
&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_cmd.arg(arg);
|
||||||
}
|
}
|
||||||
run_script_command(script_file, &cache_script_bin_name, &mut run_script_cmd)
|
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 {
|
fn get_cache_script_bin_name(user_home: &str, script_sha256: &str) -> String {
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
let cache_script_bin_name = format!(
|
let cache_script_bin_name = format!(
|
||||||
|
|||||||
Reference in New Issue
Block a user