From 71d089512e7d3e1840422cc4efe90a049c279f9c Mon Sep 17 00:00:00 2001 From: Steppy Date: Sat, 1 Feb 2025 13:52:34 +0100 Subject: [PATCH] Rename working-directory flag to download-directory and use ~/Downloads as default download directory --- Cargo.toml | 1 + README.md | 3 +-- src/main.rs | 38 ++++++++++++++++++++++++++++---------- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 87c08de..a1f6658 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,4 @@ edition = "2021" clap = { version = "4.5.23", features = ["derive"] } lazy-regex = "3.3.0" shell-words = "1.1.0" +homedir = "0.3.4" \ No newline at end of file diff --git a/README.md b/README.md index 0dfed25..d89d39f 100644 --- a/README.md +++ b/README.md @@ -66,8 +66,7 @@ Edit the server properties of steptech ```bash multi-ssh steptech -e server.properties ``` -Note that the above command will create a `.mssh` folder in your current working directory. That directory can be -changed with the `-d` flag. +This will create a copy of the file in your `~/Downloads` directory. That directory can be changed using the `-d` flag. ## Building from source diff --git a/src/main.rs b/src/main.rs index 56066c2..4c791ed 100644 --- a/src/main.rs +++ b/src/main.rs @@ -99,9 +99,11 @@ enum Command { /// If omitted, the command will be taken from the environment variable `MSSH_EDITOR`. #[arg(short, long)] editor: Option, - /// The directory where to save the file to - #[arg(short = 'd', long, default_value = ".mssh/downloads")] - working_directory: PathBuf, + /// The directory where to save the file to. + /// + /// Default directory is `~/Downloads` + #[arg(short = 'd', long)] + download_directory: Option, /// Override existing files in the working directory #[arg(short = 'f', long = "override", default_value = "false")] override_existing: bool, @@ -330,13 +332,27 @@ fn main() -> Result<(), String> { Command::Editor { file, editor, - working_directory, + download_directory, override_existing, } => { + //determine download directory + let download_directory = match download_directory { + Some(download_directory) => download_directory, + None => { + let home_dir = homedir::my_home() + .map_err(|e| format!("Failed to determine your home directory: {e}")) + .and_then(|home_dir| { + home_dir.ok_or("Failed to determine your home directory".to_string()) + }) + .map_err(|e| format!("Can't determine download directory: {e}"))?; + home_dir.join("Downloads") + } + }; + //get editor let editor = editor.ok_or(()).or_else(|_| env::var(EDITOR_ENV_VAR).map_err(|e| format!("You have not specified an editor. Please do so using the --editor flag or the {EDITOR_ENV_VAR} environment variable: {e}")))?; - fs::create_dir_all(&working_directory) + fs::create_dir_all(&download_directory) .map_err(|e| format!("failed to create working directory: {e}"))?; //make sure file doesn't exist in working directory yet, or will be overridden @@ -345,7 +361,7 @@ fn main() -> Result<(), String> { .ok_or("can only edit files, not directories")?; 'duplicate_check: { if !override_existing - && fs::read_dir(&working_directory) + && fs::read_dir(&download_directory) .map_err(|e| format!("failed to open working directory: {e}"))? .collect::, _>>() .map_err(|e| format!("error while querying working directory contents: {e}"))? @@ -355,7 +371,7 @@ fn main() -> Result<(), String> { let duplication_notification = format!( "A file with the name {} already exists in {}", file_name.to_string_lossy(), - working_directory.to_string_lossy() + download_directory.to_string_lossy() ); if !args.quiet { @@ -381,7 +397,7 @@ fn main() -> Result<(), String> { log!(logger, "Downloading file from {}...", server.ssh_name); ShellCmd::new("scp") .arg(osf!(&server.ssh_name) + ":" + server.server_directory_path.join(&file)) - .arg(&working_directory) + .arg(&download_directory) .run(&logger) .map_err(|e| format!("download failure: {e}"))?; @@ -389,7 +405,9 @@ fn main() -> Result<(), String> { let mut shell_args = shell_words::split(&editor) .map_err(|e| format!("failed to parse editor command: {e}"))? .into_iter() - .map(|part| part.replace_with_os_str(FILE_PLACEHOLDER, working_directory.join(file_name))) + .map(|part| { + part.replace_with_os_str(FILE_PLACEHOLDER, download_directory.join(file_name)) + }) .collect::>(); let command = shell_args.remove(0); @@ -400,7 +418,7 @@ fn main() -> Result<(), String> { //upload file again ShellCmd::new("scp") - .arg(working_directory.join(file_name)) + .arg(download_directory.join(file_name)) .arg(osf!(&server.ssh_name) + ":" + server.server_directory_path.join(&file)) .run(&logger) .map_err(|e| format!("failed to re-upload file: {e}"))?;