Browse Source

Begin adding support for paths relative to a non-standard
configuration file

Samuel W. Flint 3 years ago
parent
commit
96119f23c1
3 changed files with 23 additions and 2 deletions
  1. 6 0
      src/lib/config.rs
  2. 7 1
      src/lib/run.rs
  3. 10 1
      src/main.rs

+ 6 - 0
src/lib/config.rs

@@ -30,6 +30,10 @@ use crate::lib::group::{
 pub struct Config {
     #[serde(skip)]
     pub is_changed: bool,
+    #[serde(skip)]
+    pub is_not_default: bool,
+    #[serde(skip)]
+    pub base_path: PathBuf,
     #[serde(rename(serialize = "repo_type", deserialize = "repo_type"), default)]
     pub repo_types: BTreeMap<String, RepoType>,
     #[serde(rename(serialize = "repository", deserialize = "repository"), default)]
@@ -61,6 +65,8 @@ pub fn read_configuration_file(filename: &PathBuf) -> Config {
         Err(_) => {
             let config = Config {
                 is_changed: true,
+                is_not_default: false,
+                base_path: filename.parent().unwrap().to_path_buf(),
                 repo_types: BTreeMap::new(),
                 repositories: BTreeMap::new(),
                 actions: BTreeMap::new(),

+ 7 - 1
src/lib/run.rs

@@ -81,7 +81,13 @@ pub fn run_repository_sync(config: &Config, name: String) {
     match repository {
         Some(repository) => {
             if !repository.disabled {
-                let location = &repository.location;
+                let location = match config.is_not_default {
+                    true => {
+                        let thing = config.base_path.join(Path::new(&repository.location.to_string()));
+                        thing.to_str().unwrap()
+                    },
+                    _ => repository.location.as_str()
+                };
                 if !Path::new(&location).exists() {
                     if repository.auto_create {
                         run_repository_creation(config, name);

+ 10 - 1
src/main.rs

@@ -3,6 +3,7 @@ extern crate clap;
 use clap::App;
 
 use std::env;
+use std::path::{Path, PathBuf};
 
 mod lib;
 
@@ -27,13 +28,21 @@ fn main() {
     let config_file = find_config_file(matches.value_of("config"));
     let mut configuration: Config = read_configuration_file(&config_file);
 
+    if matches.is_present("config") {
+        configuration.is_not_default = true;
+        configuration.base_path = Path::new(matches.value_of("config").unwrap()).canonicalize().unwrap().parent().unwrap().to_path_buf();
+    }
+
     match matches.subcommand_name() {
         Some("run") => run::run(&configuration, matches.subcommand_matches("run").unwrap().values_of("name").unwrap()),
         Some("repository") => if let Some(matches) = matches.subcommand_matches("repository") {
             match matches.subcommand_name() {
                 Some("register") => if let Some(matches) = matches.subcommand_matches("register") {
                     let type_name = matches.value_of("type").unwrap().to_string();
-                    let location = env::current_dir().unwrap();
+                    let location = match configuration.is_not_default {
+                        true => env::current_dir().unwrap().strip_prefix(&configuration.base_path).unwrap().to_path_buf(),
+                        _ => env::current_dir().unwrap(),
+                    };
                     let location_string = location.to_str().unwrap().to_string();
                     let name = match matches.value_of("name") {
                         Some(string) => string.to_string(),