Ver código fonte

Add better action handling

Samuel W. Flint 5 anos atrás
pai
commit
9275893063
3 arquivos alterados com 58 adições e 3 exclusões
  1. 9 2
      src/cli.yml
  2. 24 0
      src/lib/action.rs
  3. 25 1
      src/main.rs

+ 9 - 2
src/cli.yml

@@ -235,15 +235,22 @@ subcommands:
                   value_name: NAME
                   help: The name of the action
                   required: true
-              - enabled:
-                  long: enabled
+              - disabled:
+                  long: disabled
                   value_name: YES/NO
                   help: Enable this action
+                  takes_value: true
               - command:
                   long: command
                   value_name: COMMAND
                   help: The command the action runs
+                  takes_value: true
                   required: true
+              - description:
+                  long: description
+                  value_name: DESCRIPTION
+                  help: Description of the action
+                  takes_value: true
         - show:
             about: Show information about an action
             args:

+ 24 - 0
src/lib/action.rs

@@ -25,6 +25,30 @@ pub fn add(config: &mut Config, name: &String, description: &String, command: &S
     config.actions.insert(name.to_string(), action);
 }
 
+pub fn update_disabled(config: &mut Config, name: &String, value: bool) {
+    let action = config.actions.get_mut(&name.to_string());
+    match action {
+        Some(action) => action.disabled = value,
+        None => panic!("No known action named \"{}\".", name)
+    }
+}
+
+pub fn update_description(config: &mut Config, name: &String, description: &String) {
+    let action = config.actions.get_mut(&name.to_string());
+    match action {
+        Some(action) => action.description = description.to_string(),
+        None => panic!("No known action named \"{}\".", name)
+    }
+}
+
+pub fn update_command(config: &mut Config, name: &String, command: &String) {
+    let action = config.actions.get_mut(&name.to_string());
+    match action {
+        Some(action) => action.command = command.to_string(),
+        None => panic!("No known action named \"{}\".", name)
+    }
+}
+
 impl fmt::Display for Action {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         write!(f, "Action {}:\n\t\"{}\"\n\tCommand: \"{}\"\n\tDisabled: {}",

+ 25 - 1
src/main.rs

@@ -80,7 +80,31 @@ fn main() {
                         _ => String::from("")
                     };
                     action::add(&mut configuration, &name, &command, &description);
-                }
+                },
+                Some("config") => if let Some(matches) = matches.subcommand_matches("config") {
+                    let name = matches.value_of("name").unwrap().to_string();
+                    match matches.value_of("disabled") {
+                        Some("YES") => action::update_disabled(&mut configuration, &name, true),
+                        Some("NO") => action::update_disabled(&mut configuration, &name, false),
+                        _ => {}
+                    }
+                    match matches.value_of("command") {
+                        Some(command) => action::update_command(&mut configuration, &name, &command.to_string()),
+                        _ => {}
+                    }
+                    match matches.value_of("description") {
+                        Some(description) => action::update_description(&mut configuration, &name, &description.to_string()),
+                        _ => {}
+                    }
+                },
+                Some("show") => if let Some(matches) = matches.subcommand_matches("show") {
+                    let name = matches.value_of("name").unwrap().to_string();
+                    let action = configuration.actions.get(&name);
+                    match action {
+                        Some(action) => println!("{}", action),
+                        None => eprintln!("No known action named \"{}\".", name)
+                    }
+                },
                 _ => panic!("Something has gone horribly wrong...")
             }
         },