Bladeren bron

Reimplement boolean environment variables, properly this time

timvisee 7 jaren geleden
bovenliggende
commit
70bbf02762
4 gewijzigde bestanden met toevoegingen van 18 en 12 verwijderingen
  1. 0 2
      REQUIREMENTS.md
  2. 5 5
      cli/src/cmd/matcher/main.rs
  3. 4 4
      cli/src/cmd/matcher/upload.rs
  4. 9 1
      cli/src/util.rs

+ 0 - 2
REQUIREMENTS.md

@@ -13,8 +13,6 @@ toolchain.
 ## Environment variables
 - `FFSEND_HOST`: upload host (string)
 - `FFSEND_HISTORY`: history file path (string)
-
-Temporarily disabled:
 - `FFSEND_FORCE`: upload host (present/boolean)
 - `FFSEND_NO_INTERACT`: upload host (present/boolean)
 - `FFSEND_YES`: upload host (present/boolean)

+ 5 - 5
cli/src/cmd/matcher/main.rs

@@ -5,7 +5,7 @@ use clap::ArgMatches;
 
 use super::Matcher;
 #[cfg(feature = "history")]
-use util::{ErrorHintsBuilder, quit_error_msg};
+use util::{env_var_present, ErrorHintsBuilder, quit_error_msg};
 
 /// The main command matcher.
 pub struct MainMatcher<'a> {
@@ -15,17 +15,17 @@ pub struct MainMatcher<'a> {
 impl<'a: 'b, 'b> MainMatcher<'a> {
     /// Check whether to force.
     pub fn force(&self) -> bool {
-        self.matches.is_present("force")
+        self.matches.is_present("force") || env_var_present("FFSEND_FORCE")
     }
 
     /// Check whether to use no-interact mode.
     pub fn no_interact(&self) -> bool {
-        self.matches.is_present("no-interact")
+        self.matches.is_present("no-interact") || env_var_present("FFSEND_NO_INTERACT")
     }
 
     /// Check whether to assume yes.
     pub fn assume_yes(&self) -> bool {
-        self.matches.is_present("yes")
+        self.matches.is_present("yes") || env_var_present("FFSEND_YES")
     }
 
     /// Get the history file to use.
@@ -52,7 +52,7 @@ impl<'a: 'b, 'b> MainMatcher<'a> {
     /// Check whether we are incognito from the file history.
     #[cfg(feature = "history")]
     pub fn incognito(&self) -> bool {
-        self.matches.is_present("incognito")
+        self.matches.is_present("incognito") || env_var_present("FFSEND_INCOGNITO")
     }
 }
 

+ 4 - 4
cli/src/cmd/matcher/upload.rs

@@ -6,7 +6,7 @@ use ffsend_api::url::Url;
 
 use cmd::arg::{ArgDownloadLimit, ArgHost, ArgPassword, CmdArgOption};
 use super::Matcher;
-use util::{ErrorHintsBuilder, quit_error_msg};
+use util::{env_var_present, ErrorHintsBuilder, quit_error_msg};
 
 /// The upload command matcher.
 pub struct UploadMatcher<'a> {
@@ -75,18 +75,18 @@ impl<'a: 'b, 'b> UploadMatcher<'a> {
     /// TODO: infer to use this flag if a directory is selected
     #[cfg(feature = "archive")]
     pub fn archive(&self) -> bool {
-        self.matches.is_present("archive")
+        self.matches.is_present("archive") || env_var_present("FFSEND_ARCHIVE")
     }
 
     /// Check whether to open the file URL in the user's browser.
     pub fn open(&self) -> bool {
-        self.matches.is_present("open")
+        self.matches.is_present("open") || env_var_present("FFSEND_OPEN")
     }
 
     /// Check whether to copy the file URL in the user's clipboard.
     #[cfg(feature = "clipboard")]
     pub fn copy(&self) -> bool {
-        self.matches.is_present("copy")
+        self.matches.is_present("copy") || env_var_present("FFSEND_COPY")
     }
 }
 

+ 9 - 1
cli/src/util.rs

@@ -6,9 +6,10 @@ extern crate fs2;
 extern crate open;
 
 use std::borrow::Borrow;
-use std::env::current_exe;
+use std::env::{current_exe, var_os};
 #[cfg(feature = "clipboard")]
 use std::error::Error as StdError;
+use std::ffi::OsStr;
 use std::fmt::{Debug, Display};
 use std::io::{
     Error as IoError,
@@ -633,3 +634,10 @@ pub fn app_history_file_path_string() -> String {
         .unwrap()
         .to_owned()
 }
+
+/// Check whether an environment variable with the given key is present in the context of the
+/// current process. The environment variable doesn't have to hold any specific value.
+/// Returns `true` if present, `false` if not.
+pub fn env_var_present(key: impl AsRef<OsStr>) -> bool {
+    var_os(key).is_some()
+}