Skip to main content

chatty_rs/
cli.rs

1use clap::Parser;
2use eyre::{Context, Result};
3
4use crate::config::{self, Configuration, load_configuration, lookup_config_path};
5
6#[derive(Debug, Parser)]
7#[command(
8    version,
9    about,
10    long_about = r#"A Terminal UI to interact OpenAI models
11
12Default configuration file location looks up in the following order:
13    * $XDG_CONFIG_HOME/chatty/config.toml
14    * $HOME/.config/chatty/config.toml
15    * $HOME/.chatty.toml
16"#,
17    disable_version_flag = true
18)]
19pub struct Command {
20    /// Configuration file path
21    #[arg(short, long, value_name = "PATH")]
22    config: Option<String>,
23
24    /// Show the version
25    #[arg(short, long)]
26    version: bool,
27}
28
29impl Command {
30    pub fn get_config(&self) -> Result<&Configuration> {
31        let config_path = self
32            .config
33            .clone()
34            .unwrap_or_else(|| lookup_config_path().unwrap_or_default());
35
36        let config = if !config_path.is_empty() {
37            load_configuration(config_path.as_str()).wrap_err("loading configuration")?
38        } else {
39            Configuration::default()
40        };
41
42        config::init(config).wrap_err("initializing configuration")?;
43        Ok(config::instance())
44    }
45
46    pub fn version(&self) -> bool {
47        self.version
48    }
49
50    pub fn print_version(&self) {
51        println!("{}", config::version())
52    }
53}
54
55impl Default for Command {
56    fn default() -> Self {
57        Self::parse()
58    }
59}