From 0e4d694b358008c5628fc92661bbcb08df7df291 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Thu, 25 Apr 2024 14:33:48 +0200 Subject: [PATCH] fix config parser --- .autocrate.yaml | 8 +++---- .env | 2 ++ src/changelog/mod.rs | 1 - src/config/mod.rs | 46 ++++++++++++++++++++++++++++------------ src/error.rs | 4 ++++ src/serverapi/forgejo.rs | 14 +++++++----- 6 files changed, 51 insertions(+), 24 deletions(-) create mode 100644 .env diff --git a/.autocrate.yaml b/.autocrate.yaml index bc33cd6..f479d9c 100644 --- a/.autocrate.yaml +++ b/.autocrate.yaml @@ -1,4 +1,5 @@ ---- +version: + !text "echo foo" changelog: enable: true git-log: true @@ -14,12 +15,11 @@ uses: api: github: type: github - endpoint: https://github.com repository: autocrate auth: user: PlexSheep pass: - env: TOKEN_GH + !env TOKEN_GH cscherr: type: forgejo endpoint: https://git.cscherr.de @@ -27,4 +27,4 @@ api: auth: user: PlexSheep pass: - env: TOKEN_CSCHERR + !env TOKEN_CSCHERR diff --git a/.env b/.env new file mode 100644 index 0000000..b544a1e --- /dev/null +++ b/.env @@ -0,0 +1,2 @@ +TOKEN_CSCHERR=test +TOKEN_GH=test diff --git a/src/changelog/mod.rs b/src/changelog/mod.rs index 3e14b81..1b914ba 100644 --- a/src/changelog/mod.rs +++ b/src/changelog/mod.rs @@ -35,7 +35,6 @@ impl Changelog { return Err(ChangelogError::GitBadStatus(out.status, buf).into()); } - dbg!(&buf); Ok(Some(buf)) } diff --git a/src/config/mod.rs b/src/config/mod.rs index 74075f7..b9d6dd6 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -1,10 +1,11 @@ +use std::str::FromStr; use std::{ collections::HashMap, fmt::Debug, fs::File, io::BufReader, path::PathBuf, process::Command, }; use git2; use libpt::log::{debug, error, trace}; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use url::Url; use crate::error::*; @@ -17,7 +18,7 @@ pub trait YamlConfigSection: Debug + Clone + for<'a> Deserialize<'a> { fn check(&self) -> Result<()>; } -#[derive(Debug, Clone, Deserialize)] +#[derive(Debug, Clone, Deserialize, Serialize)] pub struct Changelog { pub enable: bool, #[serde(alias = "git-log")] @@ -29,7 +30,7 @@ impl YamlConfigSection for Changelog { } } -#[derive(Debug, Clone, Deserialize)] +#[derive(Debug, Clone, Deserialize, Serialize)] pub struct UseCargo { pub publish: bool, pub registries: Vec, @@ -40,7 +41,7 @@ impl YamlConfigSection for UseCargo { } } -#[derive(Debug, Clone, Deserialize)] +#[derive(Debug, Clone, Deserialize, Serialize)] pub struct Uses { cargo: UseCargo, } @@ -51,7 +52,7 @@ impl YamlConfigSection for Uses { } } -#[derive(Debug, Clone, Deserialize)] +#[derive(Debug, Clone, Deserialize, Serialize)] #[serde(rename_all = "snake_case")] pub enum Pass { /// pass specified as plainext @@ -94,7 +95,7 @@ impl YamlConfigSection for Pass { Ok(()) } } -#[derive(Debug, Clone, Deserialize)] +#[derive(Debug, Clone, Deserialize, Serialize)] pub struct ApiAuth { pub user: String, pub pass: Pass, @@ -106,11 +107,12 @@ impl YamlConfigSection for ApiAuth { } } -#[derive(Debug, Clone, Deserialize)] +#[derive(Debug, Clone, Deserialize, Serialize)] pub struct Api { #[serde(alias = "type")] pub server_type: ApiType, - pub endpoint: Url, + /// May be left empty if the [ApiType] is [Github](ApiType::Github). + pub endpoint: Option, /// May be left empty if the Api does not need auth or the auth is part of the /// [endpoint](Api::endpoint) [Url]. pub auth: Option, @@ -120,9 +122,16 @@ pub struct Api { impl YamlConfigSection for Api { fn check(&self) -> Result<()> { self.server_type.check()?; - match self.endpoint.socket_addrs(|| None) { - Ok(_) => (), - Err(err) => return Err(err.into()), + if self.server_type != ApiType::Github { + if self.auth.is_none() { + return Err(ConfigError::NoEndpointSet.into()); + } + match self.endpoint.clone().unwrap().socket_addrs(|| None) { + Ok(_) => (), + Err(err) => return Err(err.into()), + } + } else if let Some(_url) = &self.endpoint { + return Err(ConfigError::EndpointSetButNotNeeded.into()); } if self.auth.is_some() { self.auth.clone().unwrap().check()?; @@ -131,7 +140,7 @@ impl YamlConfigSection for Api { } } -#[derive(Debug, Clone, Deserialize)] +#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)] pub enum ApiType { #[serde(alias = "gitea")] Gitea, @@ -142,13 +151,22 @@ pub enum ApiType { #[serde(alias = "forgejo")] Forgejo, } +impl ApiType { + pub fn default_endpoint(&self) -> Option { + match self { + Self::Github => Some(Url::from_str("https://github.com").unwrap()), + _ => None, + } + } +} + impl YamlConfigSection for ApiType { fn check(&self) -> Result<()> { Ok(()) } } -#[derive(Debug, Clone, Deserialize)] +#[derive(Debug, Clone, Deserialize, Serialize)] #[serde(rename_all = "snake_case")] pub enum Version { Text(String), @@ -198,7 +216,7 @@ impl YamlConfigSection for Version { } } -#[derive(Debug, Clone, Deserialize)] +#[derive(Debug, Clone, Deserialize, Serialize)] pub struct YamlConfig { pub changelog: Changelog, pub uses: Uses, diff --git a/src/error.rs b/src/error.rs index 7dd062a..985b588 100644 --- a/src/error.rs +++ b/src/error.rs @@ -61,4 +61,8 @@ pub enum ConfigError { EnvNotSet(String), #[error("Bad value for environment variable: {0}")] BadEnv(#[from] VarError), + #[error("An endpoint was set for an ApiType that does not require one")] + EndpointSetButNotNeeded, + #[error("No endpoint was set for an ApiType that requires one")] + NoEndpointSet } diff --git a/src/serverapi/forgejo.rs b/src/serverapi/forgejo.rs index 45aa20a..2dfaa94 100644 --- a/src/serverapi/forgejo.rs +++ b/src/serverapi/forgejo.rs @@ -45,11 +45,15 @@ impl ServerApi for Forgejo { Ok(()) } async fn push_release(&mut self, rc: ReleaseContext) -> Result<()> { - let raw_url = format!( - "{}/api/v1/repos/{}/{}/releases", - self.cfg.endpoint, rc.username, rc.repository - ); - let url = Url::parse(&raw_url).map_err(ServerApiError::from)?; + let url: Url = match &self.cfg.endpoint { + Some(url) => url.clone(), + None => self + .cfg + .server_type + .default_endpoint() + .expect("no default endpoint for this api type"), + }; + url.join("/api/v1/repos/{}/{}/releases"); let body = format!( r#" {{