Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce new OmniBOR CLI. #117

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions omnibor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
67 changes: 67 additions & 0 deletions omnibor/src/bin/omnibor.rs
Original file line number Diff line number Diff line change
@@ -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<Sha256>;

/// 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(())
}