From 5508d691bce63fc3ca999eed4f170d9fe6ec45e5 Mon Sep 17 00:00:00 2001 From: ob7 Date: Mon, 25 Nov 2024 01:18:33 -0900 Subject: [PATCH] Add custom config file path support via -c flag (#37) --- src/config.rs | 15 ++++++++++----- src/main.rs | 16 ++++++++++++---- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/config.rs b/src/config.rs index acda99c..f3a608a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,6 +3,7 @@ use toml; use dirs; use serde::Deserialize; +use std::path::PathBuf; #[derive(Deserialize, Debug)] pub struct Config { @@ -132,11 +133,15 @@ impl KeyBindings { } impl Config { - pub fn load() -> Self { - let conf_path = dirs::config_dir() - .unwrap() - .join("tenere") - .join("config.toml"); + pub fn load(custom_path: Option) -> Self { + let conf_path = if let Some(path) = custom_path { + path + } else { + dirs::config_dir() + .unwrap() + .join("tenere") + .join("config.toml") + }; let config = std::fs::read_to_string(conf_path).unwrap_or_default(); let app_config: Config = toml::from_str(&config).unwrap(); diff --git a/src/main.rs b/src/main.rs index 4106392..9e0b1ea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use ratatui::backend::CrosstermBackend; use ratatui::Terminal; -use std::{env, io}; +use std::{env, io, path::PathBuf}; use tenere::app::{App, AppResult}; use tenere::config::Config; use tenere::event::{Event, EventHandler}; @@ -14,16 +14,24 @@ use tenere::llm::LLMModel; use std::sync::Arc; use tokio::sync::Mutex; -use clap::{crate_description, crate_version, Command}; +use clap::{crate_description, crate_version, Command, Arg}; #[tokio::main] async fn main() -> AppResult<()> { - Command::new("tenere") + let matches = Command::new("tenere") .about(crate_description!()) .version(crate_version!()) + .arg( + Arg::new("config") + .short('c') + .long("config") + .help("Path to custom config file") + .value_name("FILE") + ) .get_matches(); - let config = Arc::new(Config::load()); + let config_path = matches.get_one::("config").map(PathBuf::from); + let config = Arc::new(Config::load(config_path)); let (formatter_config, formatter_assets) = Formatter::init(); let formatter = Formatter::new(&formatter_config, &formatter_assets);