mars/src/client.rs

52 lines
1.2 KiB
Rust

extern crate colored;
extern crate hyper;
extern crate mars;
use std::env;
use std::process::exit;
use colored::*;
use hyper::Client;
use hyper::rt::{self, Future};
use mars::GeneralBuilder;
fn main() {
let current_dir = match env::current_dir() {
Err(e) => {
eprintln!("Couldn't find current directory: {:?}", e);
exit(1);
},
Ok(path) => {
let p = path.to_str();
p.unwrap().to_owned()
},
};
let args = env::args().skip(1).collect::<Vec<_>>().join("&");
let url = if args.is_empty() {
format!("http://localhost:1500{}", current_dir)
} else {
format!("http://localhost:1500{}?{}", current_dir, args)
};
let url = url.parse::<hyper::Uri>().unwrap();
rt::run(fetch_url(url));
}
fn fetch_url(url: hyper::Uri) -> impl Future<Item=(), Error=()> {
let client = Client::new();
client.get(url.clone())
.map(|_| ())
// If there was an error, build in client
.map_err(move |_| {
eprintln!("{}", "Server not listening, building in client...".bold().yellow());
let builder = GeneralBuilder::new();
let _ = builder.build(&url);
})
}