Browse Source

Extract upload action to it's own module

timvisee 7 years ago
parent
commit
c2611049df
2 changed files with 44 additions and 25 deletions
  1. 42 0
      cli/src/action/upload.rs
  2. 2 25
      cli/src/main.rs

+ 42 - 0
cli/src/action/upload.rs

@@ -0,0 +1,42 @@
+use std::path::Path;
+
+use ffsend_api::action::upload::Upload as ApiUpload;
+use ffsend_api::reqwest::Client;
+use open;
+
+use cmd::cmd_upload::CmdUpload;
+
+/// A file upload action.
+pub struct Upload<'a> {
+    cmd: &'a CmdUpload<'a>,
+}
+
+impl<'a> Upload<'a> {
+    /// Construct a new upload action.
+    pub fn new(cmd: &'a CmdUpload<'a>) -> Self {
+        Self {
+            cmd,
+        }
+    }
+
+    /// Invoke the upload action.
+    // TODO: create a trait for this method
+    pub fn invoke(&self) {
+        // Get API action parameters
+        let path = Path::new(self.cmd.file()).to_path_buf();
+        let host = self.cmd.host();
+
+        // Create a reqwest client
+        let client = Client::new();
+
+        // Execute an upload action
+        // TODO: do not unwrap, but return an error
+        let file = ApiUpload::new(host, path).invoke(&client).unwrap();
+
+        // Open the URL in the browser
+        let url = file.download_url();
+        println!("Download URL: {}", url);
+        // TODO: do not expect, but return an error
+        open::that(url).expect("failed to open URL");
+    }
+}

+ 2 - 25
cli/src/main.rs

@@ -6,12 +6,8 @@ mod app;
 mod cmd;
 mod util;
 
-use std::path::Path;
-
+use action::upload::Upload;
 use cmd::Handler;
-use cmd::cmd_upload::CmdUpload;
-use ffsend_api::action::upload::Upload;
-use ffsend_api::reqwest::Client;
 
 /// Application entrypoint.
 fn main() {
@@ -29,7 +25,7 @@ fn main() {
 fn invoke_action(handler: &Handler) {
     // Match the upload command
     if let Some(cmd) = handler.upload() {
-        return action_upload(&cmd);
+        return Upload::new(&cmd).invoke();
     }
 
     // No subcommand was selected, show general help
@@ -37,22 +33,3 @@ fn invoke_action(handler: &Handler) {
         .print_help()
         .expect("failed to print command help");
 }
-
-/// The upload action.
-fn action_upload(cmd_upload: &CmdUpload) {
-    // // Get the path and host
-    let path = Path::new(cmd_upload.file()).to_path_buf();
-    let host = cmd_upload.host();
-
-    // Create a reqwest client
-    let client = Client::new();
-
-    // Create an upload action
-    let upload = Upload::new(host, path);
-    let file = upload.invoke(&client).unwrap();
-
-    // Open the URL in the browser
-    let url = file.download_url();
-    println!("Download URL: {}", url);
-    open::that(url).expect("failed to open URL");
-}