Browse Source

Allow configuring server to post to

master
Dylan Baker 5 years ago
parent
commit
8b0a20d6c5
3 changed files with 75 additions and 24 deletions
  1. 30
    7
      src/cli.rs
  2. 14
    12
      src/client.rs
  3. 31
    5
      src/main.rs

+ 30
- 7
src/cli.rs View File

@@ -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
     }

+ 14
- 12
src/client.rs View File

@@ -6,7 +6,7 @@ pub struct OutgoingGist {
6 6
     pub title: String,
7 7
     pub body: String,
8 8
     pub formatted_body: String,
9
-    pub filetype: Option<String>,
9
+    pub filetype: String,
10 10
 }
11 11
 
12 12
 #[derive(Serialize, Deserialize, Debug)]
@@ -17,21 +17,23 @@ pub struct IncomingGist {
17 17
     pub created_at: NaiveDateTime,
18 18
 }
19 19
 
20
+#[derive(Debug)]
21
+pub struct ServerConfig {
22
+    pub hostname: String,
23
+    pub protocol: String,
24
+    pub port: i32,
25
+}
26
+
20 27
 pub fn create_gist(
21
-    title: &str,
22
-    body: &str,
23
-    filetype: Option<&str>,
28
+    gist: OutgoingGist,
29
+    config: &ServerConfig,
24 30
 ) -> Result<IncomingGist, Box<std::error::Error>> {
25 31
     let client = reqwest::Client::new();
26
-    let gist = OutgoingGist {
27
-        title: title.to_string(),
28
-        body: body.to_string(),
29
-        formatted_body: String::from(""),
30
-        filetype: filetype.map(|ft| ft.to_string()),
31
-    };
32
-
33 32
     let resp: IncomingGist = client
34
-        .post("http://localhost:8000/api/gists")
33
+        .post(&format!(
34
+            "{}://{}:{}/api/gists",
35
+            config.protocol, config.hostname, config.port,
36
+        ))
35 37
         .json(&gist)
36 38
         .send()?
37 39
         .json()?;

+ 31
- 5
src/main.rs View File

@@ -17,12 +17,12 @@ fn main() -> io::Result<()> {
17 17
             SubCommand::with_name("new")
18 18
                 .about("Creates a new gist")
19 19
                 .arg(
20
-                    Arg::with_name("title")
21
-                        .long("title")
22
-                        .short("t")
20
+                    Arg::with_name("name")
21
+                        .long("name")
22
+                        .short("n")
23 23
                         .takes_value(true)
24 24
                         .required(true)
25
-                        .help("The title of the gist"),
25
+                        .help("The name of the gist"),
26 26
                 )
27 27
                 .arg(
28 28
                     Arg::with_name("file")
@@ -34,9 +34,35 @@ fn main() -> io::Result<()> {
34 34
                 .arg(
35 35
                     Arg::with_name("filetype")
36 36
                         .long("filetype")
37
-                        .short("p")
37
+                        .short("t")
38 38
                         .takes_value(true)
39
+                        .default_value("")
39 40
                         .help("The filetype to use for syntax highlighting"),
41
+                )
42
+                .arg(
43
+                    Arg::with_name("hostname")
44
+                        .long("hostname")
45
+                        .short("s")
46
+                        .takes_value(true)
47
+                        .default_value("bngl.ws")
48
+                        .help("The hostname of the server to post your gist to"),
49
+                )
50
+                .arg(
51
+                    Arg::with_name("protocol")
52
+                        .long("protocol")
53
+                        .short("c")
54
+                        .takes_value(true)
55
+                        .possible_values(&["http", "https"])
56
+                        .default_value("https")
57
+                        .help("The transport protocol to use"),
58
+                )
59
+                .arg(
60
+                    Arg::with_name("port")
61
+                        .long("port")
62
+                        .short("p")
63
+                        .takes_value(true)
64
+                        .default_value("80")
65
+                        .help("The transport protocol to use"),
40 66
                 ),
41 67
         )
42 68
         .get_matches();

Loading…
Cancel
Save