diff --git a/process/drivers/docker_driver.rs b/process/drivers/docker_driver.rs index aec3a62..6184530 100644 --- a/process/drivers/docker_driver.rs +++ b/process/drivers/docker_driver.rs @@ -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()), diff --git a/process/drivers/functions.rs b/process/drivers/functions.rs index 3020ebe..7631a96 100644 --- a/process/drivers/functions.rs +++ b/process/drivers/functions.rs @@ -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() => { diff --git a/process/drivers/opts/signing.rs b/process/drivers/opts/signing.rs index 6cfd488..d4379ab 100644 --- a/process/drivers/opts/signing.rs +++ b/process/drivers/opts/signing.rs @@ -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> for PrivateKey { impl PrivateKeyContents for PrivateKey { fn contents(&self) -> Result> { 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()?, })) } diff --git a/process/drivers/types.rs b/process/drivers/types.rs index b5a9207..88ae80f 100644 --- a/process/drivers/types.rs +++ b/process/drivers/types.rs @@ -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 for Option { 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, diff --git a/process/logging.rs b/process/logging.rs index 6cb7b2c..b6d362c 100644 --- a/process/logging.rs +++ b/process/logging.rs @@ -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, diff --git a/src/commands/bug_report.rs b/src/commands/bug_report.rs index f4a4b92..a8c10bf 100644 --- a/src/commands/bug_report.rs +++ b/src/commands/bug_report.rs @@ -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 { diff --git a/utils/src/credentials.rs b/utils/src/credentials.rs index 60ac5a2..243e7dd 100644 --- a/utils/src/credentials.rs +++ b/utils/src/credentials.rs @@ -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> = LazyLock::new(|| Mutex::new(false)); @@ -52,8 +49,8 @@ static ENV_CREDENTIALS: LazyLock> = 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> = LazyLock::new(|| { docker_credential::get_credential(®istry).ok(), docker_credential::get_podman_credential(®istry).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)), _, _, _)