Show information about torrents after creation

type: changed
This commit is contained in:
Casey Rodarmor 2020-02-04 08:36:00 -08:00
parent 3971854eaa
commit b9ca02fbaa
No known key found for this signature in database
GPG Key ID: 556186B153EC6FE0
4 changed files with 68 additions and 16 deletions

View File

@ -32,4 +32,8 @@ impl Metainfo {
let path = path.as_ref();
serde_bencode::de::from_bytes(&bytes).context(error::MetainfoLoad { path })
}
pub(crate) fn serialize(&self) -> Result<Vec<u8>, Error> {
serde_bencode::ser::to_bytes(&self).context(error::MetainfoSerialize)
}
}

View File

@ -130,7 +130,7 @@ impl Linter {
}
impl Create {
pub(crate) fn run(self, env: &Env) -> Result<(), Error> {
pub(crate) fn run(self, env: &mut Env) -> Result<(), Error> {
let mut linter = Linter::new();
linter.allow(self.allowed_lints.iter().cloned());
@ -239,7 +239,11 @@ impl Create {
info,
};
metainfo.dump(&output)?;
let bytes = metainfo.serialize()?;
fs::write(&output, &bytes).context(error::Filesystem { path: &output })?;
TorrentSummary::from_metainfo(metainfo)?.write(env)?;
if self.open {
Platform::open(&output)?;
@ -253,6 +257,8 @@ impl Create {
mod tests {
use super::*;
use pretty_assertions::assert_eq;
fn environment(args: &[&str]) -> TestEnv {
testing::env(["torrent", "create"].iter().chain(args).cloned())
}
@ -855,4 +861,33 @@ mod tests {
fs::create_dir(&dir).unwrap();
env.run().unwrap();
}
#[test]
fn output() {
let mut env = environment(&[
"--input",
"foo",
"--announce",
"http://bar",
"--no-creation-date",
]);
let dir = env.resolve("foo");
fs::create_dir(&dir).unwrap();
fs::write(dir.join("a"), "abc").unwrap();
fs::write(dir.join("x"), "xyz").unwrap();
fs::write(dir.join("h"), "hij").unwrap();
env.run().unwrap();
let have = env.out();
let want = " Name foo
Info Hash 8197efe97f10f50f249e8d5c63eb5c0d4e1d9b49
Torrent Size 166 bytes
Content Size 0 bytes
Private no
Tracker http://bar/
Piece Size 512 KiB
Piece Count 1
File Count 0
";
assert_eq!(have, want);
}
}

View File

@ -19,11 +19,7 @@ impl Show {
pub(crate) fn run(self, env: &mut Env) -> Result<(), Error> {
let summary = TorrentSummary::load(&env.resolve(self.input))?;
let table = summary.table();
table
.write_human_readable(&mut env.out)
.context(error::Stdout)?;
summary.write(env)?;
Ok(())
}

View File

@ -7,11 +7,7 @@ pub(crate) struct TorrentSummary {
}
impl TorrentSummary {
pub(crate) fn load(path: &Path) -> Result<Self, Error> {
let bytes = fs::read(path).context(error::Filesystem { path })?;
let metainfo = Metainfo::deserialize(path, &bytes)?;
fn new(bytes: &[u8], metainfo: Metainfo) -> Result<Self, Error> {
let value = bencode::Value::decode(&bytes).unwrap();
let infohash = if let bencode::Value::Dict(items) = value {
@ -26,16 +22,37 @@ impl TorrentSummary {
unreachable!()
};
let metadata = path.metadata().context(error::Filesystem { path })?;
Ok(Self {
size: Bytes(metadata.len().into()),
size: Bytes::from(bytes.len().into_u64()),
infohash,
metainfo,
})
}
pub(crate) fn table(&self) -> Table {
pub(crate) fn from_metainfo(metainfo: Metainfo) -> Result<Self, Error> {
let bytes = metainfo.serialize()?;
Self::new(&bytes, metainfo)
}
pub(crate) fn load(path: &Path) -> Result<Self, Error> {
let bytes = fs::read(path).context(error::Filesystem { path })?;
let metainfo = Metainfo::deserialize(path, &bytes)?;
Self::new(&bytes, metainfo)
}
pub(crate) fn write(&self, env: &mut Env) -> Result<(), Error> {
let table = self.table();
table
.write_human_readable(&mut env.out)
.context(error::Stdout)?;
Ok(())
}
fn table(&self) -> Table {
let mut table = Table::new();
table.row("Name", &self.metainfo.info.name);