1+ use std:: { env, fs} ;
12use std:: cmp:: Ordering ;
23use std:: collections:: HashMap ;
3- use std:: { env , fs } ;
4+ use std:: path :: PathBuf ;
45
5- use rust_util:: { debugging, opt_result, simple_error, XResult } ;
6+ use rust_util:: { debugging, opt_result, simple_error, warning , XResult } ;
67use rust_util:: util_file:: resolve_file_path;
78use serde:: { Deserialize , Serialize } ;
89
10+ use crate :: consts:: TINY_ENC_FILE_EXT ;
911use crate :: spec:: TinyEncryptEnvelopType ;
1012
1113/// Config file sample:
@@ -34,11 +36,19 @@ use crate::spec::TinyEncryptEnvelopType;
3436#[ derive( Clone , Debug , Serialize , Deserialize ) ]
3537#[ serde( rename_all = "camelCase" ) ]
3638pub struct TinyEncryptConfig {
37- pub environment : Option < HashMap < String , String > > ,
39+ pub environment : Option < HashMap < String , StringOrVecString > > ,
40+ pub namespaces : Option < HashMap < String , String > > ,
3841 pub envelops : Vec < TinyEncryptConfigEnvelop > ,
3942 pub profiles : HashMap < String , Vec < String > > ,
4043}
4144
45+ #[ derive( Clone , Debug , Serialize , Deserialize ) ]
46+ #[ serde( untagged) ]
47+ pub enum StringOrVecString {
48+ String ( String ) ,
49+ Vec ( Vec < String > ) ,
50+ }
51+
4252#[ derive( Clone , Debug , Serialize , Deserialize ) ]
4353#[ serde( rename_all = "camelCase" ) ]
4454pub struct TinyEncryptConfigEnvelop {
@@ -78,6 +88,10 @@ impl TinyEncryptConfig {
7888
7989 if let Some ( environment) = & config. environment {
8090 for ( k, v) in environment {
91+ let v = match v {
92+ StringOrVecString :: String ( s) => { s. to_string ( ) }
93+ StringOrVecString :: Vec ( vs) => { vs. join ( "," ) }
94+ } ;
8195 debugging ! ( "Set env: {}={}" , k, v) ;
8296 env:: set_var ( k, v) ;
8397 }
@@ -86,6 +100,30 @@ impl TinyEncryptConfig {
86100 Ok ( config)
87101 }
88102
103+ pub fn resolve_path_namespace ( & self , path : & PathBuf , append_te : bool ) -> PathBuf {
104+ if let Some ( path_str) = path. to_str ( ) {
105+ if path_str. starts_with ( ':' ) {
106+ let namespace = path_str. chars ( ) . skip ( 1 )
107+ . take_while ( |c| * c != ':' ) . collect :: < String > ( ) ;
108+ let mut filename = path_str. chars ( ) . skip ( 1 )
109+ . skip_while ( |c| * c != ':' ) . skip ( 1 ) . collect :: < String > ( ) ;
110+ if append_te && !filename. ends_with ( TINY_ENC_FILE_EXT ) {
111+ filename. push_str ( TINY_ENC_FILE_EXT ) ;
112+ }
113+
114+ match self . find_namespace ( & namespace) {
115+ None => warning ! ( "Namespace: {} not found" , & namespace) ,
116+ Some ( dir) => return PathBuf :: from ( dir) . join ( & filename) ,
117+ }
118+ }
119+ }
120+ path. clone ( )
121+ }
122+
123+ pub fn find_namespace ( & self , prefix : & str ) -> Option < & String > {
124+ self . namespaces . as_ref ( ) . and_then ( |m| m. get ( prefix) )
125+ }
126+
89127 pub fn find_first_arg_by_kid ( & self , kid : & str ) -> Option < & String > {
90128 self . find_args_by_kid ( kid) . and_then ( |a| a. iter ( ) . next ( ) )
91129 }
@@ -169,3 +207,10 @@ impl TinyEncryptConfig {
169207 Ok ( envelops)
170208 }
171209}
210+
211+ pub fn resolve_path_namespace ( config : & Option < TinyEncryptConfig > , path : & PathBuf , append_te : bool ) -> PathBuf {
212+ match config {
213+ None => path. clone ( ) ,
214+ Some ( config) => config. resolve_path_namespace ( path, append_te) ,
215+ }
216+ }
0 commit comments