Ver Fonte

Create history tool, for easier static history management

timvisee há 7 anos atrás
pai
commit
7526bdb5a3

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

@@ -1,5 +1,4 @@
 use clap::ArgMatches;
-use failure::Fail;
 use ffsend_api::action::delete::{
     Error as DeleteError,
     Delete as ApiDelete,
@@ -16,8 +15,8 @@ use cmd::matcher::{
     main::MainMatcher,
 };
 use error::ActionError;
-use history::History;
-use util::{ensure_owner_token, print_error, print_success};
+use history_tool;
+use util::{ensure_owner_token, print_success};
 
 /// A file delete action.
 pub struct Delete<'a> {
@@ -56,14 +55,7 @@ impl<'a> Delete<'a> {
 
         // Remove the file from the history manager
         // TODO: also remove if it was expired
-        if let Err(err) = History::load_remove_save(
-            matcher_main.history(),
-            &file,
-        ) {
-            print_error(err.context(
-                "Failed to remove file from local history, ignoring",
-            ));
-        }
+        history_tool::remove(&matcher_main, &file);
 
         // Print a success message
         print_success("File deleted");

+ 2 - 11
cli/src/action/exists.rs

@@ -1,5 +1,4 @@
 use clap::ArgMatches;
-use failure::Fail;
 use ffsend_api::action::exists::{
     Error as ExistsError,
     Exists as ApiExists,
@@ -16,8 +15,7 @@ use cmd::matcher::{
     main::MainMatcher,
 };
 use error::ActionError;
-use history::History;
-use util::print_error;
+use history_tool;
 
 /// A file exists action.
 pub struct Exists<'a> {
@@ -62,14 +60,7 @@ impl<'a> Exists<'a> {
         // Remove the file from the history manager if it doesn't exist
         // TODO: add if it does exist
         if !exists {
-            if let Err(err) = History::load_remove_save(
-                matcher_main.history(),
-                &file,
-            ) {
-                print_error(err.context(
-                    "Failed to remove file from local history, ignoring",
-                ));
-            }
+            history_tool::remove(&matcher_main, &file);
         }
 
         Ok(())

+ 2 - 9
cli/src/action/info.rs

@@ -20,7 +20,7 @@ use cmd::matcher::{
     info::InfoMatcher,
     main::MainMatcher,
 };
-use history::History;
+use history_tool;
 use util::{ensure_owner_token, ensure_password, print_error};
 
 /// A file info action.
@@ -60,14 +60,7 @@ impl<'a> Info<'a> {
         let exists = ApiExists::new(&file).invoke(&client)?;
         if !exists.exists() {
             // Remove the file from the history manager if it doesn't exist
-            if let Err(err) = History::load_remove_save(
-                matcher_main.history(),
-                &file,
-            ) {
-                print_error(err.context(
-                    "Failed to remove file from local history, ignoring",
-                ));
-            }
+            history_tool::remove(&matcher_main, &file);
 
             return Err(Error::Expired);
         }

+ 2 - 9
cli/src/action/upload.rs

@@ -11,7 +11,7 @@ use ffsend_api::reqwest::Client;
 
 use cmd::matcher::{Matcher, MainMatcher, UploadMatcher};
 use error::ActionError;
-use history::History;
+use history_tool;
 use progress::ProgressBar;
 use util::{
     ErrorHintsBuilder,
@@ -126,14 +126,7 @@ impl<'a> Upload<'a> {
         println!("Owner token: {}", file.owner_token().unwrap());
 
         // Add the file to the history manager
-        if let Err(err) = History::load_add_save(
-            matcher_main.history(),
-            file.clone(),
-        ) {
-            print_error(err.context(
-                "Failed to add file to local history, ignoring",
-            ));
-        }
+        history_tool::add(&matcher_main, file.clone());
 
         // Open the URL in the browser
         if matcher_upload.open() {

+ 0 - 19
cli/src/history.rs

@@ -109,25 +109,6 @@ impl History {
         Ok(())
     }
 
-    /// Load the history from the given path, add the given file, and save it
-    /// again.
-    /// If there is no file at the given path, new history will be created.
-    pub fn load_add_save(path: PathBuf, file: RemoteFile) -> Result<(), Error> {
-        let mut history = Self::load_or_new(path)?;
-        history.add(file);
-        history.save().map_err(|err| err.into())
-    }
-
-    /// Load the history from the given path, remove the given file by it's
-    /// ID, and save it again.
-    /// True is returned if any file was removed.
-    pub fn load_remove_save(path: PathBuf, file: &RemoteFile) -> Result<bool, Error> {
-        let mut history = Self::load_or_new(path)?;
-        let removed = history.remove(file);
-        history.save()?;
-        Ok(removed)
-    }
-
     /// Add the given remote file to the history.
     pub fn add(&mut self, file: RemoteFile) {
         self.files.push(file);

+ 58 - 0
cli/src/history_tool.rs

@@ -0,0 +1,58 @@
+use failure::Fail;
+use ffsend_api::file::remote_file::RemoteFile;
+
+use cmd::matcher::MainMatcher;
+use history::{
+    Error as HistoryError,
+    History,
+};
+use util::print_error;
+
+/// Load the history from the given path, add the given file, and save it
+/// again.
+/// If there is no file at the given path, new history will be created.
+fn add_error(matcher_main: &MainMatcher, file: RemoteFile)
+    -> Result<(), HistoryError>
+{
+    let mut history = History::load_or_new(matcher_main.history())?;
+    history.add(file);
+    history.save().map_err(|err| err.into())
+}
+
+/// Load the history from the given path, add the given file, and save it
+/// again.
+/// If there is no file at the given path, new history will be created.
+/// If an error occurred, the error is printed and ignored.
+pub fn add(matcher_main: &MainMatcher, file: RemoteFile) {
+    if let Err(err) = add_error(matcher_main, file) {
+        print_error(err.context(
+            "Failed to add file to local history, ignoring",
+        ));
+    }
+}
+
+/// Load the history from the given path, remove the given file by it's
+/// ID, and save it again.
+/// True is returned if any file was removed.
+fn remove_error(matcher_main: &MainMatcher, file: &RemoteFile)
+    -> Result<bool, HistoryError>
+{
+    let mut history = History::load_or_new(matcher_main.history())?;
+    let removed = history.remove(file);
+    history.save()?;
+    Ok(removed)
+}
+
+/// Load the history from the given path, remove the given file by it's
+/// ID, and save it again.
+/// True is returned if any file was removed.
+pub fn remove(matcher_main: &MainMatcher, file: &RemoteFile) -> bool {
+    if let Err(err) = remove_error(matcher_main, file) {
+        print_error(err.context(
+            "Failed to remove file from local history, ignoring",
+        ));
+        false
+    } else {
+        true
+    }
+}

+ 1 - 0
cli/src/main.rs

@@ -17,6 +17,7 @@ mod action;
 mod cmd;
 mod error;
 mod history;
+mod history_tool;
 mod host;
 mod progress;
 mod util;