Skip to content

Commit

Permalink
feat(cli): Add clap
Browse files Browse the repository at this point in the history
  • Loading branch information
evanpurkhiser committed May 8, 2024
1 parent c1cfcff commit 746302e
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 10 deletions.
183 changes: 183 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

anyhow = "1.0.66"
clap = { version = "4.4.6" }
14 changes: 14 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use crate::cliapp::make_app;
use anyhow::Result;

pub fn execute() -> Result<()> {
let app = make_app();
let matches = app.get_matches();

if let Some(_matches) = matches.subcommand_matches("run") {
println!("Hello world");
Ok(())
} else {
unreachable!();
}
}
30 changes: 30 additions & 0 deletions src/cliapp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//! This module implements the definition of the command line app.

use clap::builder::ValueParser;
use clap::{Arg, Command, ValueHint};

pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub const ABOUT: &str = "The Sentry uptime checker service.";

pub fn make_app() -> Command {
Command::new("relay")
.disable_help_subcommand(true)
.subcommand_required(true)
.propagate_version(true)
.version(VERSION)
.about(ABOUT)
.arg(
Arg::new("config")
.long("config")
.short('c')
.global(true)
.value_hint(ValueHint::DirPath)
.value_parser(ValueParser::path_buf())
.help("The path to the config file."),
)
.subcommand(
Command::new("run")
.about("Run the service")
.after_help("This runs the uptime-checker in the foreground until it's shut down."),
)
}
23 changes: 14 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
fn main() {
println!("Hello, world!");
}
mod cli;
mod cliapp;

use std::process;

#[cfg(test)]
mod tests {
#[test]
fn test_addition() {
assert_eq!(1 + 1, 2);
}
pub fn main() {
let exit_code = match cli::execute() {
Ok(()) => 0,
Err(_err) => {
// TODO(epurkhiser): capture error? Here's what relay did
// relay_log::ensure_error(&err);
1
}
};
process::exit(exit_code);
}

0 comments on commit 746302e

Please sign in to comment.