feat(auth): add file merge as base
Some checks failed
continuous-integration/drone/push Build is failing

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2023-08-20 01:44:54 +02:00
parent acde8b17e1
commit f65e85dbe1
Signed by: kjuulh
GPG Key ID: 9AA7BC13CE474394

View File

@ -1,5 +1,6 @@
pub use introspection::IntrospectionConfigClap;
use oauth::{OAuth, ZitadelConfig};
use serde::{Deserialize, Serialize};
mod introspection;
mod oauth;
@ -26,7 +27,12 @@ pub struct AuthClap {
pub zitadel: ZitadelClap,
}
#[derive(clap::Args, Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct AuthConfigFile {
zitadel: Option<ZitadelClap>,
}
#[derive(clap::Args, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[group(requires_all = ["auth_url", "client_id", "client_secret", "redirect_url", "token_url", "authority_url"])]
pub struct ZitadelClap {
#[arg(env = "ZITADEL_AUTH_URL", long = "zitadel-auth-url")]
@ -59,6 +65,45 @@ impl TryFrom<AuthClap> for OAuth {
}
}
impl AuthClap {
pub(crate) fn merge(&mut self, config: AuthConfigFile) -> &mut Self {
if let Some(zitadel) = config.zitadel {
if let Some(auth_url) = zitadel.auth_url {
if let Some(_) = self.zitadel.auth_url {
_ = self.zitadel.auth_url.replace(auth_url);
}
}
if let Some(client_id) = zitadel.client_id {
if let Some(_) = self.zitadel.client_id {
_ = self.zitadel.client_id.replace(client_id);
}
}
if let Some(client_secret) = zitadel.client_secret {
if let Some(_) = self.zitadel.client_secret {
_ = self.zitadel.client_secret.replace(client_secret);
}
}
if let Some(redirect_url) = zitadel.redirect_url {
if let Some(_) = self.zitadel.redirect_url {
_ = self.zitadel.redirect_url.replace(redirect_url);
}
}
if let Some(authority_url) = zitadel.authority_url {
if let Some(_) = self.zitadel.authority_url {
_ = self.zitadel.authority_url.replace(authority_url);
}
}
if let Some(token_url) = zitadel.token_url {
if let Some(_) = self.zitadel.token_url {
_ = self.zitadel.token_url.replace(token_url);
}
}
}
self
}
}
#[cfg(test)]
mod test {
use crate::{AuthClap, AuthEngine, ZitadelClap};
@ -129,8 +174,7 @@ mod test {
let cli: Cli = Cli::parse_from(&[
"base",
"one",
"--auth-engine",
"zitadel",
"--auth-engine=zitadel",
"--zitadel-client-id=something",
"--zitadel-client-secret=something",
"--zitadel-auth-url=https://something",