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.6KB

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