From 21c234735ebf74899ce9a98cb46d9500b3f51e89 Mon Sep 17 00:00:00 2001 From: diogo464 Date: Fri, 9 Jul 2021 10:03:42 -0400 Subject: Search parent directories for depot file. If the depot file is not specified and is not found in the current directory then it will be searched trough the parent directories until a depot file is found. --- dotup_cli/src/commands/utils.rs | 14 ++++++++++++++ dotup_cli/src/main.rs | 18 ++++++++++++------ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/dotup_cli/src/commands/utils.rs b/dotup_cli/src/commands/utils.rs index 4160078..706266e 100644 --- a/dotup_cli/src/commands/utils.rs +++ b/dotup_cli/src/commands/utils.rs @@ -2,6 +2,8 @@ use std::path::{Path, PathBuf}; use crate::prelude::*; +const DEFAULT_DEPOT_NAME: &str = "depot.toml"; + pub fn home_directory() -> anyhow::Result { match std::env::var("HOME") { Ok(val) => Ok(PathBuf::from(val)), @@ -12,6 +14,18 @@ pub fn home_directory() -> anyhow::Result { } } +pub fn find_archive_path() -> anyhow::Result { + let mut start = PathBuf::new(); + while { + start.push(DEFAULT_DEPOT_NAME); + !start.is_file() + } { + start.pop(); + start.push(".."); + } + Ok(start.canonicalize()?) +} + pub fn write_archive(path: impl AsRef, archive: &Archive) -> anyhow::Result<()> { let path = path.as_ref(); log::debug!("Writing archive to {}", path.display()); diff --git a/dotup_cli/src/main.rs b/dotup_cli/src/main.rs index 867afb4..0b4bff1 100644 --- a/dotup_cli/src/main.rs +++ b/dotup_cli/src/main.rs @@ -12,6 +12,7 @@ pub mod prelude { } use clap::{AppSettings, Clap}; +use commands::utils; use flexi_logger::Logger; use std::{ collections::HashMap, @@ -21,14 +22,15 @@ use std::{ use prelude::*; -const DEFAULT_DEPOT_NAME: &str = "depot.toml"; - #[derive(Clap)] #[clap(setting = AppSettings::ColoredHelp)] struct Opts { /// Path to the depot file. - #[clap(long, default_value = DEFAULT_DEPOT_NAME)] - depot: PathBuf, + /// + /// By default it will try to find a file named "depot.toml" in the current directory or any of + /// the parent directories. + #[clap(long)] + depot: Option, /// Disable output to the console #[clap(short, long)] @@ -76,9 +78,13 @@ fn main() -> anyhow::Result<()> { .start()?; } - let config = Config { - archive_path: opts.depot, + let archive_path = match opts.depot { + Some(path) => path, + None => utils::find_archive_path()?, }; + log::debug!("Archive path : {}", archive_path.display()); + + let config = Config { archive_path }; match opts.subcmd { SubCommand::Init(opts) => commands::init::main(config, opts), -- cgit