Skip to content

Commit

Permalink
Merge pull request #3229 from stmcginnis/argharghargh
Browse files Browse the repository at this point in the history
Standardize `sources/` binaries on argh for command line parsing
  • Loading branch information
stmcginnis authored Jul 6, 2023
2 parents dd71ac0 + 49e44f6 commit 99ad494
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 159 deletions.
69 changes: 3 additions & 66 deletions sources/Cargo.lock

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

10 changes: 8 additions & 2 deletions sources/deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,27 @@ license-files = [
multiple-versions = "deny"
wildcards = "deny"

deny = [
{ name = "structopt" },
{ name = "clap", wrappers = [ "cargo-readme" ] },
]

skip = [
]

skip-tree = [
# hyper-proxy is using an older hyper-rustls
{ name = "hyper-proxy", version = "=0.9.1" },
# structopt pulls in an older version of clap
{ name = "structopt", version = "=0.3.26" },
# tungstenite is using an older sha-1
{ name = "tungstenite", version = "=0.16" },
# windows-sys is not a direct dependency. mio and schannel
# are using different versions of windows-sys. we skip the
# dependency tree because windows-sys has many sub-crates
# that differ in major version.
{ name = "windows-sys", version = "=0.42.0" },
# generate-readme pulls in an older clap that causes some duplicate
# dependencies
{ name = "generate-readme", version = "=0.1.0" }
]

[sources]
Expand Down
2 changes: 1 addition & 1 deletion sources/metricdog/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ publish = false
exclude = ["README.md"]

[dependencies]
argh = "0.1"
bottlerocket-release = { path = "../bottlerocket-release", version = "0.1" }
log = "0.4"
reqwest = { version = "0.11", default-features = false, features = ["blocking", "rustls-tls-native-roots"] }
serde = { version = "1", features = ["derive"] }
simplelog = "0.12"
snafu = { version = "0.7" }
structopt = "0.3"
toml = "0.5"
url = "2"

Expand Down
49 changes: 31 additions & 18 deletions sources/metricdog/src/args.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
use argh::FromArgs;
use log::LevelFilter;
use std::path::PathBuf;
use structopt::StructOpt;

fn default_logging() -> LevelFilter {
LevelFilter::Info
}

/// Command line arguments for the metricdog program.
#[derive(StructOpt)]
#[derive(FromArgs)]
pub(crate) struct Arguments {
/// Path to the TOML config file [default: /etc/metricdog]
#[structopt(short = "c", long = "config")]
pub(crate) config: Option<PathBuf>,
/// Logging verbosity [trace|debug|info|warn|error]
#[structopt(short = "l", long = "log-level", default_value = "info")]
pub(crate) log_level: LevelFilter,
/// Path to the os-release file [default: /etc/os-release]
#[structopt(short = "o", long = "os-release")]
pub(crate) os_release: Option<PathBuf>,
#[structopt(subcommand)]
pub(crate) command: Command,
/// path to the TOML config file [default: /etc/metricdog]
#[argh(option, short = 'c', long = "config")]
pub config: Option<PathBuf>,
/// logging verbosity [trace|debug|info|warn|error]
#[argh(option, short = 'l', long = "log-level", default = "default_logging()")]
pub log_level: LevelFilter,
/// path to the os-release file [default: /etc/os-release]
#[argh(option, short = 'o', long = "os-release")]
pub os_release: Option<PathBuf>,
#[argh(subcommand)]
pub command: Command,
}

#[derive(Debug, StructOpt)]
#[derive(FromArgs)]
#[argh(subcommand)]
pub(crate) enum Command {
/// report a successful boot.
SendBootSuccess,
/// check services and report their health.
SendHealthPing,
SendBootSuccess(SendBootSuccess),
SendHealthPing(SendHealthPing),
}

#[derive(FromArgs)]
#[argh(subcommand, name = "send-boot-success")]
/// report a successful boot
pub(crate) struct SendBootSuccess {}

#[derive(FromArgs)]
#[argh(subcommand, name = "send-health-ping")]
/// check services and report their health
pub(crate) struct SendHealthPing {}
7 changes: 3 additions & 4 deletions sources/metricdog/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,9 @@ use log::error;
use simplelog::{Config as LogConfig, SimpleLogger};
use snafu::ResultExt;
use std::process;
use structopt::StructOpt;

fn main() -> ! {
let args = Arguments::from_args();
let args: Arguments = argh::from_env();
SimpleLogger::init(args.log_level, LogConfig::default()).expect("unable to configure logger");
process::exit(match main_inner(args, Box::new(SystemdCheck {})) {
Ok(()) => 0,
Expand Down Expand Up @@ -117,14 +116,14 @@ pub(crate) fn main_inner(arguments: Arguments, service_check: Box<dyn ServiceChe

// execute the specified command
match arguments.command {
Command::SendBootSuccess => {
Command::SendBootSuccess(_) => {
if let Err(err) = metricdog.send_boot_success() {
// we don't want to fail the boot if there is a failure to send this message, so
// we log the error and return Ok(())
error!("Error while reporting boot success: {}", err);
}
}
Command::SendHealthPing => {
Command::SendHealthPing(_) => {
metricdog.send_health_ping()?;
}
}
Expand Down
12 changes: 6 additions & 6 deletions sources/metricdog/src/main_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::args::{Arguments, Command};
use crate::args::{Arguments, Command, SendBootSuccess, SendHealthPing};
use crate::error::Result;
use crate::main_inner;
use crate::service_check::{ServiceCheck, ServiceHealth};
Expand Down Expand Up @@ -94,7 +94,7 @@ fn send_boot_success() {
config: Some(config_path(&tempdir)),
log_level: LevelFilter::Off,
os_release: Some(os_release_path(&tempdir)),
command: Command::SendBootSuccess,
command: Command::SendBootSuccess(SendBootSuccess {}),
};
main_inner(args, Box::new(MockCheck {})).unwrap();
}
Expand All @@ -115,7 +115,7 @@ fn opt_out() {
config: Some(config_path(&tempdir)),
log_level: LevelFilter::Off,
os_release: Some(os_release_path(&tempdir)),
command: Command::SendBootSuccess,
command: Command::SendBootSuccess(SendBootSuccess {}),
};
main_inner(args, Box::new(MockCheck {})).unwrap();
}
Expand All @@ -129,7 +129,7 @@ fn send_boot_success_no_server() {
config: Some(config_path(&tempdir)),
log_level: LevelFilter::Off,
os_release: Some(os_release_path(&tempdir)),
command: Command::SendBootSuccess,
command: Command::SendBootSuccess(SendBootSuccess {}),
};
main_inner(args, Box::new(MockCheck {})).unwrap();
}
Expand All @@ -148,7 +148,7 @@ fn send_boot_success_404() {
config: Some(config_path(&tempdir)),
log_level: LevelFilter::Off,
os_release: Some(os_release_path(&tempdir)),
command: Command::SendBootSuccess,
command: Command::SendBootSuccess(SendBootSuccess {}),
};
main_inner(args, Box::new(MockCheck {})).unwrap();
}
Expand All @@ -169,7 +169,7 @@ fn send_health_ping() {
config: Some(config_path(&tempdir)),
log_level: LevelFilter::Off,
os_release: Some(os_release_path(&tempdir)),
command: Command::SendHealthPing,
command: Command::SendHealthPing(SendHealthPing {}),
};
main_inner(args, Box::new(MockCheck {})).unwrap();
}
2 changes: 1 addition & 1 deletion sources/updater/updog/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ publish = false
exclude = ["README.md"]

[dependencies]
argh = "0.1"
bottlerocket-release = { path = "../../bottlerocket-release", version = "0.1" }
chrono = { version = "0.4", default-features = false, features = ["std", "clock"] }
log = "0.4"
Expand All @@ -25,7 +26,6 @@ snafu = "0.7"
toml = "0.5"
tough = { version = "0.13", features = ["http"] }
update_metadata = { path = "../update_metadata", version = "0.1" }
structopt = "0.3"
url = "2"
signal-hook = "0.3"
models = { path = "../../models", version = "0.1" }
Expand Down
Loading

0 comments on commit 99ad494

Please sign in to comment.