Accept -p/--path as command line argument

This commit is contained in:
Thomas Forgione 2019-06-12 15:06:04 +02:00
parent 1ce0327bdb
commit f538932fc5
No known key found for this signature in database
GPG Key ID: 203DAEA747F48F41
1 changed files with 57 additions and 3 deletions

View File

@ -15,6 +15,42 @@ fn main() {
beautylog::init(log::LevelFilter::Info).ok(); beautylog::init(log::LevelFilter::Info).ok();
let mut args = vec![];
let mut iter = env::args().skip(1);
let current_dir = {
let mut current_dir = None;
loop {
match iter.next() {
Some(ref x) if (x == "-p" || x == "--path") && current_dir.is_some() => {
error!("path is specified multiple times");
exit(1);
},
Some(ref x) if x == "-p" || x == "--path" => {
match iter.next() {
Some(v) => current_dir = Some(v.clone()),
None => {
error!("expecting a path after -p");
exit(1);
}
}
},
Some(ref x) => {
args.push(x.clone());
},
None => break current_dir,
}
}
};
let current_dir = current_dir.map(|x| {
if ! x.starts_with("/") {
let current_dir = match env::current_dir() { let current_dir = match env::current_dir() {
Err(e) => { Err(e) => {
error!("couldn't find current directory: {}", e); error!("couldn't find current directory: {}", e);
@ -25,8 +61,24 @@ fn main() {
p.unwrap().to_owned() p.unwrap().to_owned()
}, },
}; };
format!("{}/{}", current_dir, x)
} else {
x
}
});
let args = env::args().skip(1).collect::<Vec<_>>().join("&"); let current_dir = current_dir.unwrap_or_else(|| match env::current_dir() {
Err(e) => {
error!("couldn't find current directory: {}", e);
exit(1);
},
Ok(path) => {
let p = path.to_str();
p.unwrap().to_owned()
},
});
let args = args.join("&");
let url = if args.is_empty() { let url = if args.is_empty() {
format!("http://localhost:1500{}", current_dir) format!("http://localhost:1500{}", current_dir)
@ -34,6 +86,8 @@ fn main() {
format!("http://localhost:1500{}?{}", current_dir, args) format!("http://localhost:1500{}?{}", current_dir, args)
}; };
println!("{}", url);
let url = percent_encode(url.as_bytes(), DEFAULT_ENCODE_SET).to_string(); let url = percent_encode(url.as_bytes(), DEFAULT_ENCODE_SET).to_string();
let url = url.parse::<hyper::Uri>().unwrap(); let url = url.parse::<hyper::Uri>().unwrap();
rt::run(fetch_url(url)); rt::run(fetch_url(url));