Merge pull request 'Rename working-directory flag to download-directory and use ~/Downloads as default download directory' (#16) from #7-Use-home-downloads-as-default-directory-for-downloads into master
Reviewed-on: https://stupstech.de/dev/Mr_Steppy/multi-ssh/pulls/16
This commit is contained in:
commit
a83cf1013c
@ -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"
|
||||
@ -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
|
||||
|
||||
|
||||
38
src/main.rs
38
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<String>,
|
||||
/// 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<PathBuf>,
|
||||
/// 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::<Result<Vec<_>, _>>()
|
||||
.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::<Vec<_>>();
|
||||
|
||||
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}"))?;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user