fix: properly parse layers env var
ci/woodpecker/push/lint Pipeline was successful Details
ci/woodpecker/push/clippy Pipeline was successful Details
ci/woodpecker/push/build Pipeline was successful Details

main
Jef Roosens 2023-08-12 13:46:46 +02:00
parent b3d1cec078
commit 5f6366078c
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 24 additions and 7 deletions

View File

@ -2,7 +2,7 @@ mod backup;
mod config; mod config;
mod run; mod run;
use std::path::PathBuf; use std::{path::PathBuf, str::FromStr};
use clap::{Args, Parser, Subcommand}; use clap::{Args, Parser, Subcommand};
use figment::{ use figment::{
@ -83,7 +83,7 @@ impl Cli {
} }
} }
pub fn config<T, U>(&self, args: &U) -> Result<T, figment::Error> pub fn config<T, U>(&self, args: &U) -> crate::Result<T>
where where
T: Default + Serialize + for<'de> Deserialize<'de>, T: Default + Serialize + for<'de> Deserialize<'de>,
U: Serialize, U: Serialize,
@ -92,11 +92,28 @@ impl Cli {
.config_file .config_file
.clone() .clone()
.unwrap_or(PathBuf::from(Env::var_or("ALEX_CONFIG_FILE", ""))); .unwrap_or(PathBuf::from(Env::var_or("ALEX_CONFIG_FILE", "")));
Figment::new()
let mut figment = Figment::new()
.merge(Serialized::defaults(T::default())) .merge(Serialized::defaults(T::default()))
.merge(Toml::file(toml_file)) .merge(Toml::file(toml_file))
.merge(Env::prefixed("ALEX_")) .merge(Env::prefixed("ALEX_").ignore(&["ALEX_LAYERS"]));
.merge(Serialized::defaults(args))
.extract() // Layers need to be parsed separately, as the env var format is different than the one
// serde expects
if let Some(layers_env) = Env::var("ALEX_LAYERS") {
let res = layers_env
.split(';')
.map(ManagerConfig::from_str)
.collect::<Vec<_>>();
if res.iter().any(|e| e.is_err()) {
return Err(crate::other("Invalid layer configuration").into());
}
let layers: Vec<_> = res.iter().flatten().collect();
figment = figment.merge(Serialized::default("layers", layers));
}
Ok(figment.merge(Serialized::defaults(args)).extract()?)
} }
} }

View File

@ -66,7 +66,6 @@ fn backups_thread(server: Arc<server::ServerProcess>) {
impl RunCli { impl RunCli {
pub fn run(&self, cli: &super::Cli, global: &super::Config) -> crate::Result<()> { pub fn run(&self, cli: &super::Cli, global: &super::Config) -> crate::Result<()> {
let config: Config = cli.config(&self.args)?; let config: Config = cli.config(&self.args)?;
println!("{:?}", config);
let (_, mut signals) = signals::install_signal_handlers()?; let (_, mut signals) = signals::install_signal_handlers()?;

View File

@ -9,6 +9,7 @@ use serde::{Deserialize, Serialize};
use std::fmt; use std::fmt;
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Serialize, Deserialize, Debug)] #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Serialize, Deserialize, Debug)]
#[serde(rename_all = "lowercase")]
pub enum ServerType { pub enum ServerType {
Unknown, Unknown,
Paper, Paper,