Skip to content

Commit 355f51d

Browse files
authored
Add general CLI structure (#9)
* Add top level CLI * Add `GitProvider` enum * Remove redundant group * Create module folder for CLI * Add subcommand to manage allowed signers * Add arguments to ManageSigners command * Add subcommand to manage sources
1 parent a8b9d7a commit 355f51d

9 files changed

+203
-2
lines changed

Cargo.lock

+82
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ edition = "2021"
88
[dependencies]
99
async-trait = "0.1.79"
1010
chrono = "0.4.37"
11+
clap = { version = "4.5.4", features = ["derive"] }
1112
reqwest = { version = "0.12.2", features = ["json"] }
1213
serde = { version = "1.0.197", features = ["derive"] }
1314
tokio = { version = "1.37.0", features = ["full"] }

src/cli/main.rs

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use super::{manage_signers::ManageSigners, manage_sources::ManageSources};
2+
use clap::{Args, Parser, Subcommand};
3+
use std::path::PathBuf;
4+
5+
#[derive(Parser)]
6+
#[command(version, about, long_about = None)]
7+
pub struct Cli {
8+
/// The path to the configuration file.
9+
#[arg(short, long, value_name = "FILE")]
10+
config: Option<PathBuf>,
11+
12+
#[command(flatten)]
13+
logging: Logging,
14+
15+
#[command(subcommand)]
16+
command: Commands,
17+
}
18+
19+
#[derive(Args)]
20+
#[group(multiple = false)]
21+
struct Logging {
22+
/// Enable verbose logging.
23+
#[arg(short, long)]
24+
verbose: bool,
25+
26+
/// Disable all output.
27+
#[arg(long)]
28+
silent: bool,
29+
}
30+
31+
#[derive(Subcommand)]
32+
enum Commands {
33+
/// Update the allowed signers file.
34+
Update,
35+
36+
/// Manage signers.
37+
#[command(subcommand)]
38+
Signer(ManageSigners),
39+
40+
/// Manage sources.
41+
#[command(subcommand)]
42+
Source(ManageSources),
43+
}
44+
45+
#[cfg(test)]
46+
mod tests {
47+
use super::*;
48+
49+
#[test]
50+
fn verify_cli() {
51+
use clap::CommandFactory;
52+
Cli::command().debug_assert()
53+
}
54+
}

src/cli/manage_signers.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use clap::{Args, Subcommand};
2+
3+
#[derive(Subcommand)]
4+
pub enum ManageSigners {
5+
/// Add allowed signers.
6+
Add {
7+
#[command(flatten)]
8+
signers: Signers,
9+
/// The source(s) of the signer(s) to add.
10+
#[arg(short, long)]
11+
source: Vec<String>,
12+
},
13+
/// Remove allowed signers.
14+
Remove {
15+
#[command(flatten)]
16+
signers: Signers,
17+
},
18+
}
19+
20+
#[derive(Args)]
21+
#[group(multiple = true)]
22+
pub struct Signers {
23+
/// By username.
24+
#[arg(short, long)]
25+
user: Vec<String>,
26+
/// By organization.
27+
#[arg(short, long, value_name = "ORGANIZATION")]
28+
org: Vec<String>,
29+
}

src/cli/manage_sources.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use crate::GitProvider;
2+
use clap::Subcommand;
3+
4+
#[derive(Subcommand)]
5+
pub enum ManageSources {
6+
/// Add sources.
7+
Add {
8+
/// The name of the source.
9+
name: String,
10+
/// The Git provider used by the source.
11+
#[arg(short, long)]
12+
provider: GitProvider,
13+
/// The URL of the source.
14+
#[arg(short, long)]
15+
url: Option<reqwest::Url>,
16+
},
17+
/// Remove sources.
18+
Remove { name: Vec<String> },
19+
}

src/cli/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub use main::Cli;
2+
3+
mod main;
4+
mod manage_signers;
5+
mod manage_sources;

src/core.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use async_trait::async_trait;
2-
use serde::Deserialize;
2+
use serde::{Deserialize, Serialize};
33
use std::{fmt, str::FromStr};
44

55
pub const USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
@@ -30,3 +30,10 @@ pub trait GetPublicKeys {
3030
/// Get the public keys of a user by their username.
3131
async fn by_username(&self, username: &str) -> Result<Vec<SshPublicKey>, Self::Err>;
3232
}
33+
34+
/// A Git provider.
35+
#[derive(Debug, Clone, Copy, Deserialize, Serialize, clap::ValueEnum)]
36+
pub enum GitProvider {
37+
Github,
38+
Gitlab,
39+
}

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pub use allowed_signers::{AllowedSigner, AllowedSignersFile};
22
pub use core::*;
33

44
mod allowed_signers;
5+
pub mod cli;
56
mod core;
67
mod github;
78
mod gitlab;

src/main.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use clap::Parser;
2+
use hanko::cli::Cli;
3+
14
fn main() {
2-
println!("Hello, world!");
5+
let _cli = Cli::parse();
36
}

0 commit comments

Comments
 (0)