Skip to main content

steer_cli/commands/
preferences.rs

1use super::Command;
2use crate::error::Error;
3use async_trait::async_trait;
4use eyre::Result;
5use steer_core::preferences::Preferences;
6
7pub struct PreferencesCommand {
8    pub action: PreferencesAction,
9}
10
11pub enum PreferencesAction {
12    Show,
13    Edit,
14    Reset,
15}
16
17#[async_trait]
18impl Command for PreferencesCommand {
19    async fn execute(&self) -> Result<()> {
20        match &self.action {
21            PreferencesAction::Show => self.show().await.map_err(Into::into),
22            PreferencesAction::Edit => self.edit().await.map_err(Into::into),
23            PreferencesAction::Reset => self.reset().await.map_err(Into::into),
24        }
25    }
26}
27
28impl PreferencesCommand {
29    async fn show(&self) -> std::result::Result<(), Error> {
30        let prefs = Preferences::load()?;
31        let path = Preferences::config_path()?;
32
33        println!("Preferences file: {}", path.display());
34        println!("\n{}", toml::to_string_pretty(&prefs)?);
35        Ok(())
36    }
37
38    async fn edit(&self) -> std::result::Result<(), Error> {
39        let path = Preferences::config_path()?;
40
41        // Ensure the file exists
42        if !path.exists() {
43            let prefs = Preferences::default();
44            prefs.save()?;
45        }
46
47        // Open in default editor
48        let editor = std::env::var("VISUAL")
49            .or_else(|_| std::env::var("EDITOR"))
50            .unwrap_or_else(|_| {
51                if cfg!(target_os = "windows") {
52                    "notepad".to_string()
53                } else {
54                    "vi".to_string()
55                }
56            });
57
58        let status = std::process::Command::new(&editor).arg(&path).status()?;
59
60        if !status.success() {
61            return Err(Error::Process(format!(
62                "Editor '{editor}' exited with status: {status}"
63            )));
64        }
65
66        Ok(())
67    }
68
69    async fn reset(&self) -> std::result::Result<(), Error> {
70        let path = Preferences::config_path()?;
71
72        if path.exists() {
73            std::fs::remove_file(&path)?;
74            println!("Preferences reset to defaults");
75        } else {
76            println!("No preferences file found");
77        }
78        Ok(())
79    }
80}