rust: Streamline and cleanup verbosity handling

Create one implementation for the verbose option to be used by all
tools. While at it, add a quiet option to decrease the verbosity.

Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
This commit is contained in:
Steffen Eiden
2024-11-07 11:03:41 +01:00
parent 576a230341
commit 53d803abf3
9 changed files with 125 additions and 123 deletions
+5 -4
View File
@@ -3,18 +3,19 @@
// Copyright IBM Corp. 2023
use clap::{ArgGroup, Args, CommandFactory, Parser, Subcommand, ValueEnum, ValueHint};
use utils::{CertificateOptions, STDOUT};
use utils::{CertificateOptions, DeprecatedVerbosityOptions, STDOUT};
/// Manage secrets for IBM Secure Execution guests.
///
/// Use to create and send add-secret requests, list the added secrets and lock the Secret Store.
#[derive(Parser, Debug)]
pub struct CliOptions {
/// Provide more detailed output.
#[arg(short='v', long, action = clap::ArgAction::Count, short_alias('V'))]
pub verbose: u8,
#[clap(flatten)]
pub verbosity: DeprecatedVerbosityOptions,
/// Print version information and exit.
// Implemented for the help message only. Actual parsing happens in the
// version command.
#[arg(long)]
pub version: bool,
+5 -16
View File
@@ -6,22 +6,15 @@ mod cli;
mod cmd;
use clap::{CommandFactory, Parser};
use cli::{CliOptions, Command};
use cli::{validate_cli, CliOptions, Command};
use log::trace;
use std::process::ExitCode;
use utils::{print_cli_error, print_error, print_version, PvLogger};
use crate::cli::validate_cli;
static LOGGER: PvLogger = PvLogger;
static EXIT_LOGGER: u8 = 3;
const FEATURES: &[&[&str]] = &[cmd::CMD_FN, cmd::UV_CMD_FN];
fn print_version(verbosity: u8) -> anyhow::Result<()> {
print_version!(verbosity, "2024", FEATURES.concat());
Ok(())
}
fn main() -> ExitCode {
let cli: CliOptions = match CliOptions::try_parse() {
Ok(cli) => match validate_cli(&cli) {
@@ -32,7 +25,8 @@ fn main() -> ExitCode {
};
// set up logger/std(out,err)
if let Err(e) = LOGGER.start(cli.verbose) {
let log_level = cli.verbosity.to_level_filter();
if let Err(e) = LOGGER.start(log_level) {
// should(TM) never happen
eprintln!("Logger error: {e:?}");
return EXIT_LOGGER.into();
@@ -42,23 +36,18 @@ fn main() -> ExitCode {
trace!("Trace verbosity, may leak secrets to command-line");
trace!("Options {cli:?}");
if cli.version {
let _ = print_version(cli.verbose);
return ExitCode::SUCCESS;
}
// perform the command selected by the user
let res = match &cli.cmd {
Command::Add(opt) => cmd::add(opt),
Command::List(opt) => cmd::list(opt),
Command::Lock => cmd::lock(),
Command::Create(opt) => cmd::create(opt),
Command::Version => print_version(cli.verbose),
Command::Version => Ok(print_version!("2024", log_level; FEATURES.concat())),
Command::Verify(opt) => cmd::verify(opt),
};
match res {
Ok(_) => ExitCode::SUCCESS,
Err(e) => print_error(&e, cli.verbose),
Err(e) => print_error(&e, log_level),
}
}