chore: Use get_env_var

This commit is contained in:
Gerald Pinder 2025-05-16 09:55:28 -04:00
parent 9c24675483
commit 4a0fc3b4a1
7 changed files with 37 additions and 32 deletions

View file

@ -1,5 +1,4 @@
use std::{
env,
ops::Not,
path::Path,
process::{Command, ExitStatus},
@ -8,6 +7,7 @@ use std::{
use blue_build_utils::{
constants::{BLUE_BUILD, DOCKER_HOST, GITHUB_ACTIONS},
credentials::Credentials,
get_env_var,
semver::Version,
string_vec,
};
@ -85,7 +85,7 @@ impl DockerDriver {
trace!("{ls_out}");
if !ls_out.lines().any(|line| line == BLUE_BUILD) {
let remote = env::var(DOCKER_HOST).is_ok();
let remote = get_env_var(DOCKER_HOST).is_ok();
if remote {
let context_list = get_context_list()?;
trace!("{context_list:#?}");
@ -410,10 +410,8 @@ impl BuildDriver for DockerDriver {
}
fn build_tag_push_cmd(opts: &BuildTagPushOpts<'_>, first_image: &str) -> Command {
// let remote = env::var(DOCKER_HOST).is_ok();
let c = cmd!(
"docker",
// if remote => format!("--context={BLUE_BUILD}0"),
"buildx",
format!("--builder={BLUE_BUILD}"),
"build",
@ -426,7 +424,7 @@ fn build_tag_push_cmd(opts: &BuildTagPushOpts<'_>, first_image: &str) -> Command
opts.compression
),
],
ImageRef::Remote(_remote) if env::var(GITHUB_ACTIONS).is_err() => "--load",
ImageRef::Remote(_remote) if get_env_var(GITHUB_ACTIONS).is_err() => "--load",
ImageRef::LocalTar(archive_path) => [
"--output",
format!("type=oci,dest={}", archive_path.display()),

View file

@ -1,8 +1,8 @@
use std::{env, path::Path};
use std::path::Path;
use blue_build_utils::{
constants::{BB_PRIVATE_KEY, COSIGN_PRIV_PATH, COSIGN_PRIVATE_KEY, COSIGN_PUB_PATH},
string,
get_env_var, string,
};
use miette::{Result, bail};
@ -17,8 +17,8 @@ where
Ok(
match (
path.join(COSIGN_PUB_PATH).exists(),
env::var(BB_PRIVATE_KEY).ok(),
env::var(COSIGN_PRIVATE_KEY).ok(),
get_env_var(BB_PRIVATE_KEY).ok(),
get_env_var(COSIGN_PRIVATE_KEY).ok(),
path.join(COSIGN_PRIV_PATH),
) {
(true, Some(private_key), _, _) if !private_key.is_empty() => {

View file

@ -1,9 +1,10 @@
use std::{
borrow::Cow,
env, fs,
fs,
path::{Path, PathBuf},
};
use blue_build_utils::get_env_var;
use bon::Builder;
use miette::{IntoDiagnostic, Result};
use oci_distribution::Reference;
@ -49,7 +50,7 @@ impl PrivateKeyContents<Vec<u8>> for PrivateKey {
impl PrivateKeyContents<String> for PrivateKey {
fn contents(&self) -> Result<Zeroizing<String>> {
Ok(Zeroizing::new(match *self {
Self::Env(ref env) => env::var(env).into_diagnostic()?,
Self::Env(ref env) => get_env_var(env)?,
Self::Path(ref path) => fs::read_to_string(path).into_diagnostic()?,
}))
}

View file

@ -1,12 +1,12 @@
use std::{
borrow::Cow,
collections::HashMap,
env,
path::{Path, PathBuf},
};
use blue_build_utils::{
constants::{GITHUB_ACTIONS, GITLAB_CI, IMAGE_VERSION_LABEL},
get_env_var,
semver::Version,
};
use clap::ValueEnum;
@ -175,7 +175,10 @@ impl DetermineDriver<CiDriverType> for Option<CiDriverType> {
trace!("CiDriverType::determine_driver()");
*self.get_or_insert(
match (env::var(GITLAB_CI).ok(), env::var(GITHUB_ACTIONS).ok()) {
match (
get_env_var(GITLAB_CI).ok(),
get_env_var(GITHUB_ACTIONS).ok(),
) {
(Some(_gitlab_ci), None) => CiDriverType::Gitlab,
(None, Some(_github_actions)) => CiDriverType::Github,
_ => CiDriverType::Local,

View file

@ -1,6 +1,5 @@
use std::{
borrow::Cow,
env,
fs::OpenOptions,
io::{BufRead, BufReader, Result, Write as IoWrite},
path::{Path, PathBuf},
@ -10,6 +9,7 @@ use std::{
time::Duration,
};
use blue_build_utils::get_env_var;
use bon::Builder;
use chrono::Local;
use colored::{ColoredString, Colorize, control::ShouldColorize};
@ -95,7 +95,7 @@ impl Logger {
/// # Panics
/// Will panic if logging is unable to be initialized.
pub fn init(&self) {
let home = env::var("HOME").expect("$HOME should be defined");
let home = get_env_var("HOME").expect("$HOME should be defined");
let log_dir = self.log_dir.as_ref().map_or_else(
|| Path::new(home.as_str()).join(".cache/bluebuild"),
Clone::clone,

View file

@ -1,8 +1,11 @@
use blue_build_recipe::Recipe;
use blue_build_template::{GithubIssueTemplate, Template};
use blue_build_utils::constants::{
BUG_REPORT_WARNING_MESSAGE, GITHUB_CHAR_LIMIT, LC_TERMINAL, LC_TERMINAL_VERSION, TERM_PROGRAM,
TERM_PROGRAM_VERSION, UNKNOWN_SHELL, UNKNOWN_TERMINAL, UNKNOWN_VERSION,
use blue_build_utils::{
constants::{
BUG_REPORT_WARNING_MESSAGE, GITHUB_CHAR_LIMIT, LC_TERMINAL, LC_TERMINAL_VERSION,
TERM_PROGRAM, TERM_PROGRAM_VERSION, UNKNOWN_SHELL, UNKNOWN_TERMINAL, UNKNOWN_VERSION,
},
get_env_var,
};
use bon::Builder;
use clap::Args;
@ -221,12 +224,12 @@ struct TerminalInfo {
}
fn get_terminal_info() -> TerminalInfo {
let terminal = std::env::var(TERM_PROGRAM)
.or_else(|_| std::env::var(LC_TERMINAL))
let terminal = get_env_var(TERM_PROGRAM)
.or_else(|_| get_env_var(LC_TERMINAL))
.unwrap_or_else(|_| UNKNOWN_TERMINAL.to_string());
let version = std::env::var(TERM_PROGRAM_VERSION)
.or_else(|_| std::env::var(LC_TERMINAL_VERSION))
let version = get_env_var(TERM_PROGRAM_VERSION)
.or_else(|_| get_env_var(LC_TERMINAL_VERSION))
.unwrap_or_else(|_| UNKNOWN_VERSION.to_string());
TerminalInfo {

View file

@ -1,7 +1,4 @@
use std::{
env,
sync::{LazyLock, Mutex},
};
use std::sync::{LazyLock, Mutex};
use bon::Builder;
use clap::Args;
@ -13,7 +10,7 @@ use crate::{
BB_PASSWORD, BB_REGISTRY, BB_USERNAME, CI_REGISTRY, CI_REGISTRY_PASSWORD, CI_REGISTRY_USER,
GITHUB_ACTIONS, GITHUB_ACTOR, GITHUB_TOKEN,
},
string,
get_env_var, string,
};
static INIT: LazyLock<Mutex<bool>> = LazyLock::new(|| Mutex::new(false));
@ -52,8 +49,8 @@ static ENV_CREDENTIALS: LazyLock<Option<Credentials>> = LazyLock::new(|| {
let registry = match (
registry,
env::var(CI_REGISTRY).ok(),
env::var(GITHUB_ACTIONS).ok(),
get_env_var(CI_REGISTRY).ok(),
get_env_var(GITHUB_ACTIONS).ok(),
) {
(Some(registry), _, _) | (_, Some(registry), _) if !registry.is_empty() => registry,
(_, _, Some(_)) => string!("ghcr.io"),
@ -66,10 +63,13 @@ static ENV_CREDENTIALS: LazyLock<Option<Credentials>> = LazyLock::new(|| {
docker_credential::get_credential(&registry).ok(),
docker_credential::get_podman_credential(&registry).ok(),
(
env::var(CI_REGISTRY_USER).ok(),
env::var(CI_REGISTRY_PASSWORD).ok(),
get_env_var(CI_REGISTRY_USER).ok(),
get_env_var(CI_REGISTRY_PASSWORD).ok(),
),
(
get_env_var(GITHUB_ACTOR).ok(),
get_env_var(GITHUB_TOKEN).ok(),
),
(env::var(GITHUB_ACTOR).ok(), env::var(GITHUB_TOKEN).ok()),
) {
((Some(username), Some(password)), _, _, _, _)
| (_, Some(DockerCredential::UsernamePassword(username, password)), _, _, _)