From 3602382af3808a0ed84d50afe2568bffd51871ee Mon Sep 17 00:00:00 2001 From: Andrew Lilley Brinker Date: Wed, 21 Feb 2024 10:05:37 -0800 Subject: [PATCH] feat: introduce new OmniBOR CLI. (#117) This commit introduces a new, and currently very basic, OmniBOR CLI. At the moment, this CLI is only able to produce Artifact Identifiers for files it's given. In the future it'll be expanded with more functionality. To use the CLI during development, run: cargo run --features="build-binary" -- Replace `` with arguments for the CLI. Use the `--help` flag to find out the API of the CLI. Signed-off-by: Andrew Lilley Brinker --- omnibor/Cargo.toml | 11 +++++++ omnibor/src/bin/omnibor.rs | 67 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 omnibor/src/bin/omnibor.rs diff --git a/omnibor/Cargo.toml b/omnibor/Cargo.toml index 3b37c61..557de02 100644 --- a/omnibor/Cargo.toml +++ b/omnibor/Cargo.toml @@ -14,7 +14,18 @@ version = "0.3.0" gitoid = "0.5.0" tokio = { version = "1.36.0", features = ["io-util"] } url = "2.5.0" +clap = { version = "4.5.1", features = ["derive"], optional = true } +anyhow = { version = "1.0.80", optional = true } [dev-dependencies] tokio = { version = "1.36.0", features = ["io-util", "fs"] } tokio-test = "0.4.3" + +[features] +build-binary = ["anyhow", "clap"] + +[[bin]] +name = "omnibor" +test = false +bench = false +required-features = ["build-binary"] diff --git a/omnibor/src/bin/omnibor.rs b/omnibor/src/bin/omnibor.rs new file mode 100644 index 0000000..cec4aa5 --- /dev/null +++ b/omnibor/src/bin/omnibor.rs @@ -0,0 +1,67 @@ +use anyhow::Context as _; +use anyhow::Result; +use clap::Args; +use clap::Parser; +use clap::Subcommand; +use omnibor::Sha256; +use std::fs::File; +use std::path::PathBuf; +use std::process::ExitCode; + +fn main() -> ExitCode { + let args = Cli::parse(); + + let result = match args.command { + Command::Id(args) => run_id(args), + }; + + if let Err(err) = result { + eprintln!("{}", err); + return ExitCode::FAILURE; + } + + ExitCode::SUCCESS +} + + +/*=========================================================================== + * CLI Arguments + *-------------------------------------------------------------------------*/ + +#[derive(Debug, Parser)] +#[command(version)] +struct Cli { + #[command(subcommand)] + command: Command, +} + +#[derive(Debug, Subcommand)] +enum Command { + /// Print the Artifact ID of the path given. + Id(IdArgs), +} + +#[derive(Debug, Args)] +struct IdArgs { + /// The path to identify. + path: PathBuf, +} + + +/*=========================================================================== + * Command Implementations + *-------------------------------------------------------------------------*/ + +/// Type alias for the specific ID we're using. +type ArtifactId = omnibor::ArtifactId; + +/// Run the `id` subcommand. +/// +/// This command just produces the `gitoid` URL for the given file. +fn run_id(args: IdArgs) -> Result<()> { + let path = &args.path; + let file = File::open(path).with_context(|| format!("failed to open '{}'", path.display()))?; + let id = ArtifactId::id_reader(&file).context("failed to produce Artifact ID")?; + println!("{}", id.url()); + Ok(()) +}