From 02af941711a194008d7b0ace14e9cb429ee2f6b7 Mon Sep 17 00:00:00 2001 From: Dotan Nahum Date: Sun, 19 May 2024 17:40:23 +0300 Subject: [PATCH] feat: lookup config up the tree --- teller-cli/src/cli.rs | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/teller-cli/src/cli.rs b/teller-cli/src/cli.rs index 84bb49bc..a150c96a 100644 --- a/teller-cli/src/cli.rs +++ b/teller-cli/src/cli.rs @@ -1,7 +1,10 @@ -use std::path::{Path, PathBuf}; +use std::{ + env::{self, current_dir}, + path::{Path, PathBuf}, +}; use clap::{Args, Parser, Subcommand, ValueEnum}; -use eyre::eyre; +use eyre::{eyre, OptionExt}; use teller_core::{exec, export, teller::Teller}; use teller_providers::{config::KV, providers::ProviderKind}; @@ -182,8 +185,35 @@ pub struct NewArgs { pub providers: Vec, } +fn find_file_upwards(start_dir: &Path, config_filename: &str) -> eyre::Result> { + let mut current_dir = start_dir; + + loop { + let config_path = current_dir.join(config_filename); + + // Check if the configuration file exists at the current path + if config_path.exists() { + return Ok(Some(config_path)); + } + + // Move to the parent directory + match current_dir.parent() { + Some(parent) => current_dir = parent, + None => return Ok(None), // No parent means we've reached the root + } + } +} + async fn load_teller(config: Option) -> eyre::Result { - let config_arg = config.unwrap_or_else(|| DEFAULT_FILE_PATH.to_string()); + let config_arg = if let Some(config) = config { + config + } else { + find_file_upwards(env::current_dir()?.as_path(), DEFAULT_FILE_PATH)? + .ok_or_eyre("cannot find configuration from current folder and up to root")? + .to_string_lossy() + .to_string() + }; + let config_path = Path::new(&config_arg); let teller = Teller::from_yaml(config_path).await?; Ok(teller)