Quellcode durchsuchen

Add ability to add actions

Samuel W. Flint vor 5 Jahren
Ursprung
Commit
7da3499539
5 geänderte Dateien mit 36 neuen und 4 gelöschten Zeilen
  1. 5 0
      src/cli.yml
  2. 12 0
      src/lib/action.rs
  3. 1 1
      src/lib/config.rs
  4. 1 1
      src/lib/mod.rs
  5. 17 2
      src/main.rs

+ 5 - 0
src/cli.yml

@@ -222,6 +222,11 @@ subcommands:
                   value_name: COMMAND
                   help: The command the action runs
                   required: true
+              - description:
+                  long: description
+                  value_name: DESCRIPTION
+                  help: Description of the action
+                  takes_value: true
         - config:
             about: Configure an action
             args:

+ 12 - 0
src/lib/action.rs

@@ -1,6 +1,8 @@
 use serde::{Deserialize, Serialize};
 use std::fmt;
 
+use crate::lib::config::Config;
+
 #[derive(Serialize, Deserialize)]
 pub struct Action {
     #[serde(default)]
@@ -13,6 +15,16 @@ pub struct Action {
     disabled: bool,
 }
 
+pub fn add(config: &mut Config, name: &String, description: &String, command: &String) {
+    let action = Action {
+        name : name.to_string(),
+        description: description.to_string(),
+        command: command.to_string(),
+        disabled: false
+    };
+    config.actions.insert(name.to_string(), action);
+}
+
 impl fmt::Display for Action {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         write!(f, "Action {}:\n\t\"{}\"\n\tCommand: \"{}\"\n\tDisabled: {}",

+ 1 - 1
src/lib/config.rs

@@ -33,7 +33,7 @@ pub struct Config {
     #[serde(rename(serialize = "repository", deserialize = "repository"), default)]
     pub repositories: HashMap<String, Repository>,
     #[serde(rename(serialize = "action", deserialize = "action"), default)]
-    actions: HashMap<String, Action>,
+    pub actions: HashMap<String, Action>,
     #[serde(rename(serialize = "group", deserialize = "group"), default)]
     groups: HashMap<String, Group>,
 }

+ 1 - 1
src/lib/mod.rs

@@ -1,4 +1,4 @@
-mod action;
+pub mod action;
 pub mod config;
 mod group;
 pub mod repository;

+ 17 - 2
src/main.rs

@@ -13,7 +13,8 @@ use crate::lib::{
         write_configuration_file,
         Config
     },
-    repository
+    repository,
+    action
 };
 
 fn main() {
@@ -68,7 +69,21 @@ fn main() {
                 }
                 _ => panic!("Something has gone horribly wrong...")
             }
-        }
+        },
+        Some("action") => if let Some(matches) = matches.subcommand_matches("action") {
+            match matches.subcommand_name() {
+                Some("create") => if let Some(matches) = matches.subcommand_matches("create") {
+                    let name = matches.value_of("name").unwrap().to_string();
+                    let command = matches.value_of("command").unwrap().to_string();
+                    let description = match matches.value_of("description") {
+                        Some(string) => string.to_string(),
+                        _ => String::from("")
+                    };
+                    action::add(&mut configuration, &name, &command, &description);
+                }
+                _ => panic!("Something has gone horribly wrong...")
+            }
+        },
         Some(thing) => println!("{}", thing),
         _ => println!("No subcommand."),
     }