From 8ad3089e93db17321bace9af1eff8cbf92a065b1 Mon Sep 17 00:00:00 2001 From: Luc Perkins Date: Thu, 16 May 2024 14:50:28 -0300 Subject: [PATCH] Remove unnecessary determine func --- magic-nix-cache/src/env.rs | 24 ++++++++++++------------ magic-nix-cache/src/main.rs | 12 ++++-------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/magic-nix-cache/src/env.rs b/magic-nix-cache/src/env.rs index edc6453..fe27ccd 100644 --- a/magic-nix-cache/src/env.rs +++ b/magic-nix-cache/src/env.rs @@ -11,6 +11,18 @@ pub enum Environment { } impl Environment { + pub fn determine() -> Self { + if env_var_is_true("GITHUB_ACTIONS") { + return Environment::GitHubActions; + } + + if env_var_is_true("CI") && env_var_is_true("GITLAB_CI") { + return Environment::GitLabCI; + } + + Environment::Other + } + pub fn is_github_actions(&self) -> bool { matches!(self, Self::GitHubActions) } @@ -36,18 +48,6 @@ impl Display for Environment { } } -pub fn determine_environment() -> Environment { - if env_var_is_true("GITHUB_ACTIONS") { - return Environment::GitHubActions; - } - - if env_var_is_true("CI") && env_var_is_true("GITLAB_CI") { - return Environment::GitLabCI; - } - - Environment::Other -} - fn env_var_is_true(e: &str) -> bool { &env::var(e).unwrap_or(String::from("")) == "true" } diff --git a/magic-nix-cache/src/main.rs b/magic-nix-cache/src/main.rs index b0024d6..3b93ebc 100644 --- a/magic-nix-cache/src/main.rs +++ b/magic-nix-cache/src/main.rs @@ -32,8 +32,6 @@ use ::attic::nix_store::NixStore; use anyhow::{anyhow, Context, Result}; use axum::{extract::Extension, routing::get, Router}; use clap::Parser; -use env::Environment; -use error::Error; use tempfile::NamedTempFile; use tokio::process::Command; use tokio::sync::{oneshot, Mutex, RwLock}; @@ -41,8 +39,6 @@ use tracing_subscriber::filter::EnvFilter; use gha_cache::Credentials; -use crate::env::determine_environment; - type State = Arc; /// GitHub Actions-powered Nix binary cache @@ -118,15 +114,15 @@ struct Args { } impl Args { - fn validate(&self, environment: Environment) -> Result<(), error::Error> { + fn validate(&self, environment: env::Environment) -> Result<(), error::Error> { if environment.is_gitlab_ci() && self.use_gha_cache { - return Err(Error::Config(String::from( + return Err(error::Error::Config(String::from( "the --use-gha-cache flag should not be applied in GitLab CI", ))); } if environment.is_gitlab_ci() && !self.use_flakehub { - return Err(Error::Config(String::from( + return Err(error::Error::Config(String::from( "you must set --use-flakehub in GitLab CI", ))); } @@ -163,7 +159,7 @@ async fn main_cli() -> Result<()> { init_logging(); let args = Args::parse(); - let environment = determine_environment(); + let environment = env::Environment::determine(); tracing::debug!("Running in {}", environment.to_string()); args.validate(environment.clone())?;