diff --git a/Cargo.lock b/Cargo.lock index 3b6622c..230523a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1989,7 +1989,7 @@ dependencies = [ [[package]] name = "tiny-encrypt" -version = "1.9.6" +version = "1.9.7" dependencies = [ "aes-gcm-stream", "base64 0.22.1", diff --git a/Cargo.toml b/Cargo.toml index c2f4ef8..d6ad2d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tiny-encrypt" -version = "1.9.6" +version = "1.9.7" edition = "2021" license = "MIT" description = "A simple and tiny file encrypt tool" diff --git a/src/config.rs b/src/config.rs index 9e6d895..df57043 100644 --- a/src/config.rs +++ b/src/config.rs @@ -7,7 +7,7 @@ use rust_util::util_file::resolve_file_path; use rust_util::{debugging, opt_result, simple_error, warning, XResult}; use serde::{Deserialize, Serialize}; -use crate::consts::{TINY_ENC_CONFIG_FILE, TINY_ENC_CONFIG_FILE_2, TINY_ENC_CONFIG_FILE_3, TINY_ENC_FILE_EXT}; +use crate::consts::{ENV_TINY_ENC_CONFIG_FILE, TINY_ENC_CONFIG_FILE, TINY_ENC_CONFIG_FILE_2, TINY_ENC_CONFIG_FILE_3, TINY_ENC_FILE_EXT}; use crate::spec::TinyEncryptEnvelopType; /// Config file sample: @@ -75,12 +75,17 @@ pub struct TinyEncryptConfigEnvelop { impl TinyEncryptConfig { pub fn load_default() -> XResult { - let resolved_file = resolve_file_path(TINY_ENC_CONFIG_FILE); + let resolved_file0 = env::var(ENV_TINY_ENC_CONFIG_FILE).ok(); + let resolved_file_1 = resolve_file_path(TINY_ENC_CONFIG_FILE); let resolved_file_2 = resolve_file_path(TINY_ENC_CONFIG_FILE_2); let resolved_file_3 = resolve_file_path(TINY_ENC_CONFIG_FILE_3); - let config_file = if fs::metadata(&resolved_file).is_ok() { - debugging!("Load config from: {resolved_file}"); - resolved_file + if let Some(resolved_file) = resolved_file0 { + debugging!("Found tiny encrypt config file: {}", &resolved_file); + return Self::load(&resolved_file) + } + let config_file = if fs::metadata(&resolved_file_1).is_ok() { + debugging!("Load config from: {resolved_file_1}"); + resolved_file_1 } else if fs::metadata(&resolved_file_2).is_ok() { debugging!("Load config from: {resolved_file_2}"); resolved_file_2 @@ -88,8 +93,8 @@ impl TinyEncryptConfig { debugging!("Load config from: {resolved_file_3}"); resolved_file_3 } else { - warning!("Cannot find config file from:\n- {resolved_file}\n- {resolved_file_2}\n- {resolved_file_3}"); - resolved_file + warning!("Cannot find config file from:\n- {resolved_file_1}\n- {resolved_file_2}\n- {resolved_file_3}"); + resolved_file_1 }; Self::load(&config_file) } @@ -294,7 +299,7 @@ pub fn resolve_path_namespace( pub fn load_includes_and_merge(mut config: TinyEncryptConfig) -> TinyEncryptConfig { if let Some(includes) = &config.includes { - let sub_configs = search_include_configs(&includes); + let sub_configs = search_include_configs(includes); debugging!( "Found {} sub configs, detail {:?}", sub_configs.len(), diff --git a/src/consts.rs b/src/consts.rs index 8c16e5c..524014d 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -11,6 +11,7 @@ pub const ENC_CHACHA20_POLY1305_KYBER1204: &str = "chacha20-poly1305-kyber1204"; // Extend and config file pub const TINY_ENC_FILE_EXT: &str = ".tinyenc"; pub const TINY_ENC_PEM_FILE_EXT: &str = ".tinyenc.pem"; +pub const ENV_TINY_ENC_CONFIG_FILE: &str = "TINY_ENCRYPT_CONFIG_FILE"; pub const TINY_ENC_CONFIG_FILE: &str = "~/.tinyencrypt/config-rs.json"; pub const TINY_ENC_CONFIG_FILE_2: &str = "~/.config/tinyencrypt-rs.json"; pub const TINY_ENC_CONFIG_FILE_3: &str = "/etc/tinyencrypt/config-rs.json";