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 -64
View File
@@ -3,25 +3,19 @@
// Copyright IBM Corp. 2024
use clap::{Args, Parser, Subcommand, ValueEnum, ValueHint};
use log::warn;
use utils::CertificateOptions;
use utils::{CertificateOptions, DeprecatedVerbosityOptions};
/// create, perform, and verify attestation measurements
///
/// Create, perform, and verify attestation measurements for IBM Secure Execution guest systems.
#[derive(Parser, Debug)]
pub struct CliOptions {
/// Provide more detailed output
#[arg(short='v', long, action = clap::ArgAction::Count)]
verbose: u8,
/// Deprecated short verbose flag (-V) form the C implementation.
///
/// If specified a deprecation warning is emitted,
#[arg(short = 'V', hide = true, action = clap::ArgAction::Count)]
verbose_deprecated: 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,
@@ -29,29 +23,6 @@ pub struct CliOptions {
pub cmd: Command,
}
impl CliOptions {
pub fn verbosity(&self) -> u8 {
let verbose_deprecated = self.verbose_deprecated
+ match &self.cmd {
Command::Create(cmd) => cmd.verbose_deprecated,
Command::Perform(cmd) => cmd.verbose_deprecated,
Command::Verify(cmd) => cmd.verbose_deprecated,
Command::Version => 0,
};
if verbose_deprecated > 0 {
warn!("WARNING: Use of deprecated flag '-V'. Use '-v' or '--verbose' instead.")
}
verbose_deprecated
+ self.verbose
+ match &self.cmd {
Command::Create(cmd) => cmd.verbose,
Command::Perform(cmd) => cmd.verbose,
Command::Verify(cmd) => cmd.verbose,
Command::Version => 0,
}
}
}
#[derive(Subcommand, Debug)]
pub enum Command {
/// Create an attestation measurement request
@@ -107,16 +78,6 @@ pub struct CreateAttOpt {
/// Optional.
#[arg(long, value_name = "FLAGS")]
pub add_data: Vec<AttAddFlags>,
/// Provide more detailed output.
#[arg(short='v', long, action = clap::ArgAction::Count)]
verbose: u8,
/// Deprecated short verbose flag (-V) form the C implementation.
///
/// If specified a deprecation warning is emitted,
#[arg(short = 'V', hide = true, action = clap::ArgAction::Count)]
verbose_deprecated: u8,
}
#[derive(Debug, ValueEnum, Clone, Copy)]
@@ -158,16 +119,6 @@ pub struct PerformAttOpt {
/// May be any arbitrary data, as long as it is less or equal to 256 bytes
#[arg(short, long, value_name = "File", value_hint = ValueHint::FilePath,)]
pub user_data: Option<String>,
/// Provide more detailed output.
#[arg(short='v', long, action = clap::ArgAction::Count)]
verbose: u8,
/// Deprecated short verbose flag (-V) form the C implementation.
///
/// If specified a deprecation warning is emitted,
#[arg(short = 'V', hide = true, action = clap::ArgAction::Count)]
verbose_deprecated: u8,
}
#[cfg(target_arch = "s390x")]
@@ -237,16 +188,6 @@ pub struct VerifyOpt {
/// Emits a warning if the response contains no user-data
#[arg(long, short ,value_name = "FILE", value_hint = ValueHint::FilePath,)]
pub user_data: Option<String>,
/// Provide more detailed output.
#[arg(short='v', long, action = clap::ArgAction::Count)]
verbose: u8,
/// Deprecated short verbose flag (-V) form the C implementation.
///
/// If specified a deprecation warning is emitted,
#[arg(short = 'V', hide = true, action = clap::ArgAction::Count)]
verbose_deprecated: u8,
}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Debug, Default)]
+8 -10
View File
@@ -7,12 +7,11 @@ mod cmd;
mod exchange;
use clap::{CommandFactory, Parser};
use cli::CliOptions;
use cli::{CliOptions, Command};
use log::trace;
use std::process::ExitCode;
use utils::{print_cli_error, print_error, print_version, PvLogger};
use crate::cli::Command;
use crate::cmd::*;
static LOGGER: PvLogger = PvLogger;
@@ -20,11 +19,6 @@ const FEATURES: &[&[&str]] = &[cmd::CMD_FN, cmd::UV_CMD_FN];
const EXIT_CODE_ATTESTATION_FAIL: u8 = 2;
const EXIT_CODE_LOGGER_FAIL: u8 = 3;
fn print_version(verbosity: u8) -> anyhow::Result<ExitCode> {
print_version!(verbosity, "2024", FEATURES.concat());
Ok(ExitCode::SUCCESS)
}
fn main() -> ExitCode {
let cli: CliOptions = match CliOptions::try_parse() {
Ok(cli) => cli,
@@ -32,7 +26,8 @@ fn main() -> ExitCode {
};
// set up logger/stderr
if let Err(e) = LOGGER.start(cli.verbosity()) {
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_CODE_LOGGER_FAIL.into();
@@ -45,10 +40,13 @@ fn main() -> ExitCode {
Command::Create(opt) => create(opt),
Command::Perform(opt) => perform(opt),
Command::Verify(opt) => verify(opt),
Command::Version => print_version(cli.verbosity()),
Command::Version => {
print_version!("2024", log_level; FEATURES.concat());
Ok(ExitCode::SUCCESS)
}
};
match res {
Ok(c) => c,
Err(e) => print_error(&e, cli.verbosity()),
Err(e) => print_error(&e, log_level),
}
}