From e3acd4592019f00ed034b55329f8c9cdd92a59c5 Mon Sep 17 00:00:00 2001 From: Philipp Schuster Date: Wed, 7 May 2025 08:12:00 +0200 Subject: [PATCH] xtask: simplify env variables --- xtask/Cargo.toml | 2 +- xtask/src/opt.rs | 6 +++--- xtask/src/qemu.rs | 35 +++++------------------------------ 3 files changed, 9 insertions(+), 34 deletions(-) diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index c9ac855c0..64b8b9db6 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -7,7 +7,7 @@ edition.workspace = true [dependencies] anyhow = "1.0.51" -clap = { version = "4.4.0", default-features = false, features = ["derive", "help", "usage", "std"] } +clap = { version = "4.4.0", default-features = false, features = ["derive", "help", "usage", "std", "env"] } fatfs = { version = "0.3.6", default-features = false, features = ["alloc", "std"] } fs-err = "3.0.0" heck = "0.5.0" diff --git a/xtask/src/opt.rs b/xtask/src/opt.rs index 09350643c..93a3c40f8 100644 --- a/xtask/src/opt.rs +++ b/xtask/src/opt.rs @@ -187,15 +187,15 @@ pub struct QemuOpt { pub tpm: Option, /// Path of an OVMF code file. - #[clap(long, action)] + #[clap(long, action, env)] pub ovmf_code: Option, /// Path of an OVMF vars file. - #[clap(long, action)] + #[clap(long, action, env)] pub ovmf_vars: Option, /// Path of an OVMF shell application. - #[clap(long, action)] + #[clap(long, action, env)] pub ovmf_shell: Option, /// Run an example instead of the main binary. diff --git a/xtask/src/qemu.rs b/xtask/src/qemu.rs index 8f74cee63..e40cdc2ed 100644 --- a/xtask/src/qemu.rs +++ b/xtask/src/qemu.rs @@ -26,15 +26,6 @@ const OVMF_PREBUILT_SOURCE: Source = Source::EDK2_STABLE202502_R2; /// Directory into which the prebuilts will be download (relative to the repo root). const OVMF_PREBUILT_DIR: &str = "target/ovmf"; -/// Environment variable for overriding the path of the OVMF code file. -const ENV_VAR_OVMF_CODE: &str = "OVMF_CODE"; - -/// Environment variable for overriding the path of the OVMF vars file. -const ENV_VAR_OVMF_VARS: &str = "OVMF_VARS"; - -/// Environment variable for overriding the path of the OVMF shell file. -const ENV_VAR_OVMF_SHELL: &str = "OVMF_SHELL"; - impl From for ovmf_prebuilt::Arch { fn from(arch: UefiArch) -> Self { match arch { @@ -47,29 +38,13 @@ impl From for ovmf_prebuilt::Arch { /// Get a user-provided path for the given OVMF file type. /// -/// This uses the command-line arg if present, otherwise it falls back to an -/// environment variable. If neither is present, returns `None`. +/// This can come from an explicit CLI arg or an environment variable, depending +/// on how clap received and parsed the value. fn get_user_provided_path(file_type: FileType, opt: &QemuOpt) -> Option { - let opt_path; - let var_name; match file_type { - FileType::Code => { - opt_path = &opt.ovmf_code; - var_name = ENV_VAR_OVMF_CODE; - } - FileType::Vars => { - opt_path = &opt.ovmf_vars; - var_name = ENV_VAR_OVMF_VARS; - } - FileType::Shell => { - opt_path = &opt.ovmf_shell; - var_name = ENV_VAR_OVMF_SHELL; - } - } - if let Some(path) = opt_path { - Some(path.clone()) - } else { - env::var_os(var_name).map(PathBuf::from) + FileType::Code => opt.ovmf_code.clone(), + FileType::Vars => opt.ovmf_vars.clone(), + FileType::Shell => opt.ovmf_shell.clone(), } }