1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- use clap::ArgMatches;
- use std::fs;
- use std::io;
-
- use crate::client::{build_url_from_config, create_snippet, OutgoingSnippet, ServerConfig};
-
- fn get_snippet_from_matches(matches: &ArgMatches) -> io::Result<OutgoingSnippet> {
- 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(OutgoingSnippet {
- 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_snippet(matches: &ArgMatches) -> io::Result<()> {
- let snippet = get_snippet_from_matches(matches)?;
- let config = get_server_config_from_matches(matches);
- let url = build_url_from_config(&config);
-
- match create_snippet(snippet, &config) {
- Ok(resp) => println!(
- "Success! Your new snippet is available at {}/snippets/{}",
- url, resp.id
- ),
- Err(err) => println!("{}", err),
- }
-
- Ok(())
- }
|