-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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" -- <CLI ARGS> Replace `<CLI ARGS>` with arguments for the CLI. Use the `--help` flag to find out the API of the CLI. Signed-off-by: Andrew Lilley Brinker <[email protected]>
- Loading branch information
1 parent
78c2b3b
commit 3602382
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |