feat: add cli addressing from `sha256sum` output
parent
a72b871185
commit
7f889be0db
|
@ -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),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue