Skip to content

Commit

Permalink
Skip torrent file creation with imdl torrent create --dry-run
Browse files Browse the repository at this point in the history
Torrent files will not be written to disk with `--dry-run`:

    imdl torrrent create --input foo --dry-run

type: added
  • Loading branch information
casey committed Apr 8, 2020
1 parent 7e3a53c commit 1c84172
Showing 1 changed file with 42 additions and 15 deletions.
57 changes: 42 additions & 15 deletions src/subcommand/torrent/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ pub(crate) struct Create {
comment: Option<String>,
#[structopt(
long = "node",
short = "n",
value_name = "NODE",
help = "Add DHT bootstrap node `NODE` to torrent. `NODE` should be in the form `HOST:PORT`, \
where `HOST` is a domain name, an IPv4 address, or an IPv6 address surrounded by \
Expand All @@ -69,6 +68,12 @@ pub(crate) struct Create {
`--node [2001:db8:4275:7920:6269:7463:6f69:6e21]:8832`"
)]
dht_nodes: Vec<HostPort>,
#[structopt(
long = "dry-run",
short = "n",
help = "Skip writing `.torrent` file to disk."
)]
dry_run: bool,
#[structopt(
long = "follow-symlinks",
short = "F",
Expand Down Expand Up @@ -369,22 +374,24 @@ impl Create {

let bytes = metainfo.serialize()?;

match &output {
OutputTarget::File(path) => {
let mut open_options = fs::OpenOptions::new();

if self.force {
open_options.write(true).create(true).truncate(true);
} else {
open_options.write(true).create_new(true);
if !self.dry_run {
match &output {
OutputTarget::File(path) => {
let mut open_options = fs::OpenOptions::new();

if self.force {
open_options.write(true).create(true).truncate(true);
} else {
open_options.write(true).create_new(true);
}

open_options
.open(path)
.and_then(|mut file| file.write_all(&bytes))
.context(error::Filesystem { path })?;
}

open_options
.open(path)
.and_then(|mut file| file.write_all(&bytes))
.context(error::Filesystem { path })?;
OutputTarget::Stdout => env.out_mut().write_all(&bytes).context(error::Stdout)?,
}
OutputTarget::Stdout => env.out_mut().write_all(&bytes).context(error::Stdout)?,
}

#[cfg(test)]
Expand Down Expand Up @@ -2420,4 +2427,24 @@ Content Size 9 bytes
666\n"
);
}

#[test]
fn dry_run_skips_torrent_file_creation() {
let mut env = test_env! {
args: [
"torrent",
"create",
"--input",
"foo",
"--dry-run",
],
tree: {
foo: "",
}
};
assert_matches!(env.run(), Ok(()));
let torrent = env.resolve("foo.torrent");
let err = fs::read(torrent).unwrap_err();
assert_eq!(err.kind(), io::ErrorKind::NotFound);
}
}

0 comments on commit 1c84172

Please sign in to comment.