feat: edit config command
This commit is contained in:
@@ -42,6 +42,9 @@ pub enum Commands {
|
|||||||
|
|
||||||
/// List all available servers
|
/// List all available servers
|
||||||
ListServers,
|
ListServers,
|
||||||
|
|
||||||
|
/// Open global config in $EDITOR
|
||||||
|
EditConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Args, Debug)]
|
#[derive(Args, Debug)]
|
||||||
|
|||||||
+5
-4
@@ -184,11 +184,12 @@ pub fn display(path: &Path) -> String {
|
|||||||
format!("~/{}", rest.to_string_lossy().replace('\\', "/"))
|
format!("~/{}", rest.to_string_lossy().replace('\\', "/"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn global_config_path(home: Option<&Path>) -> Option<PathBuf> {
|
||||||
|
Some(home?.join(".config").join(GLOBAL_DIR).join(CONFIG_FILE_NAME))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn global_path(home: Option<&Path>) -> Option<PathBuf> {
|
pub fn global_path(home: Option<&Path>) -> Option<PathBuf> {
|
||||||
let path = home?
|
let path = global_config_path(home)?;
|
||||||
.join(".config")
|
|
||||||
.join(GLOBAL_DIR)
|
|
||||||
.join(CONFIG_FILE_NAME);
|
|
||||||
path.is_file().then_some(path)
|
path.is_file().then_some(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+52
-1
@@ -6,7 +6,7 @@ mod local;
|
|||||||
mod ssh;
|
mod ssh;
|
||||||
mod sync;
|
mod sync;
|
||||||
|
|
||||||
use std::{path::Path, process::ExitCode};
|
use std::{fs, path::Path, process::ExitCode};
|
||||||
|
|
||||||
use anyhow::{Context, Result, bail};
|
use anyhow::{Context, Result, bail};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
@@ -54,6 +54,56 @@ fn handle_list(path: &Path, cfg: &config::Config) -> Result<()> {
|
|||||||
Ok(())
|
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<()> {
|
fn handle_list_servers(path: &Path, cfg: &config::Config) -> Result<()> {
|
||||||
println!("config: {}", config::display(path));
|
println!("config: {}", config::display(path));
|
||||||
println!("configured servers:");
|
println!("configured servers:");
|
||||||
@@ -112,6 +162,7 @@ fn run(cli: &Cli) -> Result<()> {
|
|||||||
Some(Commands::Pick) => handle_pick(cli, &path, &cfg),
|
Some(Commands::Pick) => handle_pick(cli, &path, &cfg),
|
||||||
Some(Commands::List) => handle_list(&path, &cfg),
|
Some(Commands::List) => handle_list(&path, &cfg),
|
||||||
Some(Commands::ListServers) => handle_list_servers(&path, &cfg),
|
Some(Commands::ListServers) => handle_list_servers(&path, &cfg),
|
||||||
|
Some(Commands::EditConfig) => handle_edit_config(),
|
||||||
None => handle_default(&path, &cfg),
|
None => handle_default(&path, &cfg),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user