Skip to content

Commit

Permalink
refactor(main.rs): create the fetch_from_internet function
Browse files Browse the repository at this point in the history
  • Loading branch information
luthfianto committed Jan 6, 2016
1 parent 3ca297a commit 7c29036
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,38 @@ const PLATFORM: &'static str = "osx";
#[cfg(not(target_os = "macos"))]
const PLATFORM: &'static str = "linux";

fn main() {
let args: Vec<String> = env::args().collect();

if args.len() < 2 {
let error = "Specify an argument!";
println!("{}", format!("{error} \nExample: \n$ tldr ls", error = Red.paint(error).to_string()));
process::exit(1);
}

let common_url = format!("https://raw.github.com/tldr-pages/tldr/master/pages/common/{page}.md", page = args[1]);
let platform_url = format!("https://raw.github.com/tldr-pages/tldr/master/pages/{platform}/{page}.md", page = args[1], platform=PLATFORM);
fn fetch_from_internet(command: &str) -> String {
let common_url = format!("https://raw.github.com/tldr-pages/tldr/master/pages/common/{page}.md", page = command);
let platform_url = format!("https://raw.github.com/tldr-pages/tldr/master/pages/{platform}/{page}.md", page = command, platform=PLATFORM);

let client = Client::new();

// Creating an outgoing request.
// Fetch common tldr
let mut res = match client.get(&common_url).send() {
Ok(res) => res,

// Fetch platform tldr if 404
_ => client.get(&platform_url).send().unwrap(),
};

// Read the Response.
let mut the_tldr = String::new();
res.read_to_string(&mut the_tldr).unwrap();

the_tldr
}

fn main() {
let args: Vec<String> = env::args().collect();

if args.len() < 2 {
let error = "Specify an argument!";
println!("{}", format!("{error} \nExample: \n$ tldr ls", error = Red.paint(error).to_string()));
std::process::exit(1);
}

let the_tldr = fetch_from_internet(&args[1]);

render(&the_tldr);
}

Expand Down

0 comments on commit 7c29036

Please sign in to comment.