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 general CLI structure #9

Merged
merged 7 commits into from
Apr 14, 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
82 changes: 82 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
54 changes: 54 additions & 0 deletions src/cli/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use super::{manage_signers::ManageSigners, manage_sources::ManageSources};
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<PathBuf>,

#[command(flatten)]
logging: Logging,

#[command(subcommand)]
command: Commands,
}

#[derive(Args)]
#[group(multiple = false)]
struct Logging {
/// Enable verbose logging.
#[arg(short, long)]
verbose: bool,

/// Disable all output.
#[arg(long)]
silent: bool,
}

#[derive(Subcommand)]
enum Commands {
/// Update the allowed signers file.
Update,

/// Manage signers.
#[command(subcommand)]
Signer(ManageSigners),

/// Manage sources.
#[command(subcommand)]
Source(ManageSources),
}

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

#[test]
fn verify_cli() {
use clap::CommandFactory;
Cli::command().debug_assert()
}
}
29 changes: 29 additions & 0 deletions src/cli/manage_signers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use clap::{Args, Subcommand};

#[derive(Subcommand)]
pub enum ManageSigners {
/// Add allowed signers.
Add {
#[command(flatten)]
signers: Signers,
/// The source(s) of the signer(s) to add.
#[arg(short, long)]
source: Vec<String>,
},
/// Remove allowed signers.
Remove {
#[command(flatten)]
signers: Signers,
},
}

#[derive(Args)]
#[group(multiple = true)]
pub struct Signers {
/// By username.
#[arg(short, long)]
user: Vec<String>,
/// By organization.
#[arg(short, long, value_name = "ORGANIZATION")]
org: Vec<String>,
}
19 changes: 19 additions & 0 deletions src/cli/manage_sources.rs
Original file line number Diff line number Diff line change
@@ -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<reqwest::Url>,
},
/// Remove sources.
Remove { name: Vec<String> },
}
5 changes: 5 additions & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub use main::Cli;

mod main;
mod manage_signers;
mod manage_sources;
9 changes: 8 additions & 1 deletion src/core.rs
Original file line number Diff line number Diff line change
@@ -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"));
Expand Down Expand Up @@ -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<Vec<SshPublicKey>, Self::Err>;
}

/// A Git provider.
#[derive(Debug, Clone, Copy, Deserialize, Serialize, clap::ValueEnum)]
pub enum GitProvider {
Github,
Gitlab,
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use clap::Parser;
use hanko::cli::Cli;

fn main() {
println!("Hello, world!");
let _cli = Cli::parse();
}