this repo has no description
1use color_eyre::eyre::{Result, eyre};
2use confique::Config;
3use std::path::PathBuf;
4use std::sync::OnceLock;
5
6#[derive(Config, Debug, Clone)]
7pub struct AppConfig {
8 #[config(env = "DISCORD_TOKEN")]
9 pub discord_token: String,
10
11 #[config(env = "GITHUB_TOKEN")]
12 pub github_token: String,
13
14 #[config(env = "KAGI_COOKIE")]
15 pub kagi_cookie: Option<String>,
16
17 #[config(env = "BLAHAJ_DATA_DIR", default = "/var/lib/blahaj")]
18 pub data_dir: PathBuf,
19
20 #[config(
21 env = "NIXPKGS_CHANNEL",
22 default = "https://channels.nixos.org/nixpkgs-unstable"
23 )]
24 pub nixpkgs_channel: String,
25}
26
27static CONFIG: OnceLock<AppConfig> = OnceLock::new();
28
29pub fn init() -> Result<&'static AppConfig> {
30 if let Some(config) = CONFIG.get() {
31 return Ok(config);
32 }
33
34 let mut builder = AppConfig::builder().env();
35 if let Ok(path) = std::env::var("BLAHAJ_CONFIG") {
36 builder = builder.file(path);
37 } else {
38 builder = builder.file("blahaj.toml");
39 }
40
41 let config = builder.load().map_err(|err| eyre!("{err}"))?;
42 CONFIG
43 .set(config)
44 .map_err(|_| eyre!("config already initialized"))?;
45 Ok(CONFIG.get().expect("config initialized"))
46}
47
48pub fn get() -> &'static AppConfig {
49 CONFIG.get().expect("config not initialized")
50}