Skip to content

Commit

Permalink
Add custom config file path support via -c flag (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
ob7 authored Nov 25, 2024
1 parent a99f839 commit 5508d69
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
15 changes: 10 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use toml;

use dirs;
use serde::Deserialize;
use std::path::PathBuf;

#[derive(Deserialize, Debug)]
pub struct Config {
Expand Down Expand Up @@ -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<PathBuf>) -> 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();
Expand Down
16 changes: 12 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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::<String>("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);
Expand Down

0 comments on commit 5508d69

Please sign in to comment.