@@ -8,10 +8,13 @@ use args::Args;
88use clap:: Parser ;
99use rust_i18n:: { i18n, t} ;
1010use schemars:: schema_for;
11+ use serde_json:: { Map , Value } ;
1112use crate :: echo:: { Echo , Output } ;
1213
1314i18n ! ( "locales" , fallback = "en-us" ) ;
1415
16+ const SECURE_VALUE_REDACTED : & str = "<secureValue>" ;
17+
1518fn main ( ) {
1619 let args = Args :: parse ( ) ;
1720 match args. input {
@@ -24,9 +27,21 @@ fn main() {
2427 }
2528 } ;
2629 if echo. show_secrets != Some ( true ) {
27- match & echo. output {
30+ match echo. output {
2831 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) ;
3045 } ,
3146 _ => { }
3247 }
@@ -36,11 +51,39 @@ fn main() {
3651 return ;
3752 } ,
3853 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 ;
4065 }
4166 }
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+ }
4287
43- let schema = schema_for ! ( Echo ) ;
44- let json = serde_json:: to_string_pretty ( & schema) . unwrap ( ) ;
45- println ! ( "{json}" ) ;
88+ value. clone ( )
4689}
0 commit comments