From c61b701095a1f6b52777d317275f34687e57ee3e Mon Sep 17 00:00:00 2001 From: seth Date: Thu, 10 Oct 2024 07:26:29 -0400 Subject: rise once again my glorious creation (#51) * git-tracker: update tips after fetch + cleanup * nix: use nix-filter * ci: cleanup * crates: update * git-tracker: don't spam log while transferring * nix: fix static package eval --- crates/discord-bot/src/config.rs | 51 ++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 17 deletions(-) (limited to 'crates/discord-bot/src/config.rs') diff --git a/crates/discord-bot/src/config.rs b/crates/discord-bot/src/config.rs index 5076eb9..afc7845 100644 --- a/crates/discord-bot/src/config.rs +++ b/crates/discord-bot/src/config.rs @@ -1,25 +1,21 @@ -use crate::consts::NIXPKGS_REMOTE; +use git_tracker::TrackedRepository; -use std::env; +use std::{env, path::PathBuf, sync::Arc}; + +const DEFAULT_NIXPKGS_URL: &str = "https://github.com/NixOS/nixpkgs"; + +const DEFAULT_NIXPKGS_REMOTE: &str = "origin"; /// The Discord client's configuration #[derive(Clone, Debug)] pub struct Config { - /// Path to clone a new or use an existing nixpkgs repository - pub nixpkgs_path: String, - // A comma separated list of nixpkgs branch to track commits for - pub nixpkgs_branches: Vec, + /// Comma separated list of nixpkgs branch to track commits for + nixpkgs_branches: Vec, + /// Repository tracker + repository: Arc, } impl Config { - /// Take in a comma separated list and split it into a [`Vec`] - fn split_string_list(branches: &str) -> Vec { - branches - .split(',') - .map(|branch| format!("{NIXPKGS_REMOTE}/{}", branch.trim())) - .collect() - } - /// Create a new instance of [`Config`] based on variables from the environment /// /// # Errors @@ -27,12 +23,33 @@ impl Config { /// Will return [`Err`] if a variable is not found pub fn from_env() -> Result { let nixpkgs_path = env::var("BOT_NIXPKGS_PATH")?; - let nixpkgs_branches_raw = env::var("BOT_NIXPKGS_BRANCHES")?; - let nixpkgs_branches = Self::split_string_list(&nixpkgs_branches_raw); + + let nixpkgs_branches = env::var("BOT_NIXPKGS_BRANCHES")? + .split(',') + .map(ToString::to_string) + .collect(); + + let nixpkgs_remote = + env::var("BOT_NIXPKGS_REMOTE").unwrap_or(DEFAULT_NIXPKGS_REMOTE.to_string()); + let nixpkgs_url = env::var("BOT_NIXPKGS_URL").unwrap_or(DEFAULT_NIXPKGS_URL.to_string()); + + let repository = TrackedRepository::new( + PathBuf::from(nixpkgs_path.clone()), + nixpkgs_url, + nixpkgs_remote, + ); Ok(Self { - nixpkgs_path, nixpkgs_branches, + repository: Arc::new(repository), }) } + + pub fn repository(&self) -> &TrackedRepository { + &self.repository + } + + pub fn nixpkgs_branches(&self) -> &Vec { + &self.nixpkgs_branches + } } -- cgit v1.2.3