feat: v1.1.0, support --script-repo|-R
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -927,7 +927,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "runrs"
|
||||
version = "1.0.1"
|
||||
version = "1.1.0"
|
||||
dependencies = [
|
||||
"argh",
|
||||
"reqwest",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "runrs"
|
||||
version = "1.0.1"
|
||||
version = "1.1.0"
|
||||
edition = "2018"
|
||||
license = "MIT/Apache-2.0"
|
||||
description = "A Tool for Run Rust Scripts"
|
||||
|
||||
@@ -5,17 +5,17 @@ use std::fs;
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub fn install_scripts(arguments: &[String]) {
|
||||
if arguments.is_empty() {
|
||||
pub fn install_scripts(script_repo: &Option<String>, scripts: &[String]) {
|
||||
if scripts.is_empty() {
|
||||
failure_and_exit!("No scripts assigned.");
|
||||
}
|
||||
let script_meta_map = util::fetch_script_meta_or_die();
|
||||
for (i, script_name) in arguments.iter().enumerate() {
|
||||
let script_meta_map = util::fetch_script_meta_or_die(script_repo);
|
||||
for (i, script_name) in scripts.iter().enumerate() {
|
||||
information!(
|
||||
"Installing script: {} [ {} / {} ]",
|
||||
script_name,
|
||||
(i + 1),
|
||||
arguments.len()
|
||||
scripts.len()
|
||||
);
|
||||
install_script(script_name, &script_meta_map);
|
||||
}
|
||||
|
||||
@@ -2,8 +2,8 @@ use crate::util;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub fn list_scripts(filter: Option<&String>) {
|
||||
let script_meta_map = util::fetch_script_meta_or_die();
|
||||
pub fn list_scripts(script_repo: &Option<String>, filter: Option<&String>) {
|
||||
let script_meta_map = util::fetch_script_meta_or_die(script_repo);
|
||||
|
||||
let mut messages = vec![];
|
||||
for script_meta in script_meta_map.values() {
|
||||
|
||||
@@ -26,6 +26,9 @@ struct RunScriptArgs {
|
||||
/// install script
|
||||
#[argh(switch, short = 'i')]
|
||||
install: bool,
|
||||
/// script repo
|
||||
#[argh(option, short = 'R')]
|
||||
script_repo: Option<String>,
|
||||
/// output
|
||||
#[argh(option, short = 'o')]
|
||||
output: Option<String>,
|
||||
@@ -53,11 +56,11 @@ fn main() {
|
||||
return;
|
||||
}
|
||||
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;
|
||||
}
|
||||
if rs_args.install {
|
||||
install::install_scripts(&rs_args.arguments);
|
||||
install::install_scripts(&rs_args.script_repo, &rs_args.arguments);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -7,10 +7,10 @@ Show template:
|
||||
$ runrs -t|--template [--output script.rs]
|
||||
|
||||
Show scriptions:
|
||||
$ runrs -l|--list [filter]
|
||||
$ runrs [-R|--script-repo runrs] -l|--list [filter]
|
||||
|
||||
Install script:
|
||||
$ runrs -i|--install <script.rs>
|
||||
$ runrs [-R|--script-repo runrs] -i|--install <script.rs>
|
||||
|
||||
Run script:
|
||||
$ runrs <script.rs> [arguments]
|
||||
|
||||
30
src/util.rs
30
src/util.rs
@@ -8,6 +8,19 @@ use std::process::Command;
|
||||
use std::time::SystemTime;
|
||||
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)]
|
||||
#[derive(Deserialize)]
|
||||
pub struct ScriptMeta {
|
||||
@@ -19,15 +32,18 @@ pub struct ScriptMeta {
|
||||
}
|
||||
|
||||
#[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")]
|
||||
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")]
|
||||
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")]
|
||||
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")]
|
||||
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.");
|
||||
}
|
||||
|
||||
@@ -44,8 +60,8 @@ pub fn get_cmd_name() -> &'static str {
|
||||
panic!("Unknown feature assigned.");
|
||||
}
|
||||
|
||||
pub fn fetch_script_meta_or_die() -> BTreeMap<String, ScriptMeta> {
|
||||
let file_meta_url = get_script_meta_url();
|
||||
pub fn fetch_script_meta_or_die(script_repo: &Option<String>) -> BTreeMap<String, ScriptMeta> {
|
||||
let file_meta_url = get_script_meta_url(script_repo);
|
||||
debugging!("Loading URL: {}", file_meta_url);
|
||||
let response = reqwest::blocking::get(file_meta_url).unwrap_or_else(|e| {
|
||||
failure_and_exit!("Get file-meta.json failed: {}", e);
|
||||
|
||||
Reference in New Issue
Block a user