The CLI frontend to a gist server written in Rust
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

cli.rs 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. formatted_body: String::from(""),
  22. })
  23. }
  24. fn build_server_config_from_matches(matches: &ArgMatches) -> ServerConfig {
  25. let protocol = matches.value_of("protocol").unwrap();
  26. let hostname = matches.value_of("hostname").unwrap();
  27. let port = matches.value_of("port").unwrap();
  28. ServerConfig {
  29. protocol: protocol.to_string(),
  30. hostname: hostname.to_string(),
  31. port: port.parse::<i32>().expect("Port should be numeric"),
  32. }
  33. }
  34. pub fn new_snippet(matches: &ArgMatches) -> io::Result<()> {
  35. let snippet = build_snippet_from_matches(matches)?;
  36. let config = build_server_config_from_matches(matches);
  37. let url = build_url_from_config(&config);
  38. match create_snippet(snippet, &config) {
  39. Ok(resp) => match resp {
  40. ApiResult::IncomingSnippet { id, .. } => println!(
  41. "Success! Your new snippet is available at {}/snippets/{}",
  42. url, id
  43. ),
  44. ApiResult::IncomingError { message } => println!("error: {}", message),
  45. },
  46. Err(err) => println!("{}", err),
  47. }
  48. Ok(())
  49. }