feat: v0.1.12

This commit is contained in:
2023-02-10 23:44:45 +08:00
parent 38ab32052d
commit a8eb20dbb1
4 changed files with 57 additions and 5 deletions

2
Cargo.lock generated
View File

@@ -128,7 +128,7 @@ dependencies = [
[[package]]
name = "runrs"
version = "0.1.11"
version = "0.1.12"
dependencies = [
"rust_util",
"sha256",

View File

@@ -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"

View File

@@ -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::<Vec<_>>();
if args.is_empty() {
failure_and_exit!("runrs v{}, need arguments, e.g.\n\nrunrs <script.rs> [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 <script.rs> [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"),

32
src/script.template Normal file
View File

@@ -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<String>,
}
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);
}