feat: edit config command
This commit is contained in:
+52
-1
@@ -6,7 +6,7 @@ mod local;
|
||||
mod ssh;
|
||||
mod sync;
|
||||
|
||||
use std::{path::Path, process::ExitCode};
|
||||
use std::{fs, path::Path, process::ExitCode};
|
||||
|
||||
use anyhow::{Context, Result, bail};
|
||||
use clap::Parser;
|
||||
@@ -54,6 +54,56 @@ fn handle_list(path: &Path, cfg: &config::Config) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_edit_config() -> Result<()> {
|
||||
let path = config::global_config_path(home::home_dir().as_deref())
|
||||
.context("cannot determine home directory")?;
|
||||
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent)
|
||||
.with_context(|| format!("cannot create {}", parent.display()))?;
|
||||
}
|
||||
if !path.exists() {
|
||||
fs::write(&path, "").with_context(|| format!("cannot create {}", path.display()))?;
|
||||
}
|
||||
|
||||
let editor = std::env::var("EDITOR")
|
||||
.or_else(|_| std::env::var("VISUAL"))
|
||||
.unwrap_or_else(|_| {
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
"notepad".to_string()
|
||||
}
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
{
|
||||
"vi".to_string()
|
||||
}
|
||||
});
|
||||
|
||||
let cmd = format!("{editor} {}", path.display());
|
||||
let mut process = {
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
let mut c = std::process::Command::new("cmd");
|
||||
c.arg("/C").arg(&cmd);
|
||||
c
|
||||
}
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
{
|
||||
let mut c = std::process::Command::new("sh");
|
||||
c.arg("-c").arg(&cmd);
|
||||
c
|
||||
}
|
||||
};
|
||||
let status = process
|
||||
.status()
|
||||
.with_context(|| format!("failed to launch editor: {editor}"))?;
|
||||
|
||||
if !status.success() {
|
||||
std::process::exit(status.code().unwrap_or(1));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_list_servers(path: &Path, cfg: &config::Config) -> Result<()> {
|
||||
println!("config: {}", config::display(path));
|
||||
println!("configured servers:");
|
||||
@@ -112,6 +162,7 @@ fn run(cli: &Cli) -> Result<()> {
|
||||
Some(Commands::Pick) => handle_pick(cli, &path, &cfg),
|
||||
Some(Commands::List) => handle_list(&path, &cfg),
|
||||
Some(Commands::ListServers) => handle_list_servers(&path, &cfg),
|
||||
Some(Commands::EditConfig) => handle_edit_config(),
|
||||
None => handle_default(&path, &cfg),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user