get going with some configs
cargo devel CI / cargo CI (push) Successful in 2m20s Details

This commit is contained in:
Christoph J. Scherr 2024-01-24 23:04:01 +01:00
parent 78c9dd474f
commit a2c699bb98
Signed by: PlexSheep
GPG Key ID: 7CDD0B14851A08EF
7 changed files with 207 additions and 9 deletions

25
.autocrate.yaml Normal file
View File

@ -0,0 +1,25 @@
changelog:
enable: true
git-log: true
uses:
cargo:
publish: true
# tokens are loaded from ~/.cargo/config.toml
registries:
- crates.io
- example.com
api:
github:
type: github
endpoint: https://github.com
auth:
user: myUserName
pass: token_superimportantsecret
myserv:
type: gitea
endpoint: https://git.example.com
auth:
user: myUserName
pass: importantsecrettoken

View File

@ -20,12 +20,15 @@ keywords = [
[dependencies] [dependencies]
anyhow = "1.0.79"
clap = { version = "4.4.18", features = ["derive"] } clap = { version = "4.4.18", features = ["derive"] }
clap-num = "1.1.1" clap-num = "1.1.1"
clap-verbosity-flag = "2.1.2" clap-verbosity-flag = "2.1.2"
git2 = "0.18.1"
libpt = { version = "0.3.11", features = ["log"] } libpt = { version = "0.3.11", features = ["log"] }
serde = { version = "1.0.195", features = ["derive"] } serde = { version = "1.0.195", features = ["derive"] }
serde_yaml = "0.9.30" serde_yaml = "0.9.30"
thiserror = "1.0.56"
[[bin]] [[bin]]
name = "autocrate" name = "autocrate"

View File

@ -85,21 +85,30 @@ repository. It should contain the following parameters (replace the placeholders
An example `.autocrate.yaml` could look like this: An example `.autocrate.yaml` could look like this:
```yaml ```yaml
changelog: changelog:
- enable: true enable: true
- git-log: true git-log: true
uses: uses:
- cargo: cargo:
- publish = true publish: true
# tokens are loaded from ~/.cargo/config.toml # tokens are loaded from ~/.cargo/config.toml
- registries: registries:
- crates.io - crates.io
- example.com - example.com
api: api:
github: github:
user: myUserName type: github
pass: token_superimportantsecret endpoint: https://github.com
auth:
user: myUserName
pass: token_superimportantsecret
myserv:
type: gitea
endpoint: https://git.example.com
auth:
user: myUserName
pass: importantsecrettoken
``` ```
## Using Autocrate ## Using Autocrate

65
src/config/cli.rs Normal file
View File

@ -0,0 +1,65 @@
use serde;
use serde_yaml;
use std::path::PathBuf;
use libpt::log::{Level, Logger};
use clap::Parser;
use clap_verbosity_flag::{InfoLevel, Verbosity};
/// short about section displayed in help
const ABOUT_ROOT: &'static str = r##"
Release Manager for Your Projects on Gitea, GitHub, and GitLab.
"##;
/// longer about section displayed in help, is combined with [the short help](ABOUT_ROOT)
static LONG_ABOUT_ROOT: &'static str = r##""##;
#[derive(Debug, Clone, Parser)]
#[command(
author,
version,
about = ABOUT_ROOT,
long_about = format!("{}{}", ABOUT_ROOT ,LONG_ABOUT_ROOT),
help_template =
r#"{about-section}
{usage-heading} {usage}
{all-args}{tab}
autocrate: {version}
Author: {author-with-newline}
"#
)]
pub struct Cli {
// clap_verbosity_flag seems to make this a global option implicitly
/// set a verbosity, multiple allowed (f.e. -vvv)
#[command(flatten)]
pub(crate) verbose: Verbosity<InfoLevel>,
/// show additional logging meta data
#[arg(long)]
pub(crate) meta: bool,
}
impl Cli {
pub fn cli_parse() -> Self {
let cli = Self::parse();
let ll: Level = match cli.verbose.log_level().unwrap().as_str() {
"TRACE" => Level::TRACE,
"DEBUG" => Level::DEBUG,
"INFO" => Level::INFO,
"WARN" => Level::WARN,
"ERROR" => Level::ERROR,
_ => {
unreachable!();
}
};
if cli.meta {
Logger::init(None, Some(ll), true).expect("could not initialize Logger");
} else {
// less verbose version
Logger::init_mini(Some(ll)).expect("could not initialize Logger");
}
return cli;
}
}

86
src/config/mod.rs Normal file
View File

@ -0,0 +1,86 @@
use std::{
fmt::{write, Debug}, fs::File, path::PathBuf
};
use anyhow::{anyhow, Result};
use git2;
use libpt::log::error;
use serde::Deserialize;
use self::cli::Cli;
pub mod cli;
#[derive(Debug, Clone, Deserialize)]
pub struct Changelog {
enabled: bool,
git_log: bool,
}
#[derive(Debug, Clone, Deserialize)]
pub struct Uses {
cargo: Option<Cargo>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct Cargo {
publish: bool,
registries: Vec<String>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct Api {
api_type: ApiType,
publish: bool,
registries: Vec<String>,
}
#[derive(Debug, Clone, Deserialize)]
pub enum ApiType {
Gitea,
Gitlab,
Github,
}
#[derive(Debug, Clone, Deserialize)]
pub struct YamlConfig {
pub changelog: Changelog,
pub uses: Uses,
pub apis: Vec<Api>,
}
pub struct Config {
pub yaml: YamlConfig,
pub repo: git2::Repository,
pub path: PathBuf,
}
impl Debug for Config {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
format!(
"Config {{yaml: {:?}, repo_path: {:?}}}",
self.yaml, self.path
)
)
}
}
impl Config {
pub fn load(cli: Cli) -> Result<Self> {
let repo = match git2::Repository::open_from_env() {
Ok(repo) => repo,
Err(err) => {
let msg = format!("could not find a git repository: {err:?}");
error!("{}", msg);
return Err(anyhow!(msg));
}
};
let mut path = repo.path().to_path_buf();
path.pop(); // we want the real root, not the `.git` dir
todo!()
}
}

1
src/lib.rs Normal file
View File

@ -0,0 +1 @@
pub mod config;

View File

@ -1,3 +1,12 @@
fn main() { use anyhow::Result;
println!("Hello, world!");
mod config;
use config::{cli::Cli, Config};
fn main() -> Result<()> {
let cli = Cli::cli_parse();
let config = Config::load(cli.clone())?;
dbg!(config);
todo!()
} }