diff --git a/Cargo.lock b/Cargo.lock index 92e41b0..720fae5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -128,7 +128,7 @@ dependencies = [ [[package]] name = "runrs" -version = "0.1.11" +version = "0.1.12" dependencies = [ "rust_util", "sha256", diff --git a/Cargo.toml b/Cargo.toml index 9f8e8b4..00a8668 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "runrs" -version = "0.1.11" +version = "0.1.12" edition = "2018" license = "MIT/Apache-2.0" description = "Run Rust Scripts" diff --git a/src/main.rs b/src/main.rs index 7e4453f..425ebb4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,19 +8,26 @@ use std::time::SystemTime; use rust_util::util_cmd; +const SCRIPT_TEMPLATE: &'static str = include_str!("script.template"); + fn main() { let user_home = get_user_home(); let rust_script = get_run_script_bin_name(&user_home); let args = env::args().skip(1).collect::>(); if args.is_empty() { - failure_and_exit!("runrs v{}, need arguments, e.g.\n\nrunrs [arguments]\n", env!("CARGO_PKG_VERSION")); + failure_and_exit!("runrs v{}, need arguments, `runrs --help` for help", env!("CARGO_PKG_VERSION")); } let first_argument = args.get(0).unwrap_or_else(|| failure_and_exit!("Must assign a script file name")); - if first_argument == "--help" { + if first_argument == "--help" || first_argument == "-h" { print_help(); return; } + if first_argument == "--template" || first_argument == "-t" { + println!("{}", SCRIPT_TEMPLATE); + return; + } + let script_file = first_argument; let (_, script_sha256) = read_file_and_digest(script_file); @@ -89,10 +96,23 @@ fn print_help() { println!(r##"{} v{} - {} Help: -runrs --help +runrs -h|--help + +Show template: +runrs -t|--template Run Rust Script: runrs [arguments] + +Environment variables: +| Variable | Description | +| -------------------- | ------------------------------------------------------------ | +| HOME | User home, default by OS system | +| RUNRS_SKIP_CACHE | Skip compiled cached file, bool , turn on true, yes, on or 1 | +| RUNRS_REBUILD | Force rebuild, bool | +| RUNRS_SILENT_BUILD | Build new binary in silent mode, bool | +| RUNRS_RUST_SCRIPT | rust_script command line bin file | +| RUNRS_MAX_SCRIPT_LEN | Max script length, default 1MB | "##, env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"), diff --git a/src/script.template b/src/script.template new file mode 100644 index 0000000..2fe07d4 --- /dev/null +++ b/src/script.template @@ -0,0 +1,32 @@ +#!/usr/bin/env runrs + +//! ```cargo +//! [dependencies] +//! serde = { version = "1.0", features = ["derive"] } +//! serde_json = "1.0" +//! reqwest = { version = "0.11", features = ["blocking", "json"] } +//! rust_util = { version = "0.6" } +//! ``` + +use rust_util::{failure_and_exit, success}; +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize)] +struct IpResponse { + status: u16, + message: String, + ip: String, + #[serde(rename = "userAgent")] + user_agent: Option, +} + +const GET_IP_URL: &'static str = "https://hatter.ink/ip/ip.jsonp"; + +fn main() { + let ip_response: IpResponse = reqwest::blocking::get(GET_IP_URL).unwrap_or_else(|e| { + failure_and_exit!("Send request to: {} failed: {}", GET_IP_URL, e); + }).json().unwrap_or_else(|e| { + failure_and_exit!("Parse response from: {}, failed: {}", GET_IP_URL, e); + }); + success!("Your IP address is: {}", ip_response.ip); +} \ No newline at end of file