Add logic for determining environment

This commit is contained in:
Luc Perkins 2024-05-16 13:53:46 -03:00
parent a5ade67dac
commit 90180e31ef
No known key found for this signature in database
GPG key ID: 16DB1108FB591835
4 changed files with 45 additions and 1 deletions

View file

@ -55,7 +55,7 @@
cargo-bloat
cargo-edit
cargo-udeps
cargo-watch
bacon
age
];

View file

@ -0,0 +1,36 @@
use crate::error::Error;
use std::env;
pub enum Environment {
GitHubActions,
GitLabCI,
Other,
}
impl ToString for Environment {
fn to_string(&self) -> String {
use Environment::*;
String::from(match self {
GitHubActions => "GitHub Actions",
GitLabCI => "GitLab CI",
_ => "unspecified",
})
}
}
pub fn determine_environment() -> Environment {
if env_var_is_true("GITHUB_ACTIONS") {
Environment::GitHubActions
}
if env_var_is_true("CI") && env_var_is_true("GITLAB_CI") {
Environment::GitLabCI
}
Environment::Other
}
fn env_var_is_true(e: &str) -> bool {
env::var(e).unwrap_or(String::from("")) == String::from("true")
}

View file

@ -19,6 +19,9 @@ pub enum Error {
#[error("Bad Request")]
BadRequest,
#[error("Current environment couldn't be determinate")]
UnknownEnv,
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),

View file

@ -14,6 +14,7 @@
mod api;
mod binary_cache;
mod env;
mod error;
mod flakehub;
mod gha;
@ -38,6 +39,8 @@ use tracing_subscriber::filter::EnvFilter;
use gha_cache::Credentials;
use crate::env::determine_environment;
type State = Arc<StateInner>;
/// GitHub Actions-powered Nix binary cache
@ -141,6 +144,8 @@ async fn main_cli() -> Result<()> {
let args = Args::parse();
let _environment = determine_environment();
let metrics = Arc::new(telemetry::TelemetryReport::new());
if let Some(parent) = Path::new(&args.nix_conf).parent() {