The CLI frontend to a gist server written in Rust
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cli.rs 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. use clap::ArgMatches;
  2. use std::fs;
  3. use std::io;
  4. use crate::client::{
  5. build_url_from_config, create_snippet, ApiResult, OutgoingSnippet, ServerConfig,
  6. };
  7. fn build_snippet_from_matches(matches: &ArgMatches) -> io::Result<OutgoingSnippet> {
  8. let title = matches.value_of("name").unwrap();
  9. let input = matches.value_of("file");
  10. let filetype = matches.value_of("filetype").unwrap();
  11. let mut rdr: Box<io::Read> = match input {
  12. Some(file) => Box::new(fs::File::open(file)?),
  13. None => Box::new(io::stdin()),
  14. };
  15. let mut body = String::new();
  16. rdr.read_to_string(&mut body)?;
  17. Ok(OutgoingSnippet {
  18. title: title.to_string(),
  19. body,
  20. filetype: filetype.to_string(),
  21. })
  22. }
  23. fn build_server_config_from_matches(matches: &ArgMatches) -> ServerConfig {
  24. let protocol = matches.value_of("protocol").unwrap();
  25. let hostname = matches.value_of("hostname").unwrap();
  26. let port = matches.value_of("port").unwrap();
  27. ServerConfig {
  28. protocol: protocol.to_string(),
  29. hostname: hostname.to_string(),
  30. port: port.parse::<i32>().expect("Port should be numeric"),
  31. }
  32. }
  33. pub fn new_snippet(matches: &ArgMatches) -> io::Result<()> {
  34. let snippet = build_snippet_from_matches(matches)?;
  35. let config = build_server_config_from_matches(matches);
  36. let url = build_url_from_config(&config);
  37. match create_snippet(snippet, &config) {
  38. Ok(resp) => match resp {
  39. ApiResult::IncomingSnippet { uuid, .. } => println!("{}/snippets/{}", url, uuid),
  40. ApiResult::IncomingError { message } => println!("error: {}", message),
  41. },
  42. Err(err) => println!("{}", err),
  43. }
  44. Ok(())
  45. }