feat: v1.1.0, support --script-repo|-R

This commit is contained in:
2025-01-18 20:30:02 +08:00
parent 39c1d0a68c
commit 11b4e6c005
7 changed files with 39 additions and 20 deletions

2
Cargo.lock generated
View File

@@ -927,7 +927,7 @@ dependencies = [
[[package]] [[package]]
name = "runrs" name = "runrs"
version = "1.0.1" version = "1.1.0"
dependencies = [ dependencies = [
"argh", "argh",
"reqwest", "reqwest",

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "runrs" name = "runrs"
version = "1.0.1" version = "1.1.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"

View File

@@ -5,17 +5,17 @@ use std::fs;
use std::os::unix::fs::PermissionsExt; use std::os::unix::fs::PermissionsExt;
use std::path::PathBuf; use std::path::PathBuf;
pub fn install_scripts(arguments: &[String]) { pub fn install_scripts(script_repo: &Option<String>, scripts: &[String]) {
if arguments.is_empty() { if scripts.is_empty() {
failure_and_exit!("No scripts assigned."); failure_and_exit!("No scripts assigned.");
} }
let script_meta_map = util::fetch_script_meta_or_die(); let script_meta_map = util::fetch_script_meta_or_die(script_repo);
for (i, script_name) in arguments.iter().enumerate() { for (i, script_name) in scripts.iter().enumerate() {
information!( information!(
"Installing script: {} [ {} / {} ]", "Installing script: {} [ {} / {} ]",
script_name, script_name,
(i + 1), (i + 1),
arguments.len() scripts.len()
); );
install_script(script_name, &script_meta_map); install_script(script_name, &script_meta_map);
} }

View File

@@ -2,8 +2,8 @@ use crate::util;
use std::fs; use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
pub fn list_scripts(filter: Option<&String>) { pub fn list_scripts(script_repo: &Option<String>, filter: Option<&String>) {
let script_meta_map = util::fetch_script_meta_or_die(); let script_meta_map = util::fetch_script_meta_or_die(script_repo);
let mut messages = vec![]; let mut messages = vec![];
for script_meta in script_meta_map.values() { for script_meta in script_meta_map.values() {

View File

@@ -26,6 +26,9 @@ struct RunScriptArgs {
/// install script /// install script
#[argh(switch, short = 'i')] #[argh(switch, short = 'i')]
install: bool, install: bool,
/// script repo
#[argh(option, short = 'R')]
script_repo: Option<String>,
/// output /// output
#[argh(option, short = 'o')] #[argh(option, short = 'o')]
output: Option<String>, output: Option<String>,
@@ -53,11 +56,11 @@ fn main() {
return; return;
} }
if rs_args.list { if rs_args.list {
list::list_scripts(rs_args.arguments.get(0)); list::list_scripts(&rs_args.script_repo, rs_args.arguments.get(0));
return; return;
} }
if rs_args.install { if rs_args.install {
install::install_scripts(&rs_args.arguments); install::install_scripts(&rs_args.script_repo, &rs_args.arguments);
return; return;
} }

View File

@@ -7,10 +7,10 @@ Show template:
$ runrs -t|--template [--output script.rs] $ runrs -t|--template [--output script.rs]
Show scriptions: Show scriptions:
$ runrs -l|--list [filter] $ runrs [-R|--script-repo runrs] -l|--list [filter]
Install script: Install script:
$ runrs -i|--install <script.rs> $ runrs [-R|--script-repo runrs] -i|--install <script.rs>
Run script: Run script:
$ runrs <script.rs> [arguments] $ runrs <script.rs> [arguments]

View File

@@ -8,6 +8,19 @@ use std::process::Command;
use std::time::SystemTime; use std::time::SystemTime;
use std::{env, fs, process}; use std::{env, fs, process};
#[cfg(feature = "switch-rust-lang")]
const RS_SCRIPT_META_URL: &str =
"https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/script-meta-v2.json";
#[cfg(feature = "switch-go-lang")]
const GO_SCRIPT_META_URL: &str =
"https://git.hatter.ink/hatter/go-scripts/raw/branch/main/script-meta-v2.json";
#[cfg(feature = "switch-ts-lang")]
const TS_SCRIPT_META_URL: &str =
"https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/script-meta-v2.json";
#[cfg(feature = "switch-dart-lang")]
const DART_SCRIPT_META_URL: &str =
"https://git.hatter.ink/hatter/dart-scripts/raw/branch/main/script-meta-v2.json";
#[allow(dead_code)] #[allow(dead_code)]
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct ScriptMeta { pub struct ScriptMeta {
@@ -19,15 +32,18 @@ pub struct ScriptMeta {
} }
#[allow(unreachable_code)] #[allow(unreachable_code)]
pub fn get_script_meta_url() -> &'static str { pub fn get_script_meta_url(script_repo: &Option<String>) -> String {
if let Some(script_repo) = script_repo {
return format!("https://hatter.ink/script/{script_repo}/script-meta-v2.action");
}
#[cfg(feature = "switch-rust-lang")] #[cfg(feature = "switch-rust-lang")]
return "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/script-meta-v2.json"; return RS_SCRIPT_META_URL.to_string();
#[cfg(feature = "switch-go-lang")] #[cfg(feature = "switch-go-lang")]
return "https://git.hatter.ink/hatter/go-scripts/raw/branch/main/script-meta-v2.json"; return GO_SCRIPT_META_URL.to_string();
#[cfg(feature = "switch-ts-lang")] #[cfg(feature = "switch-ts-lang")]
return "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/script-meta-v2.json"; return TS_SCRIPT_META_URL.to_string();
#[cfg(feature = "switch-dart-lang")] #[cfg(feature = "switch-dart-lang")]
return "https://git.hatter.ink/hatter/dart-scripts/raw/branch/main/script-meta-v2.json"; return DART_SCRIPT_META_URL.to_string();
panic!("Unknown feature assigned."); panic!("Unknown feature assigned.");
} }
@@ -44,8 +60,8 @@ pub fn get_cmd_name() -> &'static str {
panic!("Unknown feature assigned."); panic!("Unknown feature assigned.");
} }
pub fn fetch_script_meta_or_die() -> BTreeMap<String, ScriptMeta> { pub fn fetch_script_meta_or_die(script_repo: &Option<String>) -> BTreeMap<String, ScriptMeta> {
let file_meta_url = get_script_meta_url(); let file_meta_url = get_script_meta_url(script_repo);
debugging!("Loading URL: {}", file_meta_url); debugging!("Loading URL: {}", file_meta_url);
let response = reqwest::blocking::get(file_meta_url).unwrap_or_else(|e| { let response = reqwest::blocking::get(file_meta_url).unwrap_or_else(|e| {
failure_and_exit!("Get file-meta.json failed: {}", e); failure_and_exit!("Get file-meta.json failed: {}", e);