diff --git a/magic-nix-cache/src/env.rs b/magic-nix-cache/src/env.rs index 464221c..edc6453 100644 --- a/magic-nix-cache/src/env.rs +++ b/magic-nix-cache/src/env.rs @@ -1,6 +1,9 @@ -use crate::error::Error; -use std::env; +use std::{ + env, + fmt::{self, Display}, +}; +#[derive(Clone)] pub enum Environment { GitHubActions, GitLabCI, @@ -17,30 +20,34 @@ impl Environment { } } -impl ToString for Environment { - fn to_string(&self) -> String { +impl Display for Environment { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { use Environment::*; - String::from(match self { - GitHubActions => "GitHub Actions", - GitLabCI => "GitLab CI", - _ => "an unspecified environment", - }) + write!( + f, + "{}", + match self { + GitHubActions => "GitHub Actions", + GitLabCI => "GitLab CI", + Other => "an unspecified environment", + } + ) } } pub fn determine_environment() -> Environment { if env_var_is_true("GITHUB_ACTIONS") { - Environment::GitHubActions + return Environment::GitHubActions; } if env_var_is_true("CI") && env_var_is_true("GITLAB_CI") { - Environment::GitLabCI + return Environment::GitLabCI; } Environment::Other } fn env_var_is_true(e: &str) -> bool { - env::var(e).unwrap_or(String::from("")) == String::from("true") + &env::var(e).unwrap_or(String::from("")) == "true" } diff --git a/magic-nix-cache/src/error.rs b/magic-nix-cache/src/error.rs index ae479e1..ec1b8d3 100644 --- a/magic-nix-cache/src/error.rs +++ b/magic-nix-cache/src/error.rs @@ -19,9 +19,6 @@ 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), diff --git a/magic-nix-cache/src/main.rs b/magic-nix-cache/src/main.rs index cd90c12..b0024d6 100644 --- a/magic-nix-cache/src/main.rs +++ b/magic-nix-cache/src/main.rs @@ -33,10 +33,10 @@ 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}; -use tracing_subscriber::field::debug; use tracing_subscriber::filter::EnvFilter; use gha_cache::Credentials; @@ -125,6 +125,12 @@ impl Args { ))); } + if environment.is_gitlab_ci() && !self.use_flakehub { + return Err(Error::Config(String::from( + "you must set --use-flakehub in GitLab CI", + ))); + } + Ok(()) } } @@ -158,8 +164,8 @@ async fn main_cli() -> Result<()> { let args = Args::parse(); let environment = determine_environment(); - tracing::debug!("Running in {environment}"); - args.validate(environment)?; + tracing::debug!("Running in {}", environment.to_string()); + args.validate(environment.clone())?; let metrics = Arc::new(telemetry::TelemetryReport::new()); @@ -187,6 +193,7 @@ async fn main_cli() -> Result<()> { let flakehub_flake_name = args.flakehub_flake_name; match flakehub::init_cache( + environment, &args .flakehub_api_server .ok_or_else(|| anyhow!("--flakehub-api-server is required"))?,