Address Clippy issues

This commit is contained in:
Luc Perkins 2024-05-16 14:46:17 -03:00
parent 136a3d43d6
commit 41327e96b5
No known key found for this signature in database
GPG key ID: 16DB1108FB591835
3 changed files with 29 additions and 18 deletions

View file

@ -1,6 +1,9 @@
use crate::error::Error; use std::{
use std::env; env,
fmt::{self, Display},
};
#[derive(Clone)]
pub enum Environment { pub enum Environment {
GitHubActions, GitHubActions,
GitLabCI, GitLabCI,
@ -17,30 +20,34 @@ impl Environment {
} }
} }
impl ToString for Environment { impl Display for Environment {
fn to_string(&self) -> String { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use Environment::*; use Environment::*;
String::from(match self { write!(
f,
"{}",
match self {
GitHubActions => "GitHub Actions", GitHubActions => "GitHub Actions",
GitLabCI => "GitLab CI", GitLabCI => "GitLab CI",
_ => "an unspecified environment", Other => "an unspecified environment",
}) }
)
} }
} }
pub fn determine_environment() -> Environment { pub fn determine_environment() -> Environment {
if env_var_is_true("GITHUB_ACTIONS") { if env_var_is_true("GITHUB_ACTIONS") {
Environment::GitHubActions return Environment::GitHubActions;
} }
if env_var_is_true("CI") && env_var_is_true("GITLAB_CI") { if env_var_is_true("CI") && env_var_is_true("GITLAB_CI") {
Environment::GitLabCI return Environment::GitLabCI;
} }
Environment::Other Environment::Other
} }
fn env_var_is_true(e: &str) -> bool { 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"
} }

View file

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

View file

@ -33,10 +33,10 @@ use anyhow::{anyhow, Context, Result};
use axum::{extract::Extension, routing::get, Router}; use axum::{extract::Extension, routing::get, Router};
use clap::Parser; use clap::Parser;
use env::Environment; use env::Environment;
use error::Error;
use tempfile::NamedTempFile; use tempfile::NamedTempFile;
use tokio::process::Command; use tokio::process::Command;
use tokio::sync::{oneshot, Mutex, RwLock}; use tokio::sync::{oneshot, Mutex, RwLock};
use tracing_subscriber::field::debug;
use tracing_subscriber::filter::EnvFilter; use tracing_subscriber::filter::EnvFilter;
use gha_cache::Credentials; 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(()) Ok(())
} }
} }
@ -158,8 +164,8 @@ async fn main_cli() -> Result<()> {
let args = Args::parse(); let args = Args::parse();
let environment = determine_environment(); let environment = determine_environment();
tracing::debug!("Running in {environment}"); tracing::debug!("Running in {}", environment.to_string());
args.validate(environment)?; args.validate(environment.clone())?;
let metrics = Arc::new(telemetry::TelemetryReport::new()); let metrics = Arc::new(telemetry::TelemetryReport::new());
@ -187,6 +193,7 @@ async fn main_cli() -> Result<()> {
let flakehub_flake_name = args.flakehub_flake_name; let flakehub_flake_name = args.flakehub_flake_name;
match flakehub::init_cache( match flakehub::init_cache(
environment,
&args &args
.flakehub_api_server .flakehub_api_server
.ok_or_else(|| anyhow!("--flakehub-api-server is required"))?, .ok_or_else(|| anyhow!("--flakehub-api-server is required"))?,