Sfoglia il codice sorgente

Create utility for ensuring a password is set (with prompt)

timvisee 7 anni fa
parent
commit
804455e13d
4 ha cambiato i file con 30 aggiunte e 22 eliminazioni
  1. 1 0
      IDEAS.md
  2. 3 11
      cli/src/action/download.rs
  3. 3 11
      cli/src/action/info.rs
  4. 23 0
      cli/src/util.rs

+ 1 - 0
IDEAS.md

@@ -1,4 +1,5 @@
 # Ideas
+- Prompt for owner tokens
 - allow creating non existent directories with the `-f` flag 
 - only allow file extension renaming on upload with `-f` flag
 - no interact flag

+ 3 - 11
cli/src/action/download.rs

@@ -17,7 +17,7 @@ use cmd::matcher::{
     download::DownloadMatcher,
 };
 use progress::ProgressBar;
-use util::prompt_password;
+use util::ensure_password;
 
 /// A file download action.
 pub struct Download<'a> {
@@ -58,16 +58,8 @@ impl<'a> Download<'a> {
             return Err(Error::Expired);
         }
 
-        // Check whether the file requires a password
-        if exists.has_password() != password.is_some() {
-            if exists.has_password() {
-                println!("This file is protected with a password.");
-                password = Some(prompt_password());
-            } else {
-                println!("Ignoring password, it is not required");
-                password = None;
-            }
-        }
+        // Ensure a password is set when required
+        ensure_password(&mut password, exists.has_password());
 
         // Create a progress bar reporter
         let bar = Arc::new(Mutex::new(ProgressBar::new_download()));

+ 3 - 11
cli/src/action/info.rs

@@ -19,7 +19,7 @@ use cmd::matcher::{
     Matcher,
     info::InfoMatcher,
 };
-use util::{print_error, prompt_password};
+use util::{print_error, ensure_password};
 
 /// A file info action.
 pub struct Info<'a> {
@@ -58,16 +58,8 @@ impl<'a> Info<'a> {
             return Err(Error::Expired);
         }
 
-        // Check whether the file requires a password
-        if exists.has_password() != password.is_some() {
-            if exists.has_password() {
-                println!("This file is protected with a password.");
-                password = Some(prompt_password());
-            } else {
-                println!("Ignoring password, it is not required");
-                password = None;
-            }
-        }
+        // Ensure a password is set when required
+        ensure_password(&mut password, exists.has_password());
 
         // Fetch both file info and metadata
         let info = ApiInfo::new(&file, None).invoke(&client)?;

+ 23 - 0
cli/src/util.rs

@@ -93,3 +93,26 @@ pub fn prompt_password() -> String {
         )),
     }
 }
+
+/// Get a password if required.
+/// This method will ensure a password is set (or not) in the given `password`
+/// parameter, as defined by `needs`.
+///
+/// This method will prompt the user for a password, if one is required but
+/// wasn't set. An ignore message will be shown if it was not required while it
+/// was set.
+pub fn ensure_password(password: &mut Option<String>, needs: bool) {
+    // Return if we're fine
+    if password.is_some() == needs {
+        return;
+    }
+
+    // Ask for a password, or reset it
+    if needs {
+        println!("This file is protected with a password.");
+        *password = Some(prompt_password());
+    } else {
+        println!("Ignoring password, it is not required");
+        *password = None;
+    }
+}