From bf3add13a38fe68dae11158a870f931f5608cba3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Tue, 2 Apr 2024 10:46:01 +0800 Subject: [PATCH] =?UTF-8?q?feat(tools):=E5=A2=9E=E5=8A=A0=E7=94=9F?= =?UTF-8?q?=E6=88=90=E8=AF=81=E4=B9=A6=E6=96=87=E4=BB=B6=E7=9A=84=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E8=A1=8C=E5=8A=9F=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cert_tools/Cargo.toml | 5 ++++- cert_tools/src/generate_key.rs | 28 ++++++++++++++++++++++++++ cert_tools/src/main.rs | 36 ++++++++++++++++++++++++++++++++-- cert_tools/src/power_equal.rs | 9 +++++++++ 4 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 cert_tools/src/generate_key.rs create mode 100644 cert_tools/src/power_equal.rs diff --git a/cert_tools/Cargo.toml b/cert_tools/Cargo.toml index fe80359..9c35e88 100644 --- a/cert_tools/Cargo.toml +++ b/cert_tools/Cargo.toml @@ -6,4 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -cert_lib = { path = "../cert_lib" } \ No newline at end of file +cert_lib = { path = "../cert_lib" } +clap = { version = "4.5.4", features = ["derive"] } +serde = { version = "1.0.197", features = ["derive"] } +serde_json = "1.0.115" diff --git a/cert_tools/src/generate_key.rs b/cert_tools/src/generate_key.rs new file mode 100644 index 0000000..79af6f3 --- /dev/null +++ b/cert_tools/src/generate_key.rs @@ -0,0 +1,28 @@ +use clap::Args; + +#[derive(Debug, Args)] +pub struct GenerateKeyOptions { + #[arg(short, long, help = "set certificate file version")] + version: Option, + #[arg(short, long, help = "set where certification file stores")] + output_path: Option, + #[arg(short, long, help = "set certification serial number")] + serial: Option, + #[arg(short = 'p', long, help = "set certification validity periods")] + validity_periods: Option, + #[arg(help = "set certification file name")] + name: String, +} + +pub fn generate_key_file(options: GenerateKeyOptions) { + if let Err(e) = cert_lib::generate_certificate( + options.output_path.unwrap_or(String::from(".")).as_ref(), + &options.name, + 4096, + options.validity_periods.unwrap_or(365), + options.version.unwrap_or(1), + options.serial, + ) { + println!("Failed to generate certificate, due to: {}", e.to_string()); + } +} diff --git a/cert_tools/src/main.rs b/cert_tools/src/main.rs index e7a11a9..546e303 100644 --- a/cert_tools/src/main.rs +++ b/cert_tools/src/main.rs @@ -1,3 +1,35 @@ -fn main() { - println!("Hello, world!"); +use clap::{Parser, Subcommand}; +use generate_key::{generate_key_file, GenerateKeyOptions}; +use power_equal::PowerEqualResultOption; + +mod generate_key; +mod power_equal; + +#[derive(Debug, Parser)] +#[command( + name = "cert-tools", + version = "0.1", + about = "A tool for managing license server certificates" +)] +pub struct Cli { + #[command(subcommand)] + command: Commands, +} + +#[derive(Debug, Subcommand)] +pub enum Commands { + #[command(about = "Generate a new certificate")] + Generate(GenerateKeyOptions), + #[command(about = "Calculate the equal result for power plugin")] + CalcEqual(PowerEqualResultOption), +} + +fn main() { + let args = Cli::parse(); + match args.command { + Commands::Generate(options) => generate_key_file(options), + Commands::CalcEqual(_options) => { + println!("Calculating the equal result for power plugin"); + } + } } diff --git a/cert_tools/src/power_equal.rs b/cert_tools/src/power_equal.rs new file mode 100644 index 0000000..6b1d575 --- /dev/null +++ b/cert_tools/src/power_equal.rs @@ -0,0 +1,9 @@ +use std::path::PathBuf; + +use clap::Args; + +#[derive(Debug, Args)] +pub struct PowerEqualResultOption { + #[arg(help = "Certificate file")] + key_file: PathBuf, +}