Make the FlakeHubArg a generic Trinary so we can use it for GHA Cache too

This commit is contained in:
Graham Christensen 2024-11-05 21:11:26 -05:00
parent cf183317a5
commit 3fd6eeb208

View file

@ -114,7 +114,7 @@ struct Args {
/// Whether to use the FlakeHub binary cache. /// Whether to use the FlakeHub binary cache.
#[arg(long)] #[arg(long)]
use_flakehub: Option<Option<FlakeHubArg>>, use_flakehub: Option<Option<CacheTrinary>>,
/// URL to which to post startup notification. /// URL to which to post startup notification.
#[arg(long)] #[arg(long)]
@ -130,17 +130,17 @@ struct Args {
} }
#[derive(Debug, Clone, Copy, PartialEq, clap::ValueEnum)] #[derive(Debug, Clone, Copy, PartialEq, clap::ValueEnum)]
pub enum FlakeHubArg { pub enum CacheTrinary {
NoPreference, NoPreference,
Enabled, Enabled,
Disabled, Disabled,
} }
impl From<Option<Option<FlakeHubArg>>> for FlakeHubArg { impl From<Option<Option<CacheTrinary>>> for CacheTrinary {
fn from(b: Option<Option<FlakeHubArg>>) -> Self { fn from(b: Option<Option<CacheTrinary>>) -> Self {
match b { match b {
None => FlakeHubArg::NoPreference, None => CacheTrinary::NoPreference,
Some(None) => FlakeHubArg::Enabled, Some(None) => CacheTrinary::Enabled,
Some(Some(v)) => v, Some(Some(v)) => v,
} }
} }
@ -170,7 +170,7 @@ impl Args {
))); )));
} }
if environment.is_gitlab_ci() && self.flakehub_preference() != FlakeHubArg::Enabled { if environment.is_gitlab_ci() && self.flakehub_preference() != CacheTrinary::Enabled {
return Err(error::Error::Config(String::from( return Err(error::Error::Config(String::from(
"you must set --use-flakehub in GitLab CI", "you must set --use-flakehub in GitLab CI",
))); )));
@ -179,7 +179,7 @@ impl Args {
Ok(()) Ok(())
} }
fn flakehub_preference(&self) -> FlakeHubArg { fn flakehub_preference(&self) -> CacheTrinary {
self.use_flakehub.into() self.use_flakehub.into()
} }
} }
@ -287,17 +287,17 @@ async fn main_cli() -> Result<()> {
dnixd_available, dnixd_available,
) { ) {
// User has explicitly pyassed --use-flakehub=disabled, so just straight up don't // User has explicitly pyassed --use-flakehub=disabled, so just straight up don't
(FlakeHubArg::Disabled, _, _) => { (CacheTrinary::Disabled, _, _) => {
tracing::info!("Disabling FlakeHub cache."); tracing::info!("Disabling FlakeHub cache.");
None None
} }
// User has no preference, did not pass a netrc, and determinate-nixd is not available // User has no preference, did not pass a netrc, and determinate-nixd is not available
(FlakeHubArg::NoPreference, None, Dnixd::Missing) => None, (CacheTrinary::NoPreference, None, Dnixd::Missing) => None,
// Use it when determinate-nixd is available, and let the user know what's going on // Use it when determinate-nixd is available, and let the user know what's going on
(pref, user_netrc_path, Dnixd::Available) => { (pref, user_netrc_path, Dnixd::Available) => {
if pref == FlakeHubArg::NoPreference { if pref == CacheTrinary::NoPreference {
tracing::info!("Enabling FlakeHub cache because determinate-nixd is available."); tracing::info!("Enabling FlakeHub cache because determinate-nixd is available.");
} }
@ -312,7 +312,7 @@ async fn main_cli() -> Result<()> {
(_, Some(path), Dnixd::Missing) => Some(FlakeHubAuthSource::Netrc(path.to_owned())), (_, Some(path), Dnixd::Missing) => Some(FlakeHubAuthSource::Netrc(path.to_owned())),
// User explicitly turned on flakehub cache, but we have no netrc and determinate-nixd is not present // User explicitly turned on flakehub cache, but we have no netrc and determinate-nixd is not present
(FlakeHubArg::Enabled, None, Dnixd::Missing) => { (CacheTrinary::Enabled, None, Dnixd::Missing) => {
return Err(anyhow!( return Err(anyhow!(
"--flakehub-api-server-netrc is required when determinate-nixd is unavailable" "--flakehub-api-server-netrc is required when determinate-nixd is unavailable"
)); ));