Browse Source

Add basic repository run

Samuel W. Flint 3 years ago
parent
commit
c7216e54d4
2 changed files with 63 additions and 5 deletions
  1. 5 5
      src/lib/repotype.rs
  2. 58 0
      src/lib/run.rs

+ 5 - 5
src/lib/repotype.rs

@@ -12,17 +12,17 @@ pub struct RepoType {
     #[serde(default)]
     pub create: String,
     #[serde(default)]
-    inward: String,
+    pub inward: String,
     #[serde(default)]
-    outward: String,
+    pub outward: String,
     #[serde(default)]
     status: String,
     #[serde(default)]
-    pre_inward: String,
+    pub pre_inward: String,
     #[serde(default)]
-    post_inward: String,
+    pub post_inward: String,
     #[serde(default)]
-    post_outward: String,
+    pub post_outward: String,
 }
 
 pub fn add(config: &mut Config, name: &String, description: &String, create: &String, inward: &String, outward: &String, status: &String, pre_inward: &String, post_inward: &String, post_outward: &String) {

+ 58 - 0
src/lib/run.rs

@@ -10,6 +10,7 @@ use crate::lib::{
 use string_template::Template;
 
 use std::process::Command;
+use std::path::Path;
 use std::collections::HashMap;
 
 pub fn run(config: &Config, names: Values<'_>) {
@@ -33,6 +34,63 @@ pub fn run_action(config: &Config, name: String) {
     }
 }
 
+pub fn run_repository_sync(config: &Config, name: String) {
+    let repository = config.repositories.get(&name.to_string());
+    match repository {
+        Some(repository) => {
+            if !repository.disabled {
+                let location = &repository.location;
+                if !Path::new(&location).exists() {
+                    if repository.auto_create {
+                        run_repository_creation(config, name);
+                    }
+                } else {
+                    let mut options: HashMap<&str, &str> = HashMap::new();
+                    for (key, value) in &repository.options {
+                        options.insert(key, value);
+                    }
+                    let repo_type = config.repo_types.get(&repository.repo_type);
+                    match repo_type {
+                        Some(repo_type) => {
+                            Command::new("sh")
+                                .current_dir(location)
+                                .arg("-c")
+                                .arg(Template::new(&repo_type.pre_inward).render(&options))
+                                .spawn();
+
+                            Command::new("sh")
+                                .current_dir(location)
+                                .arg("-c")
+                                .arg(Template::new(&repo_type.inward).render(&options))
+                                .spawn();
+                            
+                            Command::new("sh")
+                                .current_dir(location)
+                                .arg("-c")
+                                .arg(Template::new(&repo_type.post_inward).render(&options))
+                                .spawn();
+                            
+                            Command::new("sh")
+                                .current_dir(location)
+                                .arg("-c")
+                                .arg(Template::new(&repo_type.outward).render(&options))
+                                .spawn();
+                            
+                            Command::new("sh")
+                                .current_dir(location)
+                                .arg("-c")
+                                .arg(Template::new(&repo_type.post_outward).render(&options))
+                                .spawn();
+                        },
+                        None => panic!("No known repository type named \"{}\".", &repository.repo_type)
+                    }
+                }
+            }
+        },
+        None => panic!("No known repository named \"{}\".", name)
+    }
+}
+
 pub fn run_repository_creation(config: &Config, name: String) {
     let repository = config.repositories.get(&name.to_string());
     match repository {