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::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"
}

View file

@ -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),

View file

@ -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"))?,