Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic config parsing #10

Merged
merged 10 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ edition = "2021"
[dependencies]
async-trait = "0.1.79"
chrono = "0.4.37"
clap = { version = "4.5.4", features = ["derive"] }
clap = { version = "4.5.4", features = ["derive", "env", "string"] }
figment = { version = "0.10.16", features = ["toml"] }
reqwest = { version = "0.12.2", features = ["json"] }
serde = { version = "1.0.197", features = ["derive"] }
tokio = { version = "1.37.0", features = ["full"] }
Expand All @@ -17,9 +18,14 @@ tokio = { version = "1.37.0", features = ["full"] }
codspeed-criterion-compat = "2.4.1"
criterion = "0.5.1"
httpmock = "0.7.0"
indoc = "2.0.5"
rstest = "0.19.0"
tempfile = "3.10.1"

[[bench]]
name = "write_allowed_signers"
harness = false

[[bench]]
name = "load_configuration"
harness = false
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Example Configuration

```toml
users = [
{ name = "torvalds", sources = ["github"] },
{ name = "gvanrossum", sources = ["github", "gitlab"] },
{ name = "graydon", sources = ["github"] },
{ name = "cwoods", sources = ["acme-corp"] },
{ name = "rdavis", sources = ["acme-corp"] },
{ name = "pbrock", sources = ["acme-corp"] }
]
organizations = [
{ name = "rust-lang", sources = ["github"] }
]
local = [
"jdoe@example.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJHDGMF+tZQL3dcr1arPst+YP8v33Is0kAJVvyTKrxMw"
]

[[sources]]
name = "acme-corp"
provider = "gitlab"
url = "https://git.acme.corp"
```
38 changes: 38 additions & 0 deletions benches/load_configuration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use codspeed_criterion_compat::{criterion_group, criterion_main, Criterion};
use hanko::Config;
use indoc::indoc;
use std::{io::Write, path::Path};

pub fn criterion_benchmark(c: &mut Criterion) {
let toml = indoc! {r#"
users = [
{ name = "torvalds", sources = ["github"] },
{ name = "gvanrossum", sources = ["github", "gitlab"] },
{ name = "graydon", sources = ["github"] },
{ name = "cwoods", sources = ["acme-corp"] },
{ name = "rdavis", sources = ["acme-corp"] },
{ name = "pbrock", sources = ["acme-corp"] }
]
organizations = [
{ name = "rust-lang", sources = ["github"] }
]
local = [
"jdoe@example.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJHDGMF+tZQL3dcr1arPst+YP8v33Is0kAJVvyTKrxMw"
]

[[sources]]
name = "acme-corp"
provider = "gitlab"
url = "https://git.acme.corp"
"#};
let mut file = tempfile::NamedTempFile::new().unwrap();
file.write_all(toml.as_bytes()).unwrap();
let path: &Path = &file.into_temp_path();

c.bench_function("load the example configuration", |b| {
b.iter(|| Config::load(path).unwrap());
});
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
32 changes: 28 additions & 4 deletions src/cli/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
use super::{manage_signers::ManageSigners, manage_sources::ManageSources};
use clap::{Args, Parser, Subcommand};
use std::path::PathBuf;
use clap::{
builder::{OsStr, Resettable},
Args, Parser, Subcommand,
};
use std::{env, path::PathBuf};

#[derive(Parser)]
#[command(version, about, long_about = None)]
pub struct Cli {
/// The path to the configuration file.
#[arg(short, long, value_name = "FILE")]
config: Option<PathBuf>,
#[arg(
short,
long,
value_name = "FILE",
env = "HANKO_CONFIG",
default_value = default_config_path()
)]
pub config: PathBuf,

#[command(flatten)]
logging: Logging,
Expand Down Expand Up @@ -42,6 +51,21 @@ enum Commands {
Source(ManageSources),
}

/// The default configuration file path according to the XDG Base Directory Specification.
/// If neither `$XDG_CONFIG_HOME` nor `$HOME` are set, `Resettable::Reset` is returned, forcing the user to specify the path.
fn default_config_path() -> Resettable<OsStr> {
let dirname = env!("CARGO_PKG_NAME");
let filename = "config.toml";

if let Ok(xdg_config_home) = env::var("XDG_CONFIG_HOME") {
Resettable::Value(format!("{}/{}/{}", xdg_config_home, dirname, filename).into())
} else if let Ok(home) = env::var("HOME") {
Resettable::Value(format!("{}/.config/{}/{}", home, dirname, filename).into())
} else {
Resettable::Reset
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading