From 0f1b82c4c975de3545ec0be79166a1fbc4e5d44b Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Fri, 16 Feb 2024 20:00:55 +0100 Subject: [PATCH] release and server api skeletons --- src/config/mod.rs | 4 ++-- src/main.rs | 6 ++++-- src/publish/mod.rs | 2 +- src/release/mod.rs | 8 +++++++- src/serverapi/gitea.rs | 24 +++++++++++++++++++++++ src/serverapi/github.rs | 24 +++++++++++++++++++++++ src/serverapi/gitlab.rs | 24 +++++++++++++++++++++++ src/serverapi/mod.rs | 42 +++++++++++++++++++++++++++++++++++++++++ 8 files changed, 128 insertions(+), 6 deletions(-) create mode 100644 src/serverapi/gitea.rs create mode 100644 src/serverapi/github.rs create mode 100644 src/serverapi/gitlab.rs diff --git a/src/config/mod.rs b/src/config/mod.rs index 6943768..d2e5d09 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -73,7 +73,7 @@ impl YamlConfigSection for ApiAuth { #[derive(Debug, Clone, Deserialize)] pub struct Api { - pub r#type: ApiType, + pub server_type: ApiType, pub endpoint: Url, /// May be left empty if the Api does not need auth or the auth is part of the /// [endpoint](Api::endpoint) [Url]. @@ -81,7 +81,7 @@ pub struct Api { } impl YamlConfigSection for Api { fn check(&self) -> Result<()> { - self.r#type.check()?; + self.server_type.check()?; match self.endpoint.socket_addrs(|| None) { Ok(_) => (), Err(err) => return Err(err.into()), diff --git a/src/main.rs b/src/main.rs index 038ff95..3961406 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use autocrate::{ Config, }, release::release, + serverapi::init_servers, publish::publish, error::*, }; @@ -19,10 +20,11 @@ async fn main() -> Result<()> { println!("{}", Changelog::build(&cfg)?); } Commands::Release { .. } => { - release(&cfg)?; + init_servers(&cfg).await?; + release(&cfg).await?; } Commands::Publish { .. } => { - publish(&cfg)?; + publish(&cfg).await?; } }; Ok(()) diff --git a/src/publish/mod.rs b/src/publish/mod.rs index 22ec176..2d4d723 100644 --- a/src/publish/mod.rs +++ b/src/publish/mod.rs @@ -1,4 +1,4 @@ use crate::{config::Config, error::*}; -pub fn publish(cfg: &Config) -> Result<()> { +pub async fn publish(cfg: &Config) -> Result<()> { todo!() } diff --git a/src/release/mod.rs b/src/release/mod.rs index 35216ec..a82a979 100644 --- a/src/release/mod.rs +++ b/src/release/mod.rs @@ -1,5 +1,11 @@ use crate::{config::Config, error::*}; -pub fn release(cfg: &Config) -> Result<()> { +pub async fn release(cfg: &Config) -> Result<()> { + // TODO: git tag + // TODO: version bump + // TODO: version select interactive + // TODO: version select automated + // TODO: push to each server + // TODO: release to each server todo!() } diff --git a/src/serverapi/gitea.rs b/src/serverapi/gitea.rs new file mode 100644 index 0000000..f831ba5 --- /dev/null +++ b/src/serverapi/gitea.rs @@ -0,0 +1,24 @@ +use async_trait::async_trait; + +use super::ServerApi; +use crate::{ + config::{ApiType, Config}, + error::*, +}; +pub struct Gitea; + +#[async_trait] +impl ServerApi for Gitea { + async fn init(&mut self, cfg: &Config) -> Result<()> { + todo!() + } + async fn push_release(&mut self) -> Result<()> { + todo!() + } +} + +impl Gitea { + pub async fn build(cfg: &Config) -> Result { + todo!() + } +} diff --git a/src/serverapi/github.rs b/src/serverapi/github.rs new file mode 100644 index 0000000..6475e08 --- /dev/null +++ b/src/serverapi/github.rs @@ -0,0 +1,24 @@ +use async_trait::async_trait; + +use super::ServerApi; +use crate::{ + config::{ApiType, Config}, + error::*, +}; +pub struct Github; + +#[async_trait] +impl ServerApi for Github { + async fn init(&mut self, cfg: &Config) -> Result<()> { + todo!() + } + async fn push_release(&mut self) -> Result<()> { + todo!() + } +} + +impl Github { + pub async fn build(cfg: &Config) -> Result { + todo!() + } +} diff --git a/src/serverapi/gitlab.rs b/src/serverapi/gitlab.rs new file mode 100644 index 0000000..c0d8bfc --- /dev/null +++ b/src/serverapi/gitlab.rs @@ -0,0 +1,24 @@ +use async_trait::async_trait; + +use super::ServerApi; +use crate::{ + config::{ApiType, Config}, + error::*, +}; +pub struct Gitlab; + +#[async_trait] +impl ServerApi for Gitlab { + async fn init(&mut self, cfg: &Config) -> Result<()> { + todo!() + } + async fn push_release(&mut self) -> Result<()> { + todo!() + } +} + +impl Gitlab { + pub async fn build(cfg: &Config) -> Result { + todo!() + } +} diff --git a/src/serverapi/mod.rs b/src/serverapi/mod.rs index e69de29..c4c7d4c 100644 --- a/src/serverapi/mod.rs +++ b/src/serverapi/mod.rs @@ -0,0 +1,42 @@ +use async_trait::async_trait; + +use crate::{ + config::{ApiType, Config}, + error::*, +}; + +pub mod gitea; +pub mod github; +pub mod gitlab; +use gitea::*; +use github::*; +use gitlab::*; + +pub type ApiCollection = Vec>; + +// NOTE: in stable rust, traits can normally not contain async methods, +// see [here](https://stackoverflow.com/questions/65921581/how-can-i-define-an-async-method-in-a-trait). +// The `async_trait` crate can be used to work around this limitation. +#[async_trait] +pub trait ServerApi { + async fn init(&mut self, cfg: &Config) -> Result<()>; + async fn push_release(&mut self) -> Result<()>; +} + +pub async fn init_servers(cfg: &Config) -> Result { + let mut collection: ApiCollection = ApiCollection::new(); + for api in &cfg.yaml.api { + match api.1.server_type { + ApiType::Gitea => { + collection.push(Box::new(Gitea::build(cfg).await?)); + } + ApiType::Gitlab => { + collection.push(Box::new(Gitlab::build(cfg).await?)); + } + ApiType::Github => { + collection.push(Box::new(Github::build(cfg).await?)); + } + } + } + Ok(collection) +}