From 1179840114b429ac22b6c429ff3758f6a86b509a Mon Sep 17 00:00:00 2001 From: Dmitry Date: Fri, 25 Oct 2024 10:43:07 +0300 Subject: [PATCH] arg --openssl to --with-openssl --- src/common.rs | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/src/common.rs b/src/common.rs index 51ee04e..11a4bbd 100644 --- a/src/common.rs +++ b/src/common.rs @@ -6,7 +6,6 @@ use std::{ collections::BTreeMap, fmt::Display, path::{Path, PathBuf}, - str::FromStr, }; use tokio::{ fs::{self, File}, @@ -18,7 +17,7 @@ use futures_core::stream::Stream; pub(crate) type VarsMap = BTreeMap; pub(crate) const UTF8_STR: &str = "utf8"; -pub(crate) const DEFAULT_ENCODING: &str = "cp866"; // .bat +pub(crate) const DEFAULT_ENCODING: &str = UTF8_STR; #[derive(Debug, Clone, PartialEq)] pub enum OpenSSLProviderArg { @@ -26,12 +25,11 @@ pub enum OpenSSLProviderArg { ExternalBin(String), } -impl FromStr for OpenSSLProviderArg { - type Err = anyhow::Error; - fn from_str(s: &str) -> Result { - match s.to_ascii_lowercase().as_str() { - "internal" => Ok(OpenSSLProviderArg::Internal), - x => Ok(OpenSSLProviderArg::ExternalBin(x.to_string())), +impl From> for OpenSSLProviderArg { + fn from(value: Option<&String>) -> Self { + match value { + Some(x) => OpenSSLProviderArg::ExternalBin(x.clone()), + _ => OpenSSLProviderArg::Internal, } } } @@ -75,9 +73,9 @@ pub(crate) struct Args { #[arg(long, default_value = "3650")] pub(crate) days: u32, - /// openssl binary or (internal) - #[arg(long, short, default_value = "internal")] - pub(crate) openssl: OpenSSLProviderArg, + /// use openssl binary + #[arg(long = "with-openssl", short)] + pub(crate) openssl: Option, /// template file #[arg(long, default_value = "template.ovpn")] @@ -140,7 +138,7 @@ impl From<&Args> for AppConfig { defaults.encoding.clone() }; let name = args.name.clone(); - let openssl = args.openssl.clone(); + let openssl: OpenSSLProviderArg = args.openssl.as_ref().into(); let template_file = args.template_file.clone(); let req_days = args.days; let keys_subdir = args.keys_dir.clone(); @@ -162,16 +160,10 @@ impl From<&Args> for AppConfig { } pub(crate) async fn is_file_exist(filepath: &PathBuf) -> bool { - let metadata = tokio::fs::metadata(&filepath).await; - if metadata.is_err() { - return false; + match tokio::fs::metadata(&filepath).await { + Ok(x) => x.is_file(), + _ => false, } - - if !metadata.unwrap().is_file() { - return false; - } - - true } pub(crate) async fn read_file

(filepath: P, encoding: &str) -> Result