feat: add cli addressing from `sha256sum` output

feat/type-attributes
Tomáš Mládek 2023-01-04 21:41:33 +01:00
parent a72b871185
commit 7f889be0db
1 changed files with 28 additions and 9 deletions

View File

@ -45,8 +45,10 @@ enum Commands {
},
/// Get the address of a file, attribute or URL.
Address {
/// Path to a file.
filepath: PathBuf,
/// Type of input to be addressed
_type: AddressType,
/// Path to a file, hash...
input: String,
},
}
@ -57,6 +59,14 @@ enum OutputFormat {
Raw,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug, ValueEnum)]
enum AddressType {
/// Hash a file and output its address.
File,
/// Compute an address from the output of `sha256sum`
Sha256sum,
}
fn main() -> anyhow::Result<()> {
env_logger::init();
let args = Args::parse();
@ -117,15 +127,24 @@ fn main() -> anyhow::Result<()> {
}
}
}
Some(Commands::Address { filepath }) => {
debug!("Hashing {:?}...", filepath);
let fbuffer = FileBuffer::open(&filepath)?;
let digest = common::hash(&fbuffer);
trace!("Finished hashing {:?}...", &filepath);
let address = common::Address::Hash(digest);
Some(Commands::Address { _type, input }) => {
let address = match _type {
AddressType::File => {
let filepath = PathBuf::from(input);
debug!("Hashing {:?}...", filepath);
let fbuffer = FileBuffer::open(&filepath)?;
let digest = common::hash(&fbuffer);
trace!("Finished hashing {:?}...", &filepath);
common::Address::Hash(digest)
}
AddressType::Sha256sum => {
let digest = multibase::Base::Base16Lower.decode(input)?;
common::Address::Hash(common::Hash(digest))
}
};
match format {
OutputFormat::Json => todo!(),
OutputFormat::Json => println!("\"{}\"", address),
OutputFormat::Tsv | OutputFormat::Raw => println!("{}", address),
}
}