12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- use clap::ArgMatches;
- use std::fs;
- use std::io;
-
- use crate::client::{create_gist, OutgoingGist, ServerConfig};
-
- fn get_gist_from_matches(matches: &ArgMatches) -> io::Result<OutgoingGist> {
- let title = matches.value_of("name").unwrap();
- let input = matches.value_of("file");
- let filetype = matches.value_of("filetype").unwrap();
-
- let mut rdr: Box<io::Read> = match input {
- Some(file) => Box::new(fs::File::open(file)?),
- None => Box::new(io::stdin()),
- };
-
- let mut body = String::new();
- rdr.read_to_string(&mut body)?;
-
- Ok(OutgoingGist {
- title: title.to_string(),
- body,
- filetype: filetype.to_string(),
- formatted_body: String::from(""),
- })
- }
-
- fn get_server_config_from_matches(matches: &ArgMatches) -> ServerConfig {
- let protocol = matches.value_of("protocol").unwrap();
- let hostname = matches.value_of("hostname").unwrap();
- let port = matches.value_of("port").unwrap();
- ServerConfig {
- protocol: protocol.to_string(),
- hostname: hostname.to_string(),
- port: port.parse::<i32>().expect("Port should be numeric"),
- }
- }
-
- pub fn new_gist(matches: &ArgMatches) -> io::Result<()> {
- let gist = get_gist_from_matches(matches)?;
- let config = get_server_config_from_matches(matches);
-
- match create_gist(gist, &config) {
- Ok(resp) => println!(
- "Success! Your new gist is available at {}://{}:{}/gists/{}",
- config.protocol, config.hostname, config.port, resp.id
- ),
- Err(err) => println!("{}", err),
- }
-
- Ok(())
- }
|