common: Implement the "rm" shell command.
authorTilman Sauerbeck <tilman@code-monkey.de>
Tue, 11 Feb 2020 18:57:21 +0000 (19:57 +0100)
committerTilman Sauerbeck <tilman@code-monkey.de>
Tue, 18 Feb 2020 20:12:18 +0000 (21:12 +0100)
Removes a recording.

src/common/shell.rs

index 097bd0cf8d7e5b9cd8b85134aadf4bbe50cc1cb3..e48a5de7290c8d0336c210475228f478459528a5 100644 (file)
@@ -199,6 +199,7 @@ Supported commands:
     help           Show this help message.
     ls             List recordings.
     get REC_ID     Retrieve recording.
+    rm REC_ID      Remove recording.
     clear_storage  Fully erase the flash's contents.
     dump_storage   Dump the flash's contents.
 ";
@@ -208,6 +209,7 @@ Supported commands:
 
             Some(b"ls") => self.run_ls(logger),
             Some(b"get") => self.run_get(logger, &mut args_iter),
+            Some(b"rm") => self.run_rm(logger, &mut args_iter),
             Some(b"clear_storage") => self.run_clear_storage(logger),
             Some(b"dump_storage") => self.run_dump_storage(logger),
 
@@ -264,6 +266,28 @@ Supported commands:
         self.tx_buf.flush();
     }
 
+    fn run_rm(&mut self, logger: &mut Logger, args_iter: &mut ArgumentIter) {
+        if let Some(recording_id_s) = args_iter.next() {
+            if let Some(recording_id) = atoi16(recording_id_s) {
+                if logger.has_recording(recording_id) {
+                    let result = logger.remove_recording(recording_id);
+
+                    if result.is_err() {
+                        self.tx_buf.write(b"rm: failed to remove recording\n");
+                    }
+                } else {
+                    self.tx_buf.write(b"rm: no such recording\n");
+                }
+            } else {
+                self.tx_buf.write(b"rm: invalid argument\n");
+            }
+        } else {
+            self.tx_buf.write(b"rm: missing argument\n");
+        }
+
+        self.tx_buf.flush();
+    }
+
     fn run_clear_storage(&self, logger: &mut Logger) {
         logger.storage.clear();
     }