feat: v1.5.2, fix edit file issue, add prefixes

This commit is contained in:
2023-12-15 21:15:22 +08:00
parent a7398a2578
commit 343a6f5fac
8 changed files with 57 additions and 11 deletions

View File

@@ -1,11 +1,13 @@
use std::{env, fs};
use std::cmp::Ordering;
use std::collections::HashMap;
use std::{env, fs};
use std::path::PathBuf;
use rust_util::{debugging, opt_result, simple_error, XResult};
use rust_util::{debugging, opt_result, simple_error, warning, XResult};
use rust_util::util_file::resolve_file_path;
use serde::{Deserialize, Serialize};
use crate::consts::TINY_ENC_FILE_EXT;
use crate::spec::TinyEncryptEnvelopType;
/// Config file sample:
@@ -35,6 +37,7 @@ use crate::spec::TinyEncryptEnvelopType;
#[serde(rename_all = "camelCase")]
pub struct TinyEncryptConfig {
pub environment: Option<HashMap<String, String>>,
pub namespaces: Option<HashMap<String, String>>,
pub envelops: Vec<TinyEncryptConfigEnvelop>,
pub profiles: HashMap<String, Vec<String>>,
}
@@ -86,6 +89,30 @@ impl TinyEncryptConfig {
Ok(config)
}
pub fn resolve_path_namespace(&self, path: &PathBuf, append_te: bool) -> PathBuf {
if let Some(path_str) = path.to_str() {
if path_str.starts_with(':') {
let namespace = path_str.chars().skip(1)
.take_while(|c| *c != ':').collect::<String>();
let mut filename = path_str.chars().skip(1)
.skip_while(|c| *c != ':').skip(1).collect::<String>();
if append_te && !filename.ends_with(TINY_ENC_FILE_EXT) {
filename.push_str(TINY_ENC_FILE_EXT);
}
match self.find_namespace(&namespace) {
None => warning!("Namespace: {} not found", &namespace),
Some(dir) => return PathBuf::from(dir).join(&filename),
}
}
}
path.clone()
}
pub fn find_namespace(&self, prefix: &str) -> Option<&String> {
self.namespaces.as_ref().and_then(|m| m.get(prefix))
}
pub fn find_first_arg_by_kid(&self, kid: &str) -> Option<&String> {
self.find_args_by_kid(kid).and_then(|a| a.iter().next())
}
@@ -169,3 +196,10 @@ impl TinyEncryptConfig {
Ok(envelops)
}
}
pub fn resolve_path_namespace(config: &Option<TinyEncryptConfig>, path: &PathBuf, append_te: bool) -> PathBuf {
match config {
None => path.clone(),
Some(config) => config.resolve_path_namespace(path, append_te),
}
}