Selaa lähdekoodia

Make clipboard support an optional compilation feature

timvisee 7 vuotta sitten
vanhempi
commit
c646cf7938
4 muutettua tiedostoa jossa 31 lisäystä ja 10 poistoa
  1. 4 1
      cli/Cargo.toml
  2. 10 5
      cli/src/action/upload.rs
  3. 13 4
      cli/src/cmd/cmd_upload.rs
  4. 4 0
      cli/src/util.rs

+ 4 - 1
cli/Cargo.toml

@@ -8,8 +8,11 @@ workspace = ".."
 path = "src/main.rs"
 name = "ffsend"
 
+[features]
+default = ["clipboard"]
+
 [dependencies]
 clap = "2.31"
-clipboard = "0.4"
+clipboard = { version = "0.4", optional = true }
 ffsend-api = { version = "*", path = "../api" }
 open = "1"

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

@@ -4,7 +4,9 @@ use ffsend_api::action::upload::Upload as ApiUpload;
 use ffsend_api::reqwest::Client;
 
 use cmd::cmd_upload::CmdUpload;
-use util::{set_clipboard, open_url};
+use util::open_url;
+#[cfg(feature = "clipboard")]
+use util::set_clipboard;
 
 /// A file upload action.
 pub struct Upload<'a> {
@@ -44,10 +46,13 @@ impl<'a> Upload<'a> {
         }
 
         // Copy the URL in the user's clipboard
-        if self.cmd.copy() {
-            // TODO: do not expect, but return an error
-            set_clipboard(url.as_str().to_owned())
-                .expect("failed to put download URL in user clipboard");
+        #[cfg(feature = "clipboard")]
+        {
+            if self.cmd.copy() {
+                // TODO: do not expect, but return an error
+                set_clipboard(url.as_str().to_owned())
+                    .expect("failed to put download URL in user clipboard");
+            }
         }
     }
 }

+ 13 - 4
cli/src/cmd/cmd_upload.rs

@@ -13,7 +13,9 @@ pub struct CmdUpload<'a> {
 impl<'a: 'b, 'b> CmdUpload<'a> {
     /// Build the sub command definition.
     pub fn build<'y, 'z>() -> App<'y, 'z> {
-        SubCommand::with_name("upload")
+        // Build the subcommand
+        #[allow(unused_mut)]
+        let mut cmd = SubCommand::with_name("upload")
             .about("Upload files")
             .visible_alias("u")
             .visible_alias("up")
@@ -31,11 +33,17 @@ impl<'a: 'b, 'b> CmdUpload<'a> {
             .arg(Arg::with_name("open")
                 .long("open")
                 .short("o")
-                .help("Open the share link in your browser"))
-            .arg(Arg::with_name("copy")
+                .help("Open the share link in your browser"));
+
+        // Optional clipboard support
+        #[cfg(feature = "clipboard")] {
+            cmd = cmd.arg(Arg::with_name("copy")
                 .long("copy")
                 .short("c")
-                .help("Copy the share link to your clipboard"))
+                .help("Copy the share link to your clipboard"));
+        }
+
+        cmd
     }
 
     /// Parse CLI arguments, from the given parent command matches.
@@ -85,6 +93,7 @@ impl<'a: 'b, 'b> CmdUpload<'a> {
     }
 
     /// 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")
     }

+ 4 - 0
cli/src/util.rs

@@ -1,10 +1,13 @@
+#[cfg(feature = "clipboard")]
 extern crate clipboard;
 extern crate open;
 
+#[cfg(feature = "clipboard")]
 use std::error::Error;
 use std::io::Error as IoError;
 use std::process::{exit, ExitStatus};
 
+#[cfg(feature = "clipboard")]
 use self::clipboard::{ClipboardContext, ClipboardProvider};
 use ffsend_api::url::Url;
 
@@ -31,6 +34,7 @@ pub fn open_path(path: &str) -> Result<ExitStatus, IoError> {
 }
 
 /// Set the clipboard of the user to the given `content` string.
+#[cfg(feature = "clipboard")]
 pub fn set_clipboard(content: String) -> Result<(), Box<Error>> {
     let mut context: ClipboardContext = ClipboardProvider::new()?;
     context.set_contents(content)