Ver Fonte

Use proper history file path, allow customizing it with CLI flag

timvisee há 7 anos atrás
pai
commit
3f6c4a558b
8 ficheiros alterados com 116 adições e 6 exclusões
  1. 40 0
      Cargo.lock
  2. 2 0
      cli/Cargo.toml
  3. 5 4
      cli/src/action/upload.rs
  4. 27 0
      cli/src/cmd/handler.rs
  5. 23 0
      cli/src/cmd/matcher/main.rs
  6. 4 1
      cli/src/history.rs
  7. 2 0
      cli/src/main.rs
  8. 13 1
      cli/src/util.rs

+ 40 - 0
Cargo.lock

@@ -19,6 +19,17 @@ dependencies = [
  "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "app_dirs2"
+version = "2.0.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "ole32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "shell32-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
+ "xdg 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "arrayref"
 version = "0.3.4"
@@ -360,6 +371,7 @@ dependencies = [
 name = "ffsend-cli"
 version = "0.0.1"
 dependencies = [
+ "app_dirs2 2.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
  "clap 2.31.2 (registry+https://github.com/rust-lang/crates.io-index)",
  "clipboard 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
  "colored 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -367,6 +379,7 @@ dependencies = [
  "failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "failure_derive 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "ffsend-api 0.0.1",
+ "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "open 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "pbr 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "rpassword 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -725,6 +738,15 @@ dependencies = [
  "objc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "ole32-sys"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "open"
 version = "1.2.1"
@@ -1052,6 +1074,15 @@ dependencies = [
  "fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "shell32-sys"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "siphasher"
 version = "0.2.2"
@@ -1492,10 +1523,16 @@ dependencies = [
  "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "xdg"
+version = "2.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
 [metadata]
 "checksum adler32 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6cbd0b9af8587c72beadc9f72d35b9fbb070982c9e6203e46e93f10df25f8f45"
 "checksum aho-corasick 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d6531d44de723825aa81398a6415283229725a00fa30713812ab9323faa82fc4"
 "checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
+"checksum app_dirs2 2.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0aa02944d8a100b79057d1619032b1ad39de5eed6567cdeccbd53908b326e082"
 "checksum arrayref 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "0fd1479b7c29641adbd35ff3b5c293922d696a92f25c8c975da3e0acbc87258f"
 "checksum arrayvec 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)" = "a1e964f9e24d588183fcb43503abda40d288c8657dfc27311516ce2f05675aef"
 "checksum atty 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "af80143d6f7608d746df1520709e5d141c96f240b0e62b0aa41bdfb53374d9d4"
@@ -1577,6 +1614,7 @@ dependencies = [
 "checksum objc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "877f30f37acef6749b1841cceab289707f211aecfc756553cd63976190e6cc2e"
 "checksum objc-foundation 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1add1b659e36c9607c7aab864a76c7a4c2760cd0cd2e120f3fb8b952c7e22bf9"
 "checksum objc_id 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e4730aa1c64d722db45f7ccc4113a3e2c465d018de6db4d3e7dfe031e8c8a297"
+"checksum ole32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5d2c49021782e5233cd243168edfa8037574afed4eba4bbaf538b3d8d1789d8c"
 "checksum open 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c281318d992e4432cfa799969467003d05921582a7489a8325e37f8a450d5113"
 "checksum openssl 0.10.6 (registry+https://github.com/rust-lang/crates.io-index)" = "63246f69962e8d5ef865f82a65241d6483c8a2905a1801e2f7feb5d187d51320"
 "checksum openssl 0.9.24 (registry+https://github.com/rust-lang/crates.io-index)" = "a3605c298474a3aa69de92d21139fb5e2a81688d308262359d85cdd0d12a7985"
@@ -1614,6 +1652,7 @@ dependencies = [
 "checksum serde_json 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)" = "5c508584d9913df116b91505eec55610a2f5b16e9ed793c46e4d0152872b3e74"
 "checksum serde_urlencoded 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ce0fd303af908732989354c6f02e05e2e6d597152870f2c6990efb0577137480"
 "checksum sha2 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7daca11f2fdb8559c4f6c588386bed5e2ad4b6605c1442935a7f08144a918688"
+"checksum shell32-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9ee04b46101f57121c9da2b151988283b6beb79b34f5bb29a58ee48cb695122c"
 "checksum siphasher 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0df90a788073e8d0235a67e50441d47db7c8ad9debd91cbf43736a2a92d36537"
 "checksum slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "17b4fcaed89ab08ef143da37bc52adbcc04d4a69014f4c1208d6b51f0c47bc23"
 "checksum slab 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fdeff4cd9ecff59ec7e3744cbca73dfe5ac35c2aedb2cfba8a1c715a18912e9d"
@@ -1668,3 +1707,4 @@ dependencies = [
 "checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e"
 "checksum x11-clipboard 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2e7374c7699210cca7084ca61d57e09640fc744d1391808cb9ae2fe4ca9bd1df"
 "checksum xcb 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5e917a3f24142e9ff8be2414e36c649d47d6cc2ba81f16201cdef96e533e02de"
+"checksum xdg 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a66b7c2281ebde13cf4391d70d4c7e5946c3c25e72a7b859ca8f677dcd0b0c61"

+ 2 - 0
cli/Cargo.toml

@@ -16,6 +16,7 @@ default = ["clipboard"]
 no-color = ["colored/no-color"]
 
 [dependencies]
+app_dirs2 = "2.0"
 clap = "2.31"
 clipboard = { version = "0.4", optional = true }
 colored = "1.6"
@@ -23,6 +24,7 @@ derive_builder = "0.5"
 failure = "0.1"
 failure_derive = "0.1"
 ffsend-api = { version = "*", path = "../api" }
+lazy_static = "1.0"
 open = "1"
 pbr = "1"
 rpassword = "2.0"

+ 5 - 4
cli/src/action/upload.rs

@@ -1,5 +1,5 @@
 use std::fs::File;
-use std::path::{Path, PathBuf};
+use std::path::Path;
 use std::sync::{Arc, Mutex};
 
 use clap::ArgMatches;
@@ -126,9 +126,10 @@ impl<'a> Upload<'a> {
         println!("Owner token: {}", file.owner_token().unwrap());
 
         // Add the file to the history manager
-        // TODO: specify the proper path here
-        let history_path = PathBuf::from("./history.toml");
-        if let Err(err) = History::load_add_save(history_path, file.clone()) {
+        if let Err(err) = History::load_add_save(
+            matcher_main.history(),
+            file.clone(),
+        ) {
             print_error(err.context("Failed to add file to history, ignoring"));
         }
 

+ 27 - 0
cli/src/cmd/handler.rs

@@ -1,4 +1,7 @@
+extern crate app_dirs2;
+
 use clap::{App, AppSettings, Arg, ArgMatches};
+use self::app_dirs2::{AppDataType, AppInfo, get_app_dir};
 
 use super::matcher::{
     DeleteMatcher,
@@ -20,6 +23,23 @@ use super::cmd::{
     CmdUpload,
 };
 
+lazy_static! {
+    /// The default history file
+    static ref DEFAULT_HISTORY_FILE: String =
+        get_app_dir(
+            AppDataType::UserCache,
+            &AppInfo {
+                name: crate_name!(),
+                author: crate_name!(),
+            },
+            "history.toml",
+        )
+        .unwrap()
+        .to_str()
+        .unwrap()
+        .to_owned();
+}
+
 /// CLI argument handler.
 pub struct Handler<'a> {
     /// The CLI matches.
@@ -54,6 +74,13 @@ impl<'a: 'b, 'b> Handler<'a> {
                 .visible_alias("assume-yes")
                 .global(true)
                 .help("Assume yes for prompts"))
+            .arg(Arg::with_name("history")
+                .long("history")
+                .short("H")
+                .value_name("FILE")
+                .global(true)
+                .help("History file to use")
+                .default_value(&DEFAULT_HISTORY_FILE))
             .subcommand(CmdDelete::build())
             .subcommand(CmdDownload::build().display_order(2))
             .subcommand(CmdExists::build())

+ 23 - 0
cli/src/cmd/matcher/main.rs

@@ -1,6 +1,9 @@
+use std::path::PathBuf;
+
 use clap::ArgMatches;
 
 use super::Matcher;
+use util::{ErrorHintsBuilder, quit_error_msg};
 
 /// The main command matcher.
 pub struct MainMatcher<'a> {
@@ -22,6 +25,26 @@ impl<'a: 'b, 'b> MainMatcher<'a> {
     pub fn assume_yes(&self) -> bool {
         self.matches.is_present("yes")
     }
+
+    /// Get the history file to use.
+    pub fn history(&self) -> PathBuf {
+        // Get the path
+        let path = self.matches.value_of("history")
+            .map(|path| PathBuf::from(path));
+
+        // Ensure the path is correct
+        match path {
+            Some(path) => path,
+            None => quit_error_msg(
+                "History file path not set",
+                ErrorHintsBuilder::default()
+                    .history(true)
+                    .verbose(false)
+                    .build()
+                    .unwrap(),
+            ),
+        }
+    }
 }
 
 impl<'a> Matcher<'a> for MainMatcher<'a> {

+ 4 - 1
cli/src/history.rs

@@ -3,6 +3,7 @@ extern crate toml;
 use std::io::Error as IoError;
 use std::path::PathBuf;
 
+use failure::Fail;
 use ffsend_api::file::remote_file::RemoteFile;
 use self::toml::de::Error as DeError;
 use self::toml::ser::Error as SerError;
@@ -117,7 +118,9 @@ impl Drop for History {
         if self.autosave.is_some() && self.changed {
             // Save and report errors
             if let Err(err) = self.save() {
-                print_error(err);
+                print_error(
+                    err.context("Failed to auto save history, ignoring"),
+                );
             }
         }
     }

+ 2 - 0
cli/src/main.rs

@@ -6,6 +6,8 @@ extern crate failure;
 #[macro_use]
 extern crate failure_derive;
 extern crate ffsend_api;
+#[macro_use]
+extern crate lazy_static;
 extern crate rpassword;
 extern crate serde;
 #[macro_use]

+ 13 - 1
cli/src/util.rs

@@ -95,6 +95,9 @@ pub struct ErrorHints {
     /// Show about the owner option.
     owner: bool,
 
+    /// Show about the history flag.
+    history: bool,
+
     /// Show about the force flag.
     force: bool,
 
@@ -108,7 +111,12 @@ pub struct ErrorHints {
 impl ErrorHints {
     /// Check whether any hint should be printed.
     pub fn any(&self) -> bool {
-        self.password || self.owner || self.force || self.verbose || self.help
+        self.password
+            || self.owner
+            || self.history
+            || self.force
+            || self.verbose
+            || self.help
     }
 
     /// Print the error hints.
@@ -127,6 +135,9 @@ impl ErrorHints {
         if self.owner {
             eprintln!("Use '{}' to specify an owner token", highlight("--owner <TOKEN>"));
         }
+        if self.history {
+            eprintln!("Use '{}' to specify a history file", highlight("--history <FILE>"));
+        }
         if self.force {
             eprintln!("Use '{}' to force", highlight("--force"));
         }
@@ -147,6 +158,7 @@ impl Default for ErrorHints {
         ErrorHints {
             password: false,
             owner: false,
+            history: false,
             force: false,
             verbose: true,
             help: true,