Allow configuring the FlakeHub flake name

This commit is contained in:
Cole Helbling 2024-02-27 09:19:58 -08:00
parent 308fa515eb
commit 531387f66f
2 changed files with 15 additions and 6 deletions

View file

@ -9,7 +9,6 @@ use attic_client::{
}; };
use reqwest::Url; use reqwest::Url;
use serde::Deserialize; use serde::Deserialize;
use std::env;
use std::path::Path; use std::path::Path;
use std::str::FromStr; use std::str::FromStr;
use std::sync::Arc; use std::sync::Arc;
@ -29,6 +28,7 @@ pub async fn init_cache(
flakehub_api_server: &Url, flakehub_api_server: &Url,
flakehub_api_server_netrc: &Path, flakehub_api_server_netrc: &Path,
flakehub_cache_server: &Url, flakehub_cache_server: &Url,
flakehub_flake_name: &str,
store: Arc<NixStore>, store: Arc<NixStore>,
) -> Result<State> { ) -> Result<State> {
// Parse netrc to get the credentials for api.flakehub.com. // 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. // Get the cache UUID for this project.
let cache_name = { 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 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)))?; .map_err(|_| Error::Config(format!("bad URL '{}'", flakehub_api_server)))?;
let response = reqwest::Client::new() let response = reqwest::Client::new()

View file

@ -91,6 +91,9 @@ struct Args {
#[arg(long)] #[arg(long)]
flakehub_cache_server: Option<reqwest::Url>, flakehub_cache_server: Option<reqwest::Url>,
#[arg(long)]
flakehub_flake_name: Option<String>,
/// The location of `nix.conf`. /// The location of `nix.conf`.
#[arg(long)] #[arg(long)]
nix_conf: PathBuf, nix_conf: PathBuf,
@ -164,6 +167,15 @@ async fn main_cli() -> Result<()> {
let flakehub_api_server_netrc = args let flakehub_api_server_netrc = args
.flakehub_api_server_netrc .flakehub_api_server_netrc
.ok_or_else(|| anyhow!("--flakehub-api-server-netrc is required"))?; .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( match flakehub::init_cache(
&args &args
@ -171,6 +183,7 @@ async fn main_cli() -> Result<()> {
.ok_or_else(|| anyhow!("--flakehub-api-server is required"))?, .ok_or_else(|| anyhow!("--flakehub-api-server is required"))?,
&flakehub_api_server_netrc, &flakehub_api_server_netrc,
&flakehub_cache_server, &flakehub_cache_server,
&flakehub_flake_name,
store.clone(), store.clone(),
) )
.await .await