Skip to content

Commit

Permalink
refactor: check env once
Browse files Browse the repository at this point in the history
  • Loading branch information
de-sh committed Nov 26, 2024
1 parent 23e3216 commit adb47c9
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/about.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,37 @@ 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<Option<LatestRelease>> = 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() {
"Docker"
} else {
"Native"
}
});

pub fn platform() -> &'static str {
PLATFORM.as_ref()
}

pub fn set_latest_release(latest_release: Option<LatestRelease>) {
Expand Down

0 comments on commit adb47c9

Please sign in to comment.