feat(tools):增加生成证书文件的命令行功能。

This commit is contained in:
徐涛 2024-04-02 10:46:01 +08:00
parent 1f7ff5e66a
commit bf3add13a3
4 changed files with 75 additions and 3 deletions

View File

@ -7,3 +7,6 @@ edition = "2021"
[dependencies] [dependencies]
cert_lib = { path = "../cert_lib" } cert_lib = { path = "../cert_lib" }
clap = { version = "4.5.4", features = ["derive"] }
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.115"

View File

@ -0,0 +1,28 @@
use clap::Args;
#[derive(Debug, Args)]
pub struct GenerateKeyOptions {
#[arg(short, long, help = "set certificate file version")]
version: Option<i32>,
#[arg(short, long, help = "set where certification file stores")]
output_path: Option<String>,
#[arg(short, long, help = "set certification serial number")]
serial: Option<u32>,
#[arg(short = 'p', long, help = "set certification validity periods")]
validity_periods: Option<u32>,
#[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());
}
}

View File

@ -1,3 +1,35 @@
fn main() { use clap::{Parser, Subcommand};
println!("Hello, world!"); 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");
}
}
} }

View File

@ -0,0 +1,9 @@
use std::path::PathBuf;
use clap::Args;
#[derive(Debug, Args)]
pub struct PowerEqualResultOption {
#[arg(help = "Certificate file")]
key_file: PathBuf,
}