Show information about torrents after creation
type: changed
This commit is contained in:
parent
3971854eaa
commit
b9ca02fbaa
|
@ -32,4 +32,8 @@ impl Metainfo {
|
||||||
let path = path.as_ref();
|
let path = path.as_ref();
|
||||||
serde_bencode::de::from_bytes(&bytes).context(error::MetainfoLoad { path })
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -130,7 +130,7 @@ impl Linter {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Create {
|
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();
|
let mut linter = Linter::new();
|
||||||
linter.allow(self.allowed_lints.iter().cloned());
|
linter.allow(self.allowed_lints.iter().cloned());
|
||||||
|
|
||||||
|
@ -239,7 +239,11 @@ impl Create {
|
||||||
info,
|
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 {
|
if self.open {
|
||||||
Platform::open(&output)?;
|
Platform::open(&output)?;
|
||||||
|
@ -253,6 +257,8 @@ impl Create {
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
use pretty_assertions::assert_eq;
|
||||||
|
|
||||||
fn environment(args: &[&str]) -> TestEnv {
|
fn environment(args: &[&str]) -> TestEnv {
|
||||||
testing::env(["torrent", "create"].iter().chain(args).cloned())
|
testing::env(["torrent", "create"].iter().chain(args).cloned())
|
||||||
}
|
}
|
||||||
|
@ -855,4 +861,33 @@ mod tests {
|
||||||
fs::create_dir(&dir).unwrap();
|
fs::create_dir(&dir).unwrap();
|
||||||
env.run().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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,11 +19,7 @@ impl Show {
|
||||||
pub(crate) fn run(self, env: &mut Env) -> Result<(), Error> {
|
pub(crate) fn run(self, env: &mut Env) -> Result<(), Error> {
|
||||||
let summary = TorrentSummary::load(&env.resolve(self.input))?;
|
let summary = TorrentSummary::load(&env.resolve(self.input))?;
|
||||||
|
|
||||||
let table = summary.table();
|
summary.write(env)?;
|
||||||
|
|
||||||
table
|
|
||||||
.write_human_readable(&mut env.out)
|
|
||||||
.context(error::Stdout)?;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,11 +7,7 @@ pub(crate) struct TorrentSummary {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TorrentSummary {
|
impl TorrentSummary {
|
||||||
pub(crate) fn load(path: &Path) -> Result<Self, Error> {
|
fn new(bytes: &[u8], metainfo: Metainfo) -> Result<Self, Error> {
|
||||||
let bytes = fs::read(path).context(error::Filesystem { path })?;
|
|
||||||
|
|
||||||
let metainfo = Metainfo::deserialize(path, &bytes)?;
|
|
||||||
|
|
||||||
let value = bencode::Value::decode(&bytes).unwrap();
|
let value = bencode::Value::decode(&bytes).unwrap();
|
||||||
|
|
||||||
let infohash = if let bencode::Value::Dict(items) = value {
|
let infohash = if let bencode::Value::Dict(items) = value {
|
||||||
|
@ -26,16 +22,37 @@ impl TorrentSummary {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
};
|
};
|
||||||
|
|
||||||
let metadata = path.metadata().context(error::Filesystem { path })?;
|
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
size: Bytes(metadata.len().into()),
|
size: Bytes::from(bytes.len().into_u64()),
|
||||||
infohash,
|
infohash,
|
||||||
metainfo,
|
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();
|
let mut table = Table::new();
|
||||||
|
|
||||||
table.row("Name", &self.metainfo.info.name);
|
table.row("Name", &self.metainfo.info.name);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user