From f4180d9a3e8b3b99fade5a452ecf825d3fe40598 Mon Sep 17 00:00:00 2001 From: Marvin Vogt Date: Sun, 14 Apr 2024 13:10:37 +0000 Subject: [PATCH 1/7] Add top level CLI --- Cargo.lock | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/cli.rs | 51 +++++++++++++++++++++++++++++++++ src/lib.rs | 1 + src/main.rs | 5 +++- 5 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 src/cli.rs diff --git a/Cargo.lock b/Cargo.lock index 59a3d10..105d17a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -47,12 +47,54 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" +[[package]] +name = "anstream" +version = "0.6.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d96bd03f33fe50a863e394ee9718a706f988b9079b20c3784fb726e7678b62fb" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "utf8parse", +] + [[package]] name = "anstyle" version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8901269c6307e8d93993578286ac0edf7f195079ffff5ebdeea6a59ffb7e36bc" +[[package]] +name = "anstyle-parse" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7" +dependencies = [ + "anstyle", + "windows-sys 0.52.0", +] + [[package]] name = "ascii-canvas" version = "3.0.0" @@ -454,6 +496,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0" dependencies = [ "clap_builder", + "clap_derive", ] [[package]] @@ -462,8 +505,22 @@ version = "4.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4" dependencies = [ + "anstream", "anstyle", "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.58", ] [[package]] @@ -494,6 +551,12 @@ dependencies = [ "criterion", ] +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "colored" version = "2.1.0" @@ -960,6 +1023,7 @@ version = "0.1.0" dependencies = [ "async-trait", "chrono", + "clap", "codspeed-criterion-compat", "criterion", "httpmock", @@ -976,6 +1040,12 @@ version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "hermit-abi" version = "0.3.9" @@ -2092,6 +2162,12 @@ dependencies = [ "precomputed-hash", ] +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "syn" version = "1.0.109" @@ -2364,6 +2440,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "value-bag" version = "1.8.1" diff --git a/Cargo.toml b/Cargo.toml index 4cbd93f..31b6ba7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ edition = "2021" [dependencies] async-trait = "0.1.79" chrono = "0.4.37" +clap = { version = "4.5.4", features = ["derive"] } reqwest = { version = "0.12.2", features = ["json"] } serde = { version = "1.0.197", features = ["derive"] } tokio = { version = "1.37.0", features = ["full"] } diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..42f0874 --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,51 @@ +use clap::{Args, Parser, Subcommand}; +use std::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, + + #[command(flatten)] + logging: Logging, + + #[command(subcommand)] + command: Commands, +} + +#[derive(Args)] +#[group(multiple = false)] +struct Logging { + /// Enable verbose logging. + #[arg(short, long, group = "logging")] + verbose: bool, + + /// Disable all output. + #[arg(long, group = "logging")] + silent: bool, +} + +#[derive(Subcommand)] +enum Commands { + /// Update the allowed signers file. + Update, + + /// Add allowed signers. + Add, + + /// Remove allowed signers. + Remove, +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn verify_cli() { + use clap::CommandFactory; + Cli::command().debug_assert() + } +} diff --git a/src/lib.rs b/src/lib.rs index 32da080..dd2d969 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ pub use allowed_signers::{AllowedSigner, AllowedSignersFile}; pub use core::*; mod allowed_signers; +pub mod cli; mod core; mod github; mod gitlab; diff --git a/src/main.rs b/src/main.rs index e7a11a9..add0606 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,6 @@ +use clap::Parser; +use hanko::cli::Cli; + fn main() { - println!("Hello, world!"); + let _cli = Cli::parse(); } From a620619d046517b8dcf4b216f546f90976546803 Mon Sep 17 00:00:00 2001 From: Marvin Vogt Date: Sun, 14 Apr 2024 13:49:37 +0000 Subject: [PATCH 2/7] Add `GitProvider` enum --- src/core.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/core.rs b/src/core.rs index 9e05fac..1a79103 100644 --- a/src/core.rs +++ b/src/core.rs @@ -1,5 +1,5 @@ use async_trait::async_trait; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use std::{fmt, str::FromStr}; pub const USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")); @@ -30,3 +30,10 @@ pub trait GetPublicKeys { /// Get the public keys of a user by their username. async fn by_username(&self, username: &str) -> Result, Self::Err>; } + +/// A Git provider. +#[derive(Debug, Clone, Copy, Deserialize, Serialize)] +pub enum GitProvider { + Github, + Gitlab, +} From 21e275e450c624ce679b0d66a1b9c97d25dbf236 Mon Sep 17 00:00:00 2001 From: Marvin Vogt Date: Sun, 14 Apr 2024 13:51:01 +0000 Subject: [PATCH 3/7] Remove redundant group --- src/cli.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 42f0874..c1fa895 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -19,11 +19,11 @@ pub struct Cli { #[group(multiple = false)] struct Logging { /// Enable verbose logging. - #[arg(short, long, group = "logging")] + #[arg(short, long)] verbose: bool, /// Disable all output. - #[arg(long, group = "logging")] + #[arg(long)] silent: bool, } From ff7b69833f5445247075c049cffc088fe9474d57 Mon Sep 17 00:00:00 2001 From: Marvin Vogt Date: Sun, 14 Apr 2024 13:58:40 +0000 Subject: [PATCH 4/7] Create module folder for CLI --- src/{cli.rs => cli/main.rs} | 0 src/cli/mod.rs | 3 +++ 2 files changed, 3 insertions(+) rename src/{cli.rs => cli/main.rs} (100%) create mode 100644 src/cli/mod.rs diff --git a/src/cli.rs b/src/cli/main.rs similarity index 100% rename from src/cli.rs rename to src/cli/main.rs diff --git a/src/cli/mod.rs b/src/cli/mod.rs new file mode 100644 index 0000000..38452b8 --- /dev/null +++ b/src/cli/mod.rs @@ -0,0 +1,3 @@ +pub use main::Cli; + +mod main; From 6c25c94639cccc3299016e6350c5012e0fe54259 Mon Sep 17 00:00:00 2001 From: Marvin Vogt Date: Sun, 14 Apr 2024 14:23:13 +0000 Subject: [PATCH 5/7] Add subcommand to manage allowed signers --- src/cli/main.rs | 9 ++++----- src/cli/manage_signers.rs | 9 +++++++++ src/cli/mod.rs | 1 + 3 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 src/cli/manage_signers.rs diff --git a/src/cli/main.rs b/src/cli/main.rs index c1fa895..7f12190 100644 --- a/src/cli/main.rs +++ b/src/cli/main.rs @@ -1,3 +1,4 @@ +use super::manage_signers::ManageSigners; use clap::{Args, Parser, Subcommand}; use std::path::PathBuf; @@ -32,11 +33,9 @@ enum Commands { /// Update the allowed signers file. Update, - /// Add allowed signers. - Add, - - /// Remove allowed signers. - Remove, + /// Manage signers. + #[command(subcommand)] + Signer(ManageSigners), } #[cfg(test)] diff --git a/src/cli/manage_signers.rs b/src/cli/manage_signers.rs new file mode 100644 index 0000000..b240fbd --- /dev/null +++ b/src/cli/manage_signers.rs @@ -0,0 +1,9 @@ +use clap::Subcommand; + +#[derive(Subcommand)] +pub enum ManageSigners { + /// Add an allowed signer. + Add, + /// Remove an allowed signer. + Remove, +} diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 38452b8..5387622 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -1,3 +1,4 @@ pub use main::Cli; mod main; +mod manage_signers; From 63108d6ce1ba5fe437250ceaaf46ac55b3ffdbb8 Mon Sep 17 00:00:00 2001 From: Marvin Vogt Date: Sun, 14 Apr 2024 15:41:51 +0000 Subject: [PATCH 6/7] Add arguments to ManageSigners command --- src/cli/manage_signers.rs | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/cli/manage_signers.rs b/src/cli/manage_signers.rs index b240fbd..e66d6da 100644 --- a/src/cli/manage_signers.rs +++ b/src/cli/manage_signers.rs @@ -1,9 +1,29 @@ -use clap::Subcommand; +use clap::{Args, Subcommand}; #[derive(Subcommand)] pub enum ManageSigners { - /// Add an allowed signer. - Add, - /// Remove an allowed signer. - Remove, + /// Add allowed signers. + Add { + #[command(flatten)] + signers: Signers, + /// The source(s) of the signer(s) to add. + #[arg(short, long)] + source: Vec, + }, + /// Remove allowed signers. + Remove { + #[command(flatten)] + signers: Signers, + }, +} + +#[derive(Args)] +#[group(multiple = true)] +pub struct Signers { + /// By username. + #[arg(short, long)] + user: Vec, + /// By organization. + #[arg(short, long, value_name = "ORGANIZATION")] + org: Vec, } From a3a74d105fa8aa9372c495fbca8c897a49286063 Mon Sep 17 00:00:00 2001 From: Marvin Vogt Date: Sun, 14 Apr 2024 15:55:30 +0000 Subject: [PATCH 7/7] Add subcommand to manage sources --- src/cli/main.rs | 6 +++++- src/cli/manage_sources.rs | 19 +++++++++++++++++++ src/cli/mod.rs | 1 + src/core.rs | 2 +- 4 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 src/cli/manage_sources.rs diff --git a/src/cli/main.rs b/src/cli/main.rs index 7f12190..866be38 100644 --- a/src/cli/main.rs +++ b/src/cli/main.rs @@ -1,4 +1,4 @@ -use super::manage_signers::ManageSigners; +use super::{manage_signers::ManageSigners, manage_sources::ManageSources}; use clap::{Args, Parser, Subcommand}; use std::path::PathBuf; @@ -36,6 +36,10 @@ enum Commands { /// Manage signers. #[command(subcommand)] Signer(ManageSigners), + + /// Manage sources. + #[command(subcommand)] + Source(ManageSources), } #[cfg(test)] diff --git a/src/cli/manage_sources.rs b/src/cli/manage_sources.rs new file mode 100644 index 0000000..29cd462 --- /dev/null +++ b/src/cli/manage_sources.rs @@ -0,0 +1,19 @@ +use crate::GitProvider; +use clap::Subcommand; + +#[derive(Subcommand)] +pub enum ManageSources { + /// Add sources. + Add { + /// The name of the source. + name: String, + /// The Git provider used by the source. + #[arg(short, long)] + provider: GitProvider, + /// The URL of the source. + #[arg(short, long)] + url: Option, + }, + /// Remove sources. + Remove { name: Vec }, +} diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 5387622..65f464a 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -2,3 +2,4 @@ pub use main::Cli; mod main; mod manage_signers; +mod manage_sources; diff --git a/src/core.rs b/src/core.rs index 1a79103..4f1d385 100644 --- a/src/core.rs +++ b/src/core.rs @@ -32,7 +32,7 @@ pub trait GetPublicKeys { } /// A Git provider. -#[derive(Debug, Clone, Copy, Deserialize, Serialize)] +#[derive(Debug, Clone, Copy, Deserialize, Serialize, clap::ValueEnum)] pub enum GitProvider { Github, Gitlab,