@@ -8,10 +8,13 @@ use args::Args;
8
8
use clap:: Parser ;
9
9
use rust_i18n:: { i18n, t} ;
10
10
use schemars:: schema_for;
11
+ use serde_json:: { Map , Value } ;
11
12
use crate :: echo:: { Echo , Output } ;
12
13
13
14
i18n ! ( "locales" , fallback = "en-us" ) ;
14
15
16
+ const SECURE_VALUE_REDACTED : & str = "<secureValue>" ;
17
+
15
18
fn main ( ) {
16
19
let args = Args :: parse ( ) ;
17
20
match args. input {
@@ -24,9 +27,21 @@ fn main() {
24
27
}
25
28
} ;
26
29
if echo. show_secrets != Some ( true ) {
27
- match & echo. output {
30
+ match echo. output {
28
31
Output :: SecureString ( _) | Output :: SecureObject ( _) => {
29
- echo. output = Output :: String ( "<secureValue>" . to_string ( ) ) ;
32
+ echo. output = Output :: String ( SECURE_VALUE_REDACTED . to_string ( ) ) ;
33
+ } ,
34
+ Output :: Array ( ref mut arr) => {
35
+ for item in arr. iter_mut ( ) {
36
+ if is_secure_value ( item) {
37
+ * item = Value :: String ( SECURE_VALUE_REDACTED . to_string ( ) ) ;
38
+ } else {
39
+ * item = redact ( item) ;
40
+ }
41
+ }
42
+ } ,
43
+ Output :: Object ( ref mut obj) => {
44
+ * obj = redact ( obj) ;
30
45
} ,
31
46
_ => { }
32
47
}
@@ -36,11 +51,39 @@ fn main() {
36
51
return ;
37
52
} ,
38
53
None => {
39
- eprintln ! ( "{}" , t!( "main.noInput" ) ) ;
54
+ let schema = schema_for ! ( Echo ) ;
55
+ let json = serde_json:: to_string_pretty ( & schema) . unwrap ( ) ;
56
+ println ! ( "{json}" ) ;
57
+ }
58
+ }
59
+ }
60
+
61
+ fn is_secure_value ( value : & Value ) -> bool {
62
+ if let Some ( obj) = value. as_object ( ) {
63
+ if obj. len ( ) == 1 && ( obj. contains_key ( "secureString" ) || obj. contains_key ( "secureObject" ) ) {
64
+ return true ;
40
65
}
41
66
}
67
+ false
68
+ }
69
+
70
+ pub fn redact ( value : & Value ) -> Value {
71
+ if is_secure_value ( value) {
72
+ return Value :: String ( SECURE_VALUE_REDACTED . to_string ( ) ) ;
73
+ }
74
+
75
+ if let Some ( map) = value. as_object ( ) {
76
+ let mut new_map = Map :: new ( ) ;
77
+ for ( key, val) in map {
78
+ new_map. insert ( key. clone ( ) , redact ( val) ) ;
79
+ }
80
+ return Value :: Object ( new_map) ;
81
+ }
82
+
83
+ if let Some ( array) = value. as_array ( ) {
84
+ let new_array: Vec < Value > = array. iter ( ) . map ( redact) . collect ( ) ;
85
+ return Value :: Array ( new_array) ;
86
+ }
42
87
43
- let schema = schema_for ! ( Echo ) ;
44
- let json = serde_json:: to_string_pretty ( & schema) . unwrap ( ) ;
45
- println ! ( "{json}" ) ;
88
+ value. clone ( )
46
89
}
0 commit comments