Browse Source

Add option to copy ffsend download command instead of share URL using -C

timvisee 6 years ago
parent
commit
b697bc3231
6 changed files with 66 additions and 28 deletions
  1. 13 12
      README.md
  2. 12 5
      src/action/upload.rs
  3. 1 1
      src/cmd/matcher/mod.rs
  4. 23 3
      src/cmd/matcher/upload.rs
  5. 16 6
      src/cmd/subcmd/upload.rs
  6. 1 1
      src/util.rs

+ 13 - 12
README.md

@@ -302,18 +302,19 @@ These environment variables may be used to toggle a flag, simply by making them
 available. The actual value of these variables is ignored, and variables may be
 available. The actual value of these variables is ignored, and variables may be
 empty.
 empty.
 
 
-| Variable             | CLI flag        | Description                       |
-| :------------------- | :-------------: | :-------------------------------- |
-| `FFSEND_FORCE`       | `--force`       | Force operations                  |
-| `FFSEND_NO_INTERACT` | `--no-interact` | No interaction for prompts        |
-| `FFSEND_YES`         | `--yes`         | Assume yes for prompts            |
-| `FFSEND_INCOGNITO`   | `--incognito`   | Incognito mode, don't use history |
-| `FFSEND_OPEN`        | `--open`        | Open share link of uploaded file  |
-| `FFSEND_ARCHIVE`     | `--archive`     | Archive files uploaded            |
-| `FFSEND_EXTRACT`     | `--extract`     | Extract files downloaded          |
-| `FFSEND_COPY`        | `--copy`        | Copy share link to clipboard      |
-| `FFSEND_QUIET`       | `--quiet`       | Log quiet information             |
-| `FFSEND_VERBOSE`     | `--verbose`     | Log verbose information           |
+| Variable             | CLI flag        | Description                        |
+| :------------------- | :-------------: | :--------------------------------- |
+| `FFSEND_FORCE`       | `--force`       | Force operations                   |
+| `FFSEND_NO_INTERACT` | `--no-interact` | No interaction for prompts         |
+| `FFSEND_YES`         | `--yes`         | Assume yes for prompts             |
+| `FFSEND_INCOGNITO`   | `--incognito`   | Incognito mode, don't use history  |
+| `FFSEND_OPEN`        | `--open`        | Open share link of uploaded file   |
+| `FFSEND_ARCHIVE`     | `--archive`     | Archive files uploaded             |
+| `FFSEND_EXTRACT`     | `--extract`     | Extract files downloaded           |
+| `FFSEND_COPY`        | `--copy`        | Copy share link to clipboard       |
+| `FFSEND_COPY_CMD`    | `--copy-cmd`    | Copy download command to clipboard |
+| `FFSEND_QUIET`       | `--quiet`       | Log quiet information              |
+| `FFSEND_VERBOSE`     | `--verbose`     | Log verbose information            |
 
 
 At this time, no configuration or _dotfile_ file support is available.
 At this time, no configuration or _dotfile_ file support is available.
 This will be something added in a later release.
 This will be something added in a later release.

+ 12 - 5
src/action/upload.rs

@@ -19,15 +19,15 @@ use super::select_api_version;
 #[cfg(feature = "archive")]
 #[cfg(feature = "archive")]
 use crate::archive::archiver::Archiver;
 use crate::archive::archiver::Archiver;
 use crate::client::create_transfer_client;
 use crate::client::create_transfer_client;
-use crate::cmd::matcher::{MainMatcher, Matcher, UploadMatcher};
+use crate::cmd::matcher::{CopyMode, MainMatcher, Matcher, UploadMatcher};
 #[cfg(feature = "history")]
 #[cfg(feature = "history")]
 use crate::history_tool;
 use crate::history_tool;
 use crate::progress::ProgressBar;
 use crate::progress::ProgressBar;
 #[cfg(feature = "clipboard")]
 #[cfg(feature = "clipboard")]
 use crate::util::set_clipboard;
 use crate::util::set_clipboard;
 use crate::util::{
 use crate::util::{
-    format_bytes, open_url, print_error, print_error_msg, prompt_yes, quit, quit_error_msg,
-    ErrorHintsBuilder,
+    bin_name, format_bytes, open_url, print_error, print_error_msg, prompt_yes, quit,
+    quit_error_msg, ErrorHintsBuilder,
 };
 };
 
 
 /// A file upload action.
 /// A file upload action.
@@ -268,8 +268,15 @@ impl<'a> Upload<'a> {
         // Copy the URL in the user's clipboard
         // Copy the URL in the user's clipboard
         #[cfg(feature = "clipboard")]
         #[cfg(feature = "clipboard")]
         {
         {
-            if matcher_upload.copy() {
-                if let Err(err) = set_clipboard(url.as_str().to_owned()) {
+            if let Some(copy_mode) = matcher_upload.copy() {
+                // Build the string to copy
+                let copy = match copy_mode {
+                    CopyMode::Url => url.as_str().to_owned(),
+                    CopyMode::DownloadCmd => format!("{} download {}", bin_name(), url.as_str()),
+                };
+
+                // Copy to clipboard
+                if let Err(err) = set_clipboard(copy) {
                     print_error(
                     print_error(
                         err.context("failed to copy the share link to the clipboard, ignoring"),
                         err.context("failed to copy the share link to the clipboard, ignoring"),
                     );
                     );

+ 1 - 1
src/cmd/matcher/mod.rs

@@ -22,7 +22,7 @@ pub use self::info::InfoMatcher;
 pub use self::main::MainMatcher;
 pub use self::main::MainMatcher;
 pub use self::params::ParamsMatcher;
 pub use self::params::ParamsMatcher;
 pub use self::password::PasswordMatcher;
 pub use self::password::PasswordMatcher;
-pub use self::upload::UploadMatcher;
+pub use self::upload::{CopyMode, UploadMatcher};
 pub use self::version::VersionMatcher;
 pub use self::version::VersionMatcher;
 
 
 use clap::ArgMatches;
 use clap::ArgMatches;

+ 23 - 3
src/cmd/matcher/upload.rs

@@ -93,10 +93,21 @@ impl<'a: 'b, 'b> UploadMatcher<'a> {
         self.matches.is_present("open") || env_var_present("FFSEND_OPEN")
         self.matches.is_present("open") || env_var_present("FFSEND_OPEN")
     }
     }
 
 
-    /// Check whether to copy the file URL in the user's clipboard.
+    /// Check whether to copy the file URL in the user's clipboard, get the copy mode.
     #[cfg(feature = "clipboard")]
     #[cfg(feature = "clipboard")]
-    pub fn copy(&self) -> bool {
-        self.matches.is_present("copy") || env_var_present("FFSEND_COPY")
+    pub fn copy(&self) -> Option<CopyMode> {
+        // Get the options
+        let copy = self.matches.is_present("copy") || env_var_present("FFSEND_COPY");
+        let copy_cmd = self.matches.is_present("copy-cmd") || env_var_present("FFSEND_COPY_CMD");
+
+        // Return the corresponding copy mode
+        if copy_cmd {
+            Some(CopyMode::DownloadCmd)
+        } else if copy {
+            Some(CopyMode::Url)
+        } else {
+            None
+        }
     }
     }
 }
 }
 
 
@@ -107,3 +118,12 @@ impl<'a> Matcher<'a> for UploadMatcher<'a> {
             .map(|matches| UploadMatcher { matches })
             .map(|matches| UploadMatcher { matches })
     }
     }
 }
 }
+
+/// The copy mode.
+pub enum CopyMode {
+    /// Copy the public share link.
+    Url,
+
+    /// Copy an ffsend download command.
+    DownloadCmd,
+}

+ 16 - 6
src/cmd/subcmd/upload.rs

@@ -55,12 +55,22 @@ impl CmdUpload {
         // Optional clipboard support
         // Optional clipboard support
         #[cfg(feature = "clipboard")]
         #[cfg(feature = "clipboard")]
         {
         {
-            cmd = cmd.arg(
-                Arg::with_name("copy")
-                    .long("copy")
-                    .short("c")
-                    .help("Copy the share link to your clipboard"),
-            );
+            cmd = cmd
+                .arg(
+                    Arg::with_name("copy")
+                        .long("copy")
+                        .short("c")
+                        .help("Copy the share link to your clipboard")
+                        .conflicts_with("copy-cmd"),
+                )
+                .arg(
+                    Arg::with_name("copy-cmd")
+                        .long("copy-cmd")
+                        .alias("copy-command")
+                        .short("C")
+                        .help("Copy the ffsend download command to your clipboard")
+                        .conflicts_with("copy"),
+                );
         }
         }
 
 
         cmd
         cmd

+ 1 - 1
src/util.rs

@@ -413,7 +413,7 @@ pub fn prompt_password(main_matcher: &MainMatcher, optional: bool) -> Option<Str
                     ErrorHints::default(),
                     ErrorHints::default(),
                 )
                 )
             } else {
             } else {
-                return None;
+                None
             }
             }
         }
         }
     }
     }