feat(config): add command to edit config files with preferred editor
This commit is contained in:
12
README.md
12
README.md
@@ -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:
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
|||||||
59
src/main.rs
59
src/main.rs
@@ -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(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user