Skip to main content

mai_cli/cli/
use_cmd.rs

1use crate::core::MaiConfig;
2use crate::error::Result;
3use crate::storage::XdgPaths;
4use clap::Args;
5
6#[derive(Debug, Args)]
7pub struct UseCommand {
8    /// Tool name to switch to (e.g., qwen, claude)
9    pub tool: String,
10}
11
12impl UseCommand {
13    pub fn run(&self) -> Result<()> {
14        let paths = XdgPaths::new();
15        let config_path = paths.config_file();
16
17        let mut config = if config_path.exists() {
18            let content = std::fs::read_to_string(&config_path)?;
19            toml::from_str::<MaiConfig>(&content).unwrap_or_default()
20        } else {
21            MaiConfig::new()
22        };
23
24        config.set_active_tool(&self.tool);
25
26        std::fs::create_dir_all(paths.config_dir())?;
27        let content = toml::to_string_pretty(&config)?;
28        std::fs::write(&config_path, content)?;
29
30        println!("✓ Switched to tool: {}", self.tool);
31        Ok(())
32    }
33}