Просмотр исходного кода

Implement quiet CLI flag, only report URLs for history with quiet

timvisee 6 лет назад
Родитель
Сommit
5ac3cd4b15
4 измененных файлов с 64 добавлено и 41 удалено
  1. 1 0
      README.md
  2. 51 41
      src/action/history.rs
  3. 7 0
      src/cmd/handler.rs
  4. 5 0
      src/cmd/matcher/main.rs

+ 1 - 0
README.md

@@ -275,6 +275,7 @@ empty.
 | `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           |
 
 At this time, no configuration or _dotfile_ file support is available.

+ 51 - 41
src/action/history.rs

@@ -26,7 +26,9 @@ impl<'a> History<'a> {
         // Get the history path, make sure it exists
         let history_path = matcher_main.history();
         if !history_path.is_file() {
-            eprintln!("No files in history");
+            if !matcher_main.quiet() {
+                eprintln!("No files in history");
+            }
             return Ok(());
         }
 
@@ -35,59 +37,67 @@ impl<'a> History<'a> {
 
         // Do not report any files if there aren't any
         if history.files().is_empty() {
-            eprintln!("No files in history");
+            if !matcher_main.quiet() {
+                eprintln!("No files in history");
+            }
             return Ok(());
         }
 
-        // Build the list of column names
-        let mut columns = vec!["#", "LINK", "EXPIRY"];
-        if matcher_main.verbose() {
-            columns.push("OWNER TOKEN");
-        }
-
-        // Create a new table
-        let mut table = Table::new();
-        table.set_format(FormatBuilder::new().padding(0, 2).build());
-        table.add_row(Row::new(
-            columns.into_iter().map(Cell::new).collect(),
-        ));
-
         // Get the list of files, and sort the first expiring files to be last
         let mut files = history.files().clone();
         files.sort_by(|a, b| b.expire_at().cmp(&a.expire_at()));
 
-        // Add an entry for each file
-        for (i, file) in files.iter().enumerate() {
-            // Build the expiry time string
-            let mut expiry = format_duration(&file.expire_duration());
-            if file.expire_uncertain() {
-                expiry.insert(0, '~');
-            }
-
-            // Get the owner token
-            let owner_token: String = match file.owner_token() {
-                Some(token) => token.clone(),
-                None => "?".into(),
-            };
-
-            // Define the cell values
-            let mut cells: Vec<String>= vec![
-                format!("{}", i + 1),
-                file.download_url(true).into_string(),
-                expiry,
-            ];
+        // Log a history table, or just the URLs in quiet mode
+        if !matcher_main.quiet() {
+            // Build the list of column names
+            let mut columns = vec!["#", "LINK", "EXPIRY"];
             if matcher_main.verbose() {
-                cells.push(owner_token);
+                columns.push("OWNER TOKEN");
             }
 
-            // Add the row
+            // Create a new table
+            let mut table = Table::new();
+            table.set_format(FormatBuilder::new().padding(0, 2).build());
             table.add_row(Row::new(
-                cells.into_iter().map(|c| Cell::new(&c)).collect(),
+                columns.into_iter().map(Cell::new).collect(),
             ));
-        }
 
-        // Print the table
-        table.printstd();
+            // Add an entry for each file
+            for (i, file) in files.iter().enumerate() {
+                // Build the expiry time string
+                let mut expiry = format_duration(&file.expire_duration());
+                if file.expire_uncertain() {
+                    expiry.insert(0, '~');
+                }
+
+                // Get the owner token
+                let owner_token: String = match file.owner_token() {
+                    Some(token) => token.clone(),
+                    None => "?".into(),
+                };
+
+                // Define the cell values
+                let mut cells: Vec<String>= vec![
+                    format!("{}", i + 1),
+                    file.download_url(true).into_string(),
+                    expiry,
+                ];
+                if matcher_main.verbose() {
+                    cells.push(owner_token);
+                }
+
+                // Add the row
+                table.add_row(Row::new(
+                    cells.into_iter().map(|c| Cell::new(&c)).collect(),
+                ));
+            }
+
+            // Print the table
+            table.printstd();
+
+        } else {
+            files.iter().for_each(|f| println!("{}", f.download_url(true)));
+        }
 
         Ok(())
     }

+ 7 - 0
src/cmd/handler.rs

@@ -123,6 +123,13 @@ impl<'a: 'b, 'b> Handler<'a> {
                         ))
                     ),
             )
+            .arg(
+                Arg::with_name("quiet")
+                    .long("quiet")
+                    .short("q")
+                    .global(true)
+                    .help("Produce output suitable for logging and automation"),
+            )
             .arg(
                 Arg::with_name("verbose")
                     .long("verbose")

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

@@ -71,6 +71,11 @@ impl<'a: 'b, 'b> MainMatcher<'a> {
         self.matches.is_present("incognito") || env_var_present("FFSEND_INCOGNITO")
     }
 
+    /// Check whether quiet mode is used.
+    pub fn quiet(&self) -> bool {
+        !self.verbose() && (self.matches.is_present("quiet") || env_var_present("FFSEND_QUIET"))
+    }
+
     /// Check whether verbose mode is used.
     pub fn verbose(&self) -> bool {
         self.matches.is_present("verbose") || env_var_present("FFSEND_VERBOSE")