rust/pvsecret: Streamline arch dependend code

Get rid of all arch barriers in main.rs. cmd.rs handles the arch
barriers for the individual commands. Simplifies main.rs & cmd.rs and
makes it easier to read and understand the code.

Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
Reviewed-by: Julian Ruess <julianr@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
Steffen Eiden
2024-02-16 11:02:59 +01:00
committed by Jan Höppner
parent c88a8b6130
commit 7c2ae3d2e8
2 changed files with 31 additions and 35 deletions

View File

@@ -8,18 +8,39 @@ pub use create::create;
mod verify;
pub use verify::verify;
// Commands (directly) related to UVCs are only available on s389x
pub const CMD_FN: &[&str] = &["+create", "+verify"];
#[cfg(target_arch = "s390x")]
mod add;
#[cfg(target_arch = "s390x")]
pub use add::add;
#[cfg(target_arch = "s390x")]
mod list;
#[cfg(target_arch = "s390x")]
pub use list::list;
#[cfg(target_arch = "s390x")]
mod lock;
// Commands (directly) related to UVCs are only available on s389x
#[cfg(target_arch = "s390x")]
pub use lock::lock;
mod uv_cmd {
pub use super::*;
pub use add::add;
pub use list::list;
pub use lock::lock;
pub const UV_CMD_FN: &[&str] = &["+add", "+lock", "+list"];
}
#[cfg(not(target_arch = "s390x"))]
mod uv_cmd {
use crate::cli::{AddSecretOpt, ListSecretOpt};
use anyhow::{bail, Result};
macro_rules! not_supp {
($name: ident $( ,$opt: ty )?) => {
pub fn $name($(_: &$opt)?) -> Result<()> {
bail!("Command only available on s390x")
}
};
}
not_supp!(add, AddSecretOpt);
not_supp!(list, ListSecretOpt);
not_supp!(lock);
pub const UV_CMD_FN: &[&str] = &[];
}
pub use uv_cmd::*;

View File

@@ -17,16 +17,7 @@ use crate::cli::validate_cli;
static LOGGER: PvLogger = PvLogger;
static EXIT_LOGGER: u8 = 3;
const FEATURES: &[&str] = &[
"+create",
#[cfg(target_arch = "s390x")]
"+add",
#[cfg(target_arch = "s390x")]
"+lock",
#[cfg(target_arch = "s390x")]
"+list",
"+verify",
];
const FEATURES: &[&[&str]] = &[cmd::CMD_FN, cmd::UV_CMD_FN];
fn print_error(e: anyhow::Error, verbosity: u8) -> ExitCode {
if verbosity > 0 {
@@ -57,7 +48,7 @@ fn print_version(verbosity: u8) -> anyhow::Result<()> {
release_string!()
);
if verbosity > 0 {
FEATURES.iter().for_each(|f| print!("{f} "));
FEATURES.concat().iter().for_each(|f| print!("{f} "));
println!("(compiled)");
println!(
"\n{}-crate {}",
@@ -69,12 +60,6 @@ fn print_version(verbosity: u8) -> anyhow::Result<()> {
Ok(())
}
#[cfg(not(target_arch = "s390x"))]
fn not_supported() -> anyhow::Result<()> {
use anyhow::bail;
bail!("Command only available on s390x")
}
fn main() -> ExitCode {
let cli: CliOptions = match CliOptions::try_parse() {
Ok(cli) => match validate_cli(&cli) {
@@ -102,19 +87,9 @@ fn main() -> ExitCode {
// perform the command selected by the user
let res = match &cli.cmd {
#[cfg(target_arch = "s390x")]
Command::Add(opt) => cmd::add(opt),
#[cfg(target_arch = "s390x")]
Command::List(opt) => cmd::list(opt),
#[cfg(target_arch = "s390x")]
Command::Lock => cmd::lock(),
#[cfg(not(target_arch = "s390x"))]
Command::Add(_) => not_supported(),
#[cfg(not(target_arch = "s390x"))]
Command::List(_) => not_supported(),
#[cfg(not(target_arch = "s390x"))]
Command::Lock => not_supported(),
Command::Create(opt) => cmd::create(opt),
Command::Version => print_version(cli.verbose),
Command::Verify(opt) => cmd::verify(opt),