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::{ use std::{
env,
ops::Not, ops::Not,
path::Path, path::Path,
process::{Command, ExitStatus}, process::{Command, ExitStatus},
@ -8,6 +7,7 @@ use std::{
use blue_build_utils::{ use blue_build_utils::{
constants::{BLUE_BUILD, DOCKER_HOST, GITHUB_ACTIONS}, constants::{BLUE_BUILD, DOCKER_HOST, GITHUB_ACTIONS},
credentials::Credentials, credentials::Credentials,
get_env_var,
semver::Version, semver::Version,
string_vec, string_vec,
}; };
@ -85,7 +85,7 @@ impl DockerDriver {
trace!("{ls_out}"); trace!("{ls_out}");
if !ls_out.lines().any(|line| line == BLUE_BUILD) { 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 { if remote {
let context_list = get_context_list()?; let context_list = get_context_list()?;
trace!("{context_list:#?}"); trace!("{context_list:#?}");
@ -410,10 +410,8 @@ impl BuildDriver for DockerDriver {
} }
fn build_tag_push_cmd(opts: &BuildTagPushOpts<'_>, first_image: &str) -> Command { fn build_tag_push_cmd(opts: &BuildTagPushOpts<'_>, first_image: &str) -> Command {
// let remote = env::var(DOCKER_HOST).is_ok();
let c = cmd!( let c = cmd!(
"docker", "docker",
// if remote => format!("--context={BLUE_BUILD}0"),
"buildx", "buildx",
format!("--builder={BLUE_BUILD}"), format!("--builder={BLUE_BUILD}"),
"build", "build",
@ -426,7 +424,7 @@ fn build_tag_push_cmd(opts: &BuildTagPushOpts<'_>, first_image: &str) -> Command
opts.compression 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) => [ ImageRef::LocalTar(archive_path) => [
"--output", "--output",
format!("type=oci,dest={}", archive_path.display()), 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::{ use blue_build_utils::{
constants::{BB_PRIVATE_KEY, COSIGN_PRIV_PATH, COSIGN_PRIVATE_KEY, COSIGN_PUB_PATH}, constants::{BB_PRIVATE_KEY, COSIGN_PRIV_PATH, COSIGN_PRIVATE_KEY, COSIGN_PUB_PATH},
string, get_env_var, string,
}; };
use miette::{Result, bail}; use miette::{Result, bail};
@ -17,8 +17,8 @@ where
Ok( Ok(
match ( match (
path.join(COSIGN_PUB_PATH).exists(), path.join(COSIGN_PUB_PATH).exists(),
env::var(BB_PRIVATE_KEY).ok(), get_env_var(BB_PRIVATE_KEY).ok(),
env::var(COSIGN_PRIVATE_KEY).ok(), get_env_var(COSIGN_PRIVATE_KEY).ok(),
path.join(COSIGN_PRIV_PATH), path.join(COSIGN_PRIV_PATH),
) { ) {
(true, Some(private_key), _, _) if !private_key.is_empty() => { (true, Some(private_key), _, _) if !private_key.is_empty() => {

View file

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

View file

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

View file

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

View file

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

View file

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