Skip to content

Commit adb47c9

Browse files
author
Devdutt Shenoi
committed
refactor: check env once
1 parent 23e3216 commit adb47c9

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/about.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,37 @@ use crate::utils::update::{self, LatestRelease};
2424
use chrono::Duration;
2525
use chrono_humanize::{Accuracy, Tense};
2626
use crossterm::style::Stylize;
27-
use once_cell::sync::OnceCell;
27+
use once_cell::sync::{Lazy, OnceCell};
2828
use std::env;
2929
use std::path::Path;
3030
use sysinfo::System;
3131
use ulid::Ulid;
32+
3233
// Expose some static variables for internal usage
3334
pub static LATEST_RELEASE: OnceCell<Option<LatestRelease>> = OnceCell::new();
34-
static K8S_ENV_TO_CHECK: &str = "KUBERNETES_SERVICE_HOST";
35-
36-
fn is_docker() -> bool {
37-
Path::new("/.dockerenv").exists()
38-
}
3935

36+
static K8S_ENV_TO_CHECK: &str = "KUBERNETES_SERVICE_HOST";
4037
fn is_k8s() -> bool {
4138
env::var(K8S_ENV_TO_CHECK).is_ok()
4239
}
4340

44-
pub fn platform() -> &'static str {
41+
static DOCKERENV_FILE: &str = "/.dockerenv";
42+
fn is_docker() -> bool {
43+
Path::new(DOCKERENV_FILE).exists()
44+
}
45+
46+
static PLATFORM: Lazy<&'static str> = Lazy::new(|| {
4547
if is_k8s() {
4648
"Kubernetes"
4749
} else if is_docker() {
4850
"Docker"
4951
} else {
5052
"Native"
5153
}
54+
});
55+
56+
pub fn platform() -> &'static str {
57+
PLATFORM.as_ref()
5258
}
5359

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

0 commit comments

Comments
 (0)