|
@@ -2,12 +2,12 @@ use clap::ArgMatches;
|
2
|
2
|
use std::fs;
|
3
|
3
|
use std::io;
|
4
|
4
|
|
5
|
|
-use crate::client::create_gist;
|
|
5
|
+use crate::client::{create_gist, OutgoingGist, ServerConfig};
|
6
|
6
|
|
7
|
|
-pub fn new_gist(matches: &ArgMatches) -> io::Result<()> {
|
8
|
|
- let title = matches.value_of("title").unwrap();
|
|
7
|
+fn get_gist_from_matches(matches: &ArgMatches) -> io::Result<OutgoingGist> {
|
|
8
|
+ let title = matches.value_of("name").unwrap();
|
9
|
9
|
let input = matches.value_of("file");
|
10
|
|
- let filetype = matches.value_of("filetype");
|
|
10
|
+ let filetype = matches.value_of("filetype").unwrap();
|
11
|
11
|
|
12
|
12
|
let mut rdr: Box<io::Read> = match input {
|
13
|
13
|
Some(file) => Box::new(fs::File::open(file)?),
|
|
@@ -17,10 +17,33 @@ pub fn new_gist(matches: &ArgMatches) -> io::Result<()> {
|
17
|
17
|
let mut body = String::new();
|
18
|
18
|
rdr.read_to_string(&mut body)?;
|
19
|
19
|
|
20
|
|
- match create_gist(title, &body, filetype) {
|
|
20
|
+ Ok(OutgoingGist {
|
|
21
|
+ title: title.to_string(),
|
|
22
|
+ body,
|
|
23
|
+ filetype: filetype.to_string(),
|
|
24
|
+ formatted_body: String::from(""),
|
|
25
|
+ })
|
|
26
|
+}
|
|
27
|
+
|
|
28
|
+fn get_server_config_from_matches(matches: &ArgMatches) -> ServerConfig {
|
|
29
|
+ let protocol = matches.value_of("protocol").unwrap();
|
|
30
|
+ let hostname = matches.value_of("hostname").unwrap();
|
|
31
|
+ let port = matches.value_of("port").unwrap();
|
|
32
|
+ ServerConfig {
|
|
33
|
+ protocol: protocol.to_string(),
|
|
34
|
+ hostname: hostname.to_string(),
|
|
35
|
+ port: port.parse::<i32>().expect("Port should be numeric"),
|
|
36
|
+ }
|
|
37
|
+}
|
|
38
|
+
|
|
39
|
+pub fn new_gist(matches: &ArgMatches) -> io::Result<()> {
|
|
40
|
+ let gist = get_gist_from_matches(matches)?;
|
|
41
|
+ let config = get_server_config_from_matches(matches);
|
|
42
|
+
|
|
43
|
+ match create_gist(gist, &config) {
|
21
|
44
|
Ok(resp) => println!(
|
22
|
|
- "Success! Your new gist is available at http://localhost:8000/gists/{}",
|
23
|
|
- resp.id
|
|
45
|
+ "Success! Your new gist is available at {}://{}:{}/gists/{}",
|
|
46
|
+ config.protocol, config.hostname, config.port, resp.id
|
24
|
47
|
),
|
25
|
48
|
Err(err) => println!("{}", err),
|
26
|
49
|
}
|