From 92d69f13f099981532d0a2e4e4cb27bc66fd6521 Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Mon, 13 Oct 2025 15:44:52 +0530 Subject: [PATCH] feat(config): add command to edit config files with preferred editor --- README.md | 12 +++++++++++ src/cli.rs | 7 +++++++ src/main.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) diff --git a/README.md b/README.md index e2ae410..1e4e855 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,18 @@ Show configuration file search paths: yarr config paths ``` +Edit configuration file with your preferred editor: + +```bash +yarr config edit +``` + +Edit specific config file: + +```bash +yarr config edit --path /path/to/config.toml +``` + ### Shell Completions Generate shell completions: diff --git a/src/cli.rs b/src/cli.rs index c1b7a81..c7995a4 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -78,6 +78,13 @@ pub enum ConfigAction { /// Show possible config file locations Paths, + + /// Edit configuration file with $EDITOR or vi + Edit { + /// Path to config file to edit + #[arg(short, long)] + path: Option, + }, } impl Cli { diff --git a/src/main.rs b/src/main.rs index ed05448..3dc7f68 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ mod tui; use crate::config::AppConfig; use clap::Parser; use std::process; +use std::process::Command; use yarr_api::SonarrClient; #[tokio::main] @@ -122,6 +123,64 @@ fn handle_config_command( println!(" YARR_SONARR_URL"); println!(" YARR_SONARR_API_KEY"); } + cli::ConfigAction::Edit { path } => { + let config_path = if let Some(path) = path { + path.clone() + } else { + // Find existing config file or use default location + let paths = AppConfig::get_default_config_paths(); + let existing_config = paths.iter().find(|p| p.exists()); + + if let Some(existing_path) = existing_config { + existing_path.clone() + } else { + // No existing config found, use preferred default location + if let Some(default_path) = paths.get(1) { + // Prefer user config directory over current directory + default_path.clone() + } else { + std::env::current_dir()?.join("yarr.toml") + } + } + }; + + // Create config file if it doesn't exist + if !config_path.exists() { + println!( + "Config file doesn't exist. Creating sample config at: {}", + config_path.display() + ); + AppConfig::create_sample_config(&config_path)?; + } + + // Get editor from environment or use vi as fallback + let editor = std::env::var("EDITOR").unwrap_or_else(|_| "vi".to_string()); + + println!( + "Opening config file with {}: {}", + editor, + config_path.display() + ); + + // Execute editor + let status = Command::new(&editor).arg(&config_path).status(); + + match status { + Ok(exit_status) => { + if exit_status.success() { + println!("Configuration file edited successfully."); + } else { + eprintln!("Editor exited with non-zero status: {}", exit_status); + process::exit(1); + } + } + Err(e) => { + eprintln!("Failed to launch editor '{}': {}", editor, e); + eprintln!("Make sure the editor is installed and accessible in your PATH."); + process::exit(1); + } + } + } } Ok(()) }