1use crate::settings::{Settings, get_settings_file_path};
3use anyhow::{Context, Result, bail};
4use clap::Subcommand;
5use std::fs;
6use std::io::{self, Write};
7use std::path::Path;
8
9#[derive(Subcommand)]
11pub enum SettingsSubcommands {
12 Edit,
14 Delete,
16 Path,
18 Show,
20 ShowDefault,
22}
23
24impl SettingsSubcommands {
25 pub fn execute(self) -> Result<()> {
27 match self {
28 Self::Edit => handle_edit_command()?,
29 Self::Delete => handle_delete_command()?,
30 Self::Path => handle_path_command(),
31 Self::Show => handle_show_command()?,
32 Self::ShowDefault => handle_show_default_command(),
33 }
34
35 Ok(())
36 }
37}
38
39fn ensure_settings_file_exists(file_path: &Path) -> Result<()> {
41 if file_path.is_file() {
42 return Ok(());
44 }
45
46 if let Some(dir_path) = file_path.parent() {
47 fs::create_dir_all(dir_path)
49 .with_context(|| format!("Failed to create directory: {}", dir_path.display()))?;
50 }
51
52 fs::write(file_path, Settings::default_file_contents())?;
54
55 Ok(())
56}
57
58fn handle_edit_command() -> Result<()> {
60 let file_path = get_settings_file_path();
61 ensure_settings_file_exists(&file_path)?;
62
63 println!("Opening settings file for editing: {}", file_path.display());
65 edit::edit_file(&file_path)?;
66
67 Ok(())
68}
69
70fn handle_delete_command() -> Result<()> {
72 let file_path = get_settings_file_path();
73 if file_path.exists() {
74 fs::remove_file(&file_path)
75 .with_context(|| format!("Error deleting file: {}", file_path.display()))?;
76 println!("Deleted settings file: {}", file_path.display());
77 } else {
78 eprintln!("No settings file to delete");
79 }
80
81 Ok(())
82}
83
84fn handle_path_command() {
86 let file_path = get_settings_file_path();
87 if file_path.is_file() {
88 println!("{}", file_path.display());
89 } else {
90 eprintln!("Settings file not found at: {}", file_path.display());
91 }
92}
93
94fn handle_show_command() -> Result<()> {
96 let file_path = get_settings_file_path();
97
98 match fs::read(&file_path) {
99 Ok(ref contents) => io::stdout().write_all(contents)?,
101 Err(err) => {
102 if err.kind() == io::ErrorKind::NotFound {
103 bail!("Settings file not found at: {}", file_path.display())
104 }
105 Err(err)?;
107 }
108 }
109
110 Ok(())
111}
112
113fn handle_show_default_command() {
115 print!("{}", Settings::default_file_contents());
116}