Add logic for determining environment
This commit is contained in:
parent
a5ade67dac
commit
90180e31ef
36
magic-nix-cache/src/env.rs
Normal file
36
magic-nix-cache/src/env.rs
Normal 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")
|
||||
}
|
|
@ -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),
|
||||
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in a new issue