From adb47c9d5b30357d7c38b0f34a0d6b0668433ff9 Mon Sep 17 00:00:00 2001 From: Devdutt Shenoi Date: Wed, 27 Nov 2024 04:16:58 +0530 Subject: [PATCH] refactor: check env once --- src/about.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/about.rs b/src/about.rs index 5a1d20e03..1897e1299 100644 --- a/src/about.rs +++ b/src/about.rs @@ -24,24 +24,26 @@ use crate::utils::update::{self, LatestRelease}; use chrono::Duration; use chrono_humanize::{Accuracy, Tense}; use crossterm::style::Stylize; -use once_cell::sync::OnceCell; +use once_cell::sync::{Lazy, OnceCell}; use std::env; use std::path::Path; use sysinfo::System; use ulid::Ulid; + // Expose some static variables for internal usage pub static LATEST_RELEASE: OnceCell> = OnceCell::new(); -static K8S_ENV_TO_CHECK: &str = "KUBERNETES_SERVICE_HOST"; - -fn is_docker() -> bool { - Path::new("/.dockerenv").exists() -} +static K8S_ENV_TO_CHECK: &str = "KUBERNETES_SERVICE_HOST"; fn is_k8s() -> bool { env::var(K8S_ENV_TO_CHECK).is_ok() } -pub fn platform() -> &'static str { +static DOCKERENV_FILE: &str = "/.dockerenv"; +fn is_docker() -> bool { + Path::new(DOCKERENV_FILE).exists() +} + +static PLATFORM: Lazy<&'static str> = Lazy::new(|| { if is_k8s() { "Kubernetes" } else if is_docker() { @@ -49,6 +51,10 @@ pub fn platform() -> &'static str { } else { "Native" } +}); + +pub fn platform() -> &'static str { + PLATFORM.as_ref() } pub fn set_latest_release(latest_release: Option) {