use lws_client::LwsVfsIns; use std::thread; use clap::{Arg, App}; extern crate log; use std::env; const DEFAULT_PORT: u16 = 33444; // use std::process::{Command,Stdio}; // fn get_ssh_clinet() -> String { // // Run `who` command // let who = Command::new("who") // .stdout(Stdio::piped()) // .spawn().unwrap(); // // Run `grep $(whoami)` command // let whoami = Command::new("whoami") // .stdout(Stdio::piped()) // .output().unwrap(); // let user = String::from_utf8_lossy(&whoami.stdout).trim().to_string(); // let grep = Command::new("grep") // .arg(&user) // .stdin(Stdio::from(who.stdout.unwrap())) // .stdout(Stdio::piped()) // .spawn().unwrap(); // // Run `awk '{print $5}'` command // let awk = Command::new("awk") // .arg("{print $5}") // .stdin(Stdio::from(grep.stdout.unwrap())) // .stdout(Stdio::piped()) // .spawn().unwrap(); // // Collect the output // let output = awk.wait_with_output().unwrap(); // let output = String::from_utf8_lossy(&output.stdout).to_string(); // println!("output: {}",output); // let mut iter = output.split("("); // iter.next(); // let output = iter.next().unwrap().split(")").collect::>()[0].to_string(); // println!("output: {}",output); // output // } fn get_ssh_clinet() -> String { // 获取特定环境变量的值 let client = env::var("SSH_CLIENT").expect("only support auto get ssh connection"); let client = client.split(' ').next().unwrap(); client.to_string() } fn param_parser() -> (String, String) { let matches = App::new("lws_client") .version("1.0") .author("Ekko.bao") .about("linux&windows shared filesystem") .arg(Arg::with_name("s") .short('s') .long("server") .takes_value(true) .value_name("server") .help(format!("server addr: [ip[:port]], default is [sshclient:{}]", DEFAULT_PORT).as_str())) .arg(Arg::with_name("m") .short('m') .long("mount") .takes_value(true) .value_name("mount point") .required(true) .help("mount point, eg: ~/mnt")) .get_matches(); let server = match matches.value_of("s") { Some(server) => { let split = server.split(":").collect::>(); if split.len() == 2 { server.to_string() } else { format!("{}:{}", split[0], DEFAULT_PORT) } }, None => { let ip = get_ssh_clinet(); format!("{}:{}", ip, DEFAULT_PORT) } }; log::info!("args server: [{}]", server); let mount_point = matches.value_of("m").unwrap().to_string(); log::info!("args mount_point: [{}]", mount_point); (server, mount_point) } #[tokio::main] async fn main() -> Result<(), Box> { env_logger::init(); let (server, mount_point) = param_parser(); let lws_ins = match LwsVfsIns::new(&server).await { Ok(ins) => ins, Err(e) => { log::error!("Error creating lws server instance: {:?}", e); return Err(e); } }; log::info!("lws client instance created"); match lws_ins.hello().await { Err(e) => { log::error!("lws client instance hello err {:?}", e); return Err(e); } _ => {} } log::info!("start mount process"); let handle = thread::spawn(move ||{ match LwsVfsIns::mount(&mount_point, lws_ins) { Ok(_) => { Ok::(0) }, Err(e) => { log::error!("mount err {:?}", e); Ok::(-1) } } }); match handle.join() { Ok(_) => { Ok(()) }, Err(e) => { log::error!("mount thread start err {:?}", e); Err(Box::new(std::io::Error::new(std::io::ErrorKind::Other, "mount fail"))) } } }