use clap::{App, Arg}; use lws_client::LwsVfsIns; use std::thread; extern crate log; use std::env; const DEFAULT_PORT: u16 = 33444; const DEFAULT_CACHE_LIFE: u32 = 10_000; // 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, u32) { 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) .min_values(1) .max_values(1) .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) .min_values(1) .max_values(1) .value_name("mount point") .required(true) .help("mount point, eg: ~/mnt"), ) .arg( Arg::with_name("c") .short('c') .long("cache") .takes_value(true) .min_values(1) .max_values(1) .value_name("cache invalid time") .help("file info cache invalid time, unit: ms, defualt is 10_000ms"), ) .get_matches(); let (ip, port) = match matches.value_of("s") { Some(server) => { let split = server.split(":").collect::>(); if split.len() == 2 { (split[0].to_string(), split[1].to_string()) } else { (split[0].to_string(), DEFAULT_PORT.to_string()) } } None => (String::new(), String::new()), }; let ip = if ip.len() == 0 { get_ssh_clinet() } else { ip }; let port = if port.len() == 0 { DEFAULT_PORT.to_string() } else { port }; let server = format!("{}:{}", ip, port); log::info!("args server: [{}]", server); let mount_point = matches.value_of("m").unwrap().to_string(); log::info!("args mount_point: [{}]", mount_point); let cache_life = match matches.value_of("c") { Some(cache_life) => cache_life.parse().unwrap(), None => DEFAULT_CACHE_LIFE, }; log::info!("args cache invalid time: [{}ms]", cache_life); (server, mount_point, cache_life) } #[tokio::main] async fn main() -> Result<(), Box> { env_logger::init(); let (server, mount_point, cache_life) = param_parser(); let lws_ins = match LwsVfsIns::new(&server, cache_life).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", ))) } } }