feat(config): add command to edit config files with preferred editor
Some checks failed
build / checks-matrix (push) Successful in 19m18s
build / codecov (push) Failing after 19m13s
docs / docs (push) Failing after 28m39s
build / checks-build (push) Has been cancelled

This commit is contained in:
uttarayan21
2025-10-13 15:44:52 +05:30
parent 37e682a162
commit 92d69f13f0
3 changed files with 78 additions and 0 deletions

View File

@@ -144,6 +144,18 @@ Show configuration file search paths:
yarr config 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 ### Shell Completions
Generate shell completions: Generate shell completions:

View File

@@ -78,6 +78,13 @@ pub enum ConfigAction {
/// Show possible config file locations /// Show possible config file locations
Paths, Paths,
/// Edit configuration file with $EDITOR or vi
Edit {
/// Path to config file to edit
#[arg(short, long)]
path: Option<PathBuf>,
},
} }
impl Cli { impl Cli {

View File

@@ -6,6 +6,7 @@ mod tui;
use crate::config::AppConfig; use crate::config::AppConfig;
use clap::Parser; use clap::Parser;
use std::process; use std::process;
use std::process::Command;
use yarr_api::SonarrClient; use yarr_api::SonarrClient;
#[tokio::main] #[tokio::main]
@@ -122,6 +123,64 @@ fn handle_config_command(
println!(" YARR_SONARR_URL"); println!(" YARR_SONARR_URL");
println!(" YARR_SONARR_API_KEY"); 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(()) Ok(())
} }