Browse Source

Add repository creation

Samuel W. Flint 3 years ago
parent
commit
d1ad5637f0
3 changed files with 36 additions and 6 deletions
  1. 5 5
      src/lib/repository.rs
  2. 1 1
      src/lib/repotype.rs
  3. 30 0
      src/lib/run.rs

+ 5 - 5
src/lib/repository.rs

@@ -9,15 +9,15 @@ pub struct Repository {
     #[serde(default)]
     name: String,
     #[serde(default)]
-    location: String,
+    pub location: String,
     #[serde(default)]
-    repo_type: String,
+    pub repo_type: String,
     #[serde(default)]
-    auto_create: bool,
+    pub auto_create: bool,
     #[serde(default)]
-    disabled: bool,
+    pub disabled: bool,
     #[serde(default)]
-    options: HashMap<String, String>,
+    pub options: HashMap<String, String>,
 }
 
 pub fn register(config: &mut Config, name: &String, location: String, repo_type: String, options_strings: Vec<String>) {

+ 1 - 1
src/lib/repotype.rs

@@ -10,7 +10,7 @@ pub struct RepoType {
     #[serde(default)]
     description: String,
     #[serde(default)]
-    create: String,
+    pub create: String,
     #[serde(default)]
     inward: String,
     #[serde(default)]

+ 30 - 0
src/lib/run.rs

@@ -7,8 +7,10 @@ use crate::lib::{
     action::Action
 };
 
+use string_template::Template;
 
 use std::process::Command;
+use std::collections::HashMap;
 
 pub fn run(config: &Config, names: Values<'_>) {
     for name in names {
@@ -29,3 +31,31 @@ pub fn run_action(config: &Config, name: String) {
         None => panic!("No known action named \"{}\".", name)
     }
 }
+
+pub fn run_repository_creation(config: &Config, name: String) {
+    let repository = config.repositories.get(&name.to_string());
+    match repository {
+        Some(repository) => {
+            let repository_type_name = &repository.repo_type;
+            let repository_type = config.repo_types.get(repository_type_name);
+            match repository_type {
+                Some(repository_type) => {
+                    if !repository.disabled {
+                        let mut options: HashMap<&str, &str> = HashMap::new();
+                        for (key, value) in &repository.options {
+                            options.insert(key, value);
+                        }
+                        options.insert("location", &repository.location);
+                        let create_command = Template::new(&repository_type.create);
+                        Command::new("sh")
+                            .arg("-c")
+                            .arg(create_command.render(&options))
+                            .spawn();
+                    }
+                },
+                None => panic!("No known repository type named \"{}\".", repository_type_name)
+            }
+        },
+        None => panic!("No known repository named \"{}\".", name)
+    }
+}