Browse Source

Add in creation of groups

Samuel W. Flint 5 years ago
parent
commit
086d372e13
4 changed files with 33 additions and 3 deletions
  1. 1 1
      src/lib/config.rs
  2. 11 0
      src/lib/group.rs
  3. 1 1
      src/lib/mod.rs
  4. 20 1
      src/main.rs

+ 1 - 1
src/lib/config.rs

@@ -35,7 +35,7 @@ pub struct Config {
     #[serde(rename(serialize = "action", deserialize = "action"), default)]
     pub actions: HashMap<String, Action>,
     #[serde(rename(serialize = "group", deserialize = "group"), default)]
-    groups: HashMap<String, Group>,
+    pub groups: HashMap<String, Group>,
 }
 
 pub fn find_config_file(original: Option<&str>) -> PathBuf {

+ 11 - 0
src/lib/group.rs

@@ -1,6 +1,8 @@
 use serde::{Deserialize, Serialize};
 use std::fmt;
 
+use crate::lib::config::Config;
+
 #[derive(Serialize, Deserialize)]
 pub struct Group {
     #[serde(default)]
@@ -11,6 +13,15 @@ pub struct Group {
     members: Vec<String>,
 }
 
+pub fn add(config: &mut Config, name: &String) {
+    let group = Group {
+        name: name.to_string(),
+        actions_after: Vec::new(),
+        members: Vec::new()
+    };
+    config.groups.insert(name.to_string(), group);
+}
+
 impl fmt::Display for Group {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         write!(f, "Group {}:\n\tRepos:\n", self.name)?;

+ 1 - 1
src/lib/mod.rs

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

+ 20 - 1
src/main.rs

@@ -14,7 +14,8 @@ use crate::lib::{
         Config
     },
     repository,
-    action
+    action,
+    group
 };
 
 fn main() {
@@ -108,6 +109,24 @@ fn main() {
                 _ => panic!("Something has gone horribly wrong...")
             }
         },
+        Some("group") => if let Some(matches) = matches.subcommand_matches("group") {
+            match matches.subcommand_name() {
+                Some("create") => if let Some(matches) = matches.subcommand_matches("create") {
+                    let name = matches.value_of("name").unwrap().to_string();
+                    group::add(&mut configuration, &name);
+                },
+                },
+                Some("show") => if let Some(matches) = matches.subcommand_matches("show") {
+                    let name = matches.value_of("name").unwrap().to_string();
+                    let group = configuration.groups.get(&name);
+                    match group {
+                        Some(group) => println!("{}", group),
+                        None => eprintln!("No known group named \"{}\".", name)
+                    }
+                },
+                _ => panic!("Something has gone horribly wrong...")
+            }
+        },
         Some(thing) => println!("{}", thing),
         _ => println!("No subcommand."),
     }