From 531387f66f69cbfc0bb62dbf587dffdf10c73fbb Mon Sep 17 00:00:00 2001 From: Cole Helbling Date: Tue, 27 Feb 2024 09:19:58 -0800 Subject: [PATCH] Allow configuring the FlakeHub flake name --- magic-nix-cache/src/flakehub.rs | 8 ++------ magic-nix-cache/src/main.rs | 13 +++++++++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/magic-nix-cache/src/flakehub.rs b/magic-nix-cache/src/flakehub.rs index 3f63796..4782273 100644 --- a/magic-nix-cache/src/flakehub.rs +++ b/magic-nix-cache/src/flakehub.rs @@ -9,7 +9,6 @@ use attic_client::{ }; use reqwest::Url; use serde::Deserialize; -use std::env; use std::path::Path; use std::str::FromStr; use std::sync::Arc; @@ -29,6 +28,7 @@ pub async fn init_cache( flakehub_api_server: &Url, flakehub_api_server_netrc: &Path, flakehub_cache_server: &Url, + flakehub_flake_name: &str, store: Arc, ) -> Result { // Parse netrc to get the credentials for api.flakehub.com. @@ -93,12 +93,8 @@ pub async fn init_cache( // Get the cache UUID for this project. let cache_name = { - let github_repo = env::var("GITHUB_REPOSITORY").map_err(|_| { - Error::Config("GITHUB_REPOSITORY environment variable is not set".to_owned()) - })?; - let url = flakehub_api_server - .join(&format!("project/{}", github_repo)) + .join(&format!("project/{}", flakehub_flake_name)) .map_err(|_| Error::Config(format!("bad URL '{}'", flakehub_api_server)))?; let response = reqwest::Client::new() diff --git a/magic-nix-cache/src/main.rs b/magic-nix-cache/src/main.rs index 623270f..6c32d4e 100644 --- a/magic-nix-cache/src/main.rs +++ b/magic-nix-cache/src/main.rs @@ -91,6 +91,9 @@ struct Args { #[arg(long)] flakehub_cache_server: Option, + #[arg(long)] + flakehub_flake_name: Option, + /// The location of `nix.conf`. #[arg(long)] nix_conf: PathBuf, @@ -164,6 +167,15 @@ async fn main_cli() -> Result<()> { let flakehub_api_server_netrc = args .flakehub_api_server_netrc .ok_or_else(|| anyhow!("--flakehub-api-server-netrc is required"))?; + let flakehub_flake_name = args + .flakehub_flake_name + .ok_or_else(|| { + tracing::debug!( + "--flakehub-flake-name was not set, inferring from $GITHUB_REPOSITORY env var" + ); + std::env::var("GITHUB_REPOSITORY") + }) + .map_err(|_| anyhow!("--flakehub-flake-name and $GITHUB_REPOSITORY were both unset"))?; match flakehub::init_cache( &args @@ -171,6 +183,7 @@ async fn main_cli() -> Result<()> { .ok_or_else(|| anyhow!("--flakehub-api-server is required"))?, &flakehub_api_server_netrc, &flakehub_cache_server, + &flakehub_flake_name, store.clone(), ) .await