diff options
author | Bryan Newbold <bnewbold@archive.org> | 2021-02-10 14:09:47 -0800 |
---|---|---|
committer | Bryan Newbold <bnewbold@archive.org> | 2021-02-10 14:09:47 -0800 |
commit | 64b7dd2cd2e55008851ba5011032433e568d3544 (patch) | |
tree | b479dec3e4f88a641563894fd5c6491aded31d3f /fatcat-cli/src/main.rs | |
parent | 29f2b072a6395edf8527de9a5ae76d53a045819c (diff) | |
download | fatcat-cli-64b7dd2cd2e55008851ba5011032433e568d3544.tar.gz fatcat-cli-64b7dd2cd2e55008851ba5011032433e568d3544.zip |
download output path/directory args
Diffstat (limited to 'fatcat-cli/src/main.rs')
-rw-r--r-- | fatcat-cli/src/main.rs | 45 |
1 files changed, 35 insertions, 10 deletions
diff --git a/fatcat-cli/src/main.rs b/fatcat-cli/src/main.rs index 055ac41..760f851 100644 --- a/fatcat-cli/src/main.rs +++ b/fatcat-cli/src/main.rs @@ -20,7 +20,7 @@ struct Opt { )] api_host: String, - /// API auth tokens can be generated from the account page in the fatcat.wiki web interface + // API auth tokens can be generated from the account page in the fatcat.wiki web interface #[structopt( global = true, long = "--api-token", @@ -127,7 +127,13 @@ enum BatchCommand { #[structopt(long)] auto_accept: bool, }, - Download {}, + Download { + #[structopt(long, short = "-o", parse(from_os_str))] + output_dir: Option<PathBuf>, + + #[structopt(long, short = "-j", default_value = "1")] + jobs: u64, + }, } #[derive(StructOpt)] @@ -151,7 +157,7 @@ enum Command { entity_type: EntityType, /// Input file, "-" for stdin. - #[structopt(long = "--file", short = "-f", parse(from_os_str))] + #[structopt(long = "--input-file", short = "-i", parse(from_os_str))] input_path: Option<PathBuf>, #[structopt( @@ -166,7 +172,7 @@ enum Command { specifier: Specifier, /// Input file, "-" for stdin. - #[structopt(long = "--file", short = "-f", parse(from_os_str))] + #[structopt(long = "--input-file", short = "-i", parse(from_os_str))] input_path: Option<PathBuf>, #[structopt( @@ -213,6 +219,9 @@ enum Command { }, Download { specifier: Specifier, + + #[structopt(long = "--output-dir", short = "-o", parse(from_os_str))] + output_path: Option<PathBuf>, }, History { specifier: Specifier, @@ -266,7 +275,7 @@ enum Command { cmd: BatchCommand, /// Input file, "-" for stdin. - #[structopt(long = "--file", short = "-f", parse(from_os_str))] + #[structopt(long = "--input-file", short = "-i", parse(from_os_str))] input_path: Option<PathBuf>, #[structopt(long)] @@ -463,14 +472,25 @@ fn run(opt: Opt) -> Result<()> { batch.run(&mut api_client, input_path, BatchOp::Delete, None)?; } Command::Batch { - cmd: BatchCommand::Download {}, + cmd: BatchCommand::Download { + jobs, + output_dir, + }, input_path, limit, } => { let input_path = path_or_stdin(input_path); - download_batch(input_path, limit)?; + if let Some(ref dir) = output_dir { + if !dir.is_dir() { + return Err(anyhow!("output directory doesn't exist")); + } + } + download_batch(input_path, output_dir, limit, jobs)?; } - Command::Download { specifier } => { + Command::Download { + specifier, + output_path, + } => { // run lookups if necessary (inefficient) let specifier = match specifier { Specifier::ReleaseLookup(_, _) | Specifier::FileLookup(_, _) => { @@ -478,6 +498,11 @@ fn run(opt: Opt) -> Result<()> { } _ => specifier, }; + if let Some(ref path) = output_path { + if path.exists() { + return Err(anyhow!("refusing to over-write output file")); + } + } let status = match specifier { Specifier::Release(ident) => { let result = api_client.rt.block_on(api_client.api.get_release( @@ -490,7 +515,7 @@ fn run(opt: Opt) -> Result<()> { resp => Err(anyhow!("{:?}", resp)) .with_context(|| format!("API GET failed: {:?}", ident)), }?; - download_release(&release_entity) + download_release(&release_entity, output_path) } Specifier::File(ident) => { let result = api_client.rt.block_on(api_client.api.get_file( @@ -503,7 +528,7 @@ fn run(opt: Opt) -> Result<()> { resp => Err(anyhow!("{:?}", resp)) .with_context(|| format!("API GET failed: {:?}", ident)), }?; - download_file(&file_entity) + download_file(&file_entity, output_path) } other => Err(anyhow!("Don't know how to download: {:?}", other)), }?; |