feat: add test mode

This commit is contained in:
2021-05-01 08:31:16 +08:00
parent e99f077494
commit f272bee02f

View File

@@ -30,6 +30,12 @@ enum Algo {
Rsa(u32),
}
#[derive(Debug)]
enum Mode {
Prod,
Test,
}
#[async_std::main]
async fn main() -> tide::Result<()> {
println!("{}", include_str!("logo.txt"));
@@ -46,6 +52,7 @@ async fn main() -> tide::Result<()> {
.arg(Arg::with_name("email").long("email").takes_value(true).help("Contract email"))
.arg(Arg::with_name("algo").short("a").long("algo").takes_value(true).default_value("ec384").help("Pki algo"))
.arg(Arg::with_name("timeout").long("timeout").takes_value(true).default_value("5000").help("Timeout (ms)"))
.arg(Arg::with_name("mode").short("m").long("mode").takes_value(true).default_value("prod").help("Mode"))
.get_matches();
if matches.is_present("version") {
@@ -93,6 +100,14 @@ async fn main() -> tide::Result<()> {
exit(1);
}
};
let mode = match matches.value_of("mode") {
Some("prod") => Mode::Prod,
Some("test") => Mode::Test,
_ => {
failure!("Mode is not assigned, or wrong, should be: prod or test");
exit(1);
}
};
let domains_val = matches.values_of("domain").unwrap_or_else(|| {
failure!("Domains is not assigned.");
@@ -108,15 +123,19 @@ async fn main() -> tide::Result<()> {
let primary_name = domains[0];
let alt_names: Vec<&str> = domains.into_iter().skip(1).collect();
information!("Domains, main: {}, alt: {:?}", primary_name, alt_names);
if let Err(e) = request_domains(email, primary_name, &alt_names, algo, timeout) {
if let Err(e) = request_domains(email, primary_name, &alt_names, algo, mode, timeout) {
failure!("Request certificate by acme failed: {}", e);
exit(1);
}
Ok(())
}
fn request_domains(contract_email: &str, primary_name: &str, alt_names: &[&str], algo: Algo, timeout: u64) -> XResult<()> {
let url = DirectoryUrl::LetsEncrypt;
fn request_domains(contract_email: &str, primary_name: &str, alt_names: &[&str], algo: Algo, mode: Mode, timeout: u64) -> XResult<()> {
information!("Acme mode: {:?}", mode);
let url = match mode {
Mode::Prod => DirectoryUrl::LetsEncrypt,
Mode::Test => DirectoryUrl::LetsEncryptStaging,
};
std::fs::create_dir("__temp_dir").ok();
let persist = FilePersist::new("__temp_dir");
let dir = opt_result!(Directory::from_url(persist, url), "Create directory from url failed: {}");