| { | ||
| "git": { | ||
| "sha1": "08aa7851327684b0f1b3822f6f26cff615c2978b" | ||
| "sha1": "1170c2d81e4b7f791affd2f5c8e1020833b084b9" | ||
| } | ||
| } |
+1
-1
@@ -16,3 +16,3 @@ # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO | ||
| name = "rshc" | ||
| version = "0.1.0" | ||
| version = "0.1.1" | ||
| authors = ["Yukang <moorekang@gmail.com>"] | ||
@@ -19,0 +19,0 @@ description = "rshc: Rust compile shell script(or expect script) to Rust code and binary." |
+56
-2
@@ -13,2 +13,4 @@ extern crate clap; | ||
| fn main() { | ||
@@ -66,2 +68,54 @@ let matches = App::new("Shell script compiled to Rustc code and binary") | ||
| let template = r###" | ||
| use std::io; | ||
| use std::io::Write; | ||
| use std::process; | ||
| use std::process::{Command, Stdio}; | ||
| use std::env; | ||
| fn run_process(iterp: &String, prog: &String, args: &Vec<String>) { | ||
| let prog = format!("{}", prog.to_owned()); | ||
| //println!("{}", prog); | ||
| let output = Command::new(iterp) | ||
| .arg("-c") | ||
| .arg(prog) | ||
| .args(args) | ||
| .stdin(Stdio::inherit()) | ||
| .stdout(Stdio::inherit()) | ||
| .stderr(Stdio::inherit()) | ||
| .output() | ||
| .expect("failed to execute process"); | ||
| //println!("status: {}", output.status.code().unwrap()); | ||
| std::process::exit(output.status.code().unwrap()); | ||
| } | ||
| fn main() { | ||
| let prog = { script_code }; | ||
| let pass = "{ pass }"; | ||
| let iterp = "{ interp }"; | ||
| //println!("res: {:?}", prog); | ||
| if pass.len() != 0 { | ||
| let mut input = String::new(); | ||
| print!("Password: "); | ||
| io::stdout().flush().ok(); | ||
| io::stdin() | ||
| .read_line(&mut input) | ||
| .ok() | ||
| .expect("Couldn't read password"); | ||
| if input.trim() != pass { | ||
| println!("Invalid password!"); | ||
| process::exit(1); | ||
| } | ||
| } | ||
| let prog_str = String::from_utf8(prog).unwrap(); | ||
| //println!("running ...:\n {}", prog_str); | ||
| let mut args = env::args().collect::<Vec<_>>(); | ||
| args[0] = String::from(""); | ||
| run_process(&iterp.to_string(), &prog_str, &args); | ||
| } | ||
| "###; | ||
| let content = fs::read_to_string(file).expect("Failed to read source file"); | ||
@@ -72,4 +126,4 @@ let _encoded = util::encode(content.clone(), "hello".to_string()); // we need to encode it latter | ||
| let encoded_str = format!("vec!{:?}\n", content.as_bytes()); | ||
| let prog = fs::read_to_string("./src/prog.rs").expect("Failed to read prog file"); | ||
| let prog = prog | ||
| // let prog = fs::read_to_string("./src/prog.rs").expect("Failed to read prog file"); | ||
| let prog = template | ||
| .replace("{ script_code }", &encoded_str) | ||
@@ -76,0 +130,0 @@ .replace("{ pass }", &pass) |
Sorry, the diff of this file is not supported yet