From e3b3c2ddac5d29a93afe40859636baf3ec32bb2d Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sat, 24 Feb 2024 13:24:14 +0100 Subject: [PATCH] pass enum --- .autocrate.yaml | 14 ++++++---- src/config/cli.rs | 13 +++++++++ src/config/mod.rs | 59 ++++++++++++++++++++++++++++++---------- src/error.rs | 6 +++- src/main.rs | 4 +++ src/release/mod.rs | 2 +- src/serverapi/forgejo.rs | 4 +++ src/serverapi/gitea.rs | 3 ++ src/serverapi/github.rs | 3 ++ src/serverapi/gitlab.rs | 3 ++ 10 files changed, 89 insertions(+), 22 deletions(-) diff --git a/.autocrate.yaml b/.autocrate.yaml index 9797223..52b1079 100644 --- a/.autocrate.yaml +++ b/.autocrate.yaml @@ -16,11 +16,13 @@ api: type: github endpoint: https://github.com auth: - user: myUserName - pass: token_superimportantsecret - myserv: - type: gitea + user: PlexSheep + pass: + env: TOKEN_GH + cscherr: + type: forgejo endpoint: https://git.cscherr.de auth: - user: myUserName - pass: importantsecrettoken + user: PlexSheep + pass: + env: TOKEN_CSCHERR diff --git a/src/config/cli.rs b/src/config/cli.rs index 554b911..1f977a7 100644 --- a/src/config/cli.rs +++ b/src/config/cli.rs @@ -38,6 +38,7 @@ pub struct Cli { #[derive(Debug, Clone, Subcommand)] pub enum Commands { Changelog {}, + /// Create a new release on the server Release { // FIXME: allow taking a message like this: // `autocrate changelog -m arg1 arg2 arg3` @@ -55,15 +56,26 @@ pub enum Commands { // // TODO: // integrate a CHANGELOG.md file + // + /// Message body of the release #[arg(short, long)] message: Option>, + + /// generate and add a changelog + changelog: bool, + + /// publish after releasing + publish: bool, }, + /// Publish to a package registry Publish { // see Commands::Release { message } #[arg(short, long)] message: Option>, }, + /// Version {}, + Init {}, } impl Display for Commands { @@ -76,6 +88,7 @@ impl Display for Commands { Self::Release { .. } => "Release", Self::Publish { .. } => "Publish", Self::Version { .. } => "Version", + Self::Init { .. } => "Init", } ) } diff --git a/src/config/mod.rs b/src/config/mod.rs index 6f8bb45..0e5e7b9 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -9,8 +9,8 @@ use crate::error::*; pub mod cli; pub mod packages; -use packages::*; use cli::Cli; +use packages::*; pub trait YamlConfigSection: Debug + Clone + for<'a> Deserialize<'a> { fn check(&self) -> Result<()>; @@ -50,25 +50,56 @@ impl YamlConfigSection for Uses { } } +#[derive(Debug, Clone, Deserialize)] +pub enum Pass { + /// pass specified as plainext + Text(String), + /// pass to be loaded from an env var + Env(String), + /// pass to be loaded from a file + File(PathBuf), +} +impl Pass { + /// Get the pass, extracting from the underlying source + fn get_pass(&self) -> Result { + self.check()?; + Ok(match self { + Self::Text(pass) => pass.clone(), + Self::Env(key) => std::env::var(key).map_err(|err| ConfigError::from(err))?, + Self::File(file) => std::fs::read_to_string(file)?, + }) + } +} +impl YamlConfigSection for Pass { + fn check(&self) -> Result<()> { + match self { + Self::Text(_) => (), + Self::Env(envvar) => { + if !std::env::var(envvar) + .map_err(ConfigError::from)? + .is_empty() + { + } else { + return Err(ConfigError::EnvNotSet(envvar.clone()).into()); + } + } + Self::File(file) => { + if !file.exists() { + return Err(ConfigError::PassFileDoesNotExist(file.clone()).into()); + } + } + }; + Ok(()) + } +} #[derive(Debug, Clone, Deserialize)] pub struct ApiAuth { pub user: String, - pub pass: Option, - pub pass_file: Option, + pub pass: Pass, } impl YamlConfigSection for ApiAuth { fn check(&self) -> Result<()> { - if self.pass.is_some() && self.pass_file.is_some() { - let err = ConfigError::YamlApiAuthBothPass(self.clone()).into(); - error!("{err}"); - return Err(err); - } - if self.pass_file.is_some() { - let file = self.pass_file.clone().unwrap(); - if !file.exists() { - return Err(ConfigError::PassFileDoesNotExist(file).into()); - } - } + self.pass.check()?; Ok(()) } } diff --git a/src/error.rs b/src/error.rs index deb1e8a..e0d5b06 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,4 +1,4 @@ -use std::{path::PathBuf, process::ExitStatus, string::FromUtf8Error}; +use std::{env::VarError, path::PathBuf, process::ExitStatus, string::FromUtf8Error}; use anyhow; use thiserror::Error; @@ -45,4 +45,8 @@ pub enum ConfigError { YamlApiAuthBothPass(ApiAuth), #[error("password provided as file, but does not exist: {0}")] PassFileDoesNotExist(PathBuf), + #[error("config requires environment variable {0}, but {0} is not set")] + EnvNotSet(String), + #[error("Bad value for environment variable: {0}")] + BadEnv(#[from] VarError), } diff --git a/src/main.rs b/src/main.rs index 98641ae..81770d0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,6 +32,10 @@ async fn main() -> Result<()> { // TODO: version select automated todo!() } + Commands::Init { .. } => { + // TODO: create a basic autocrate yaml + todo!() + } }; Ok(()) } diff --git a/src/release/mod.rs b/src/release/mod.rs index 4f51099..9c8eee9 100644 --- a/src/release/mod.rs +++ b/src/release/mod.rs @@ -1,6 +1,6 @@ use crate::{config::Config, error::*, serverapi::ApiCollection}; -pub async fn release(_cfg: &Config, _apis: &mut ApiCollection) -> Result<()> { +pub async fn release(cfg: &Config, apis: &mut ApiCollection) -> Result<()> { // TODO: git tag // TODO: push to each server diff --git a/src/serverapi/forgejo.rs b/src/serverapi/forgejo.rs index e854a04..011eeba 100644 --- a/src/serverapi/forgejo.rs +++ b/src/serverapi/forgejo.rs @@ -15,9 +15,13 @@ impl ServerApi for Forgejo { async fn push_release(&mut self) -> Result<()> { todo!() } + async fn push_release_artifact(&mut self) -> Result<()> { + todo!() + } async fn push_pkg(&mut self, pkg_type: PackageType) -> Result<()> { todo!() } + } impl Forgejo { diff --git a/src/serverapi/gitea.rs b/src/serverapi/gitea.rs index 7c74584..425fd74 100644 --- a/src/serverapi/gitea.rs +++ b/src/serverapi/gitea.rs @@ -15,6 +15,9 @@ impl ServerApi for Gitea { async fn push_release(&mut self) -> Result<()> { todo!() } + async fn push_release_artifact(&mut self) -> Result<()> { + todo!() + } async fn push_pkg(&mut self, pkg_type: PackageType) -> Result<()> { todo!() } diff --git a/src/serverapi/github.rs b/src/serverapi/github.rs index a7be38a..1456dff 100644 --- a/src/serverapi/github.rs +++ b/src/serverapi/github.rs @@ -15,6 +15,9 @@ impl ServerApi for Github { async fn push_release(&mut self) -> Result<()> { todo!() } + async fn push_release_artifact(&mut self) -> Result<()> { + todo!() + } async fn push_pkg(&mut self, pkg_type: PackageType) -> Result<()> { todo!() } diff --git a/src/serverapi/gitlab.rs b/src/serverapi/gitlab.rs index 2d47f34..fc330bd 100644 --- a/src/serverapi/gitlab.rs +++ b/src/serverapi/gitlab.rs @@ -15,6 +15,9 @@ impl ServerApi for Gitlab { async fn push_release(&mut self) -> Result<()> { todo!() } + async fn push_release_artifact(&mut self) -> Result<()> { + todo!() + } async fn push_pkg(&mut self, pkg_type: PackageType) -> Result<()> { todo!() }