1
- use clap:: Parser ;
1
+ use clap:: { Parser , ValueEnum } ;
2
2
use serde:: Deserialize ;
3
+ use serde_json:: json;
3
4
use serde_yaml;
4
5
use std:: {
5
6
fs,
@@ -11,7 +12,21 @@ use std::{
11
12
#[ derive( Parser ) ]
12
13
#[ command( ) ]
13
14
struct Args {
15
+ /// Path to the kustomization file or directory
14
16
path : PathBuf ,
17
+
18
+ /// Output format
19
+ #[ arg( short, long, value_enum, default_value = "text" ) ]
20
+ format : Option < Format > ,
21
+ }
22
+
23
+ #[ derive( Copy , Clone , Debug , PartialEq , Eq , PartialOrd , Ord , ValueEnum ) ]
24
+ enum Format {
25
+ /// One path per line
26
+ Text ,
27
+
28
+ /// JSON
29
+ Json ,
15
30
}
16
31
17
32
@@ -59,9 +74,9 @@ fn deserialize(path: PathBuf) -> Vec<Kustomization> {
59
74
}
60
75
61
76
62
- fn run ( path : PathBuf , result : Vec < PathBuf > ) {
77
+ fn run ( path : PathBuf , result : & mut Vec < String > ) {
63
78
if let Ok ( canonical) = canonical_path ( path. clone ( ) ) {
64
- println ! ( "{}" , canonical. display( ) ) ;
79
+ result . push ( format ! ( "{}" , canonical. display( ) ) ) ;
65
80
66
81
let resources: Vec < String > = deserialize ( canonical. clone ( ) )
67
82
. iter ( )
@@ -76,17 +91,27 @@ fn run(path: PathBuf, result: Vec<PathBuf>) {
76
91
. to_path_buf ( ) ;
77
92
next_path. push ( PathBuf :: from ( r) ) ;
78
93
79
- let mut branch = result. clone ( ) ;
80
-
81
- branch. push ( canonical. clone ( ) ) ;
82
- run ( next_path, branch) ;
94
+ run ( next_path, result) ;
83
95
} ;
84
96
} ;
85
97
}
86
98
87
99
88
100
fn main ( ) {
89
101
let args = Args :: parse ( ) ;
90
-
91
- run ( args. path , Vec :: new ( ) ) ;
102
+ let mut result = Vec :: new ( ) ;
103
+
104
+ run ( args. path , & mut result) ;
105
+
106
+ match args. format {
107
+ Some ( Format :: Json ) => {
108
+ let json = json ! ( result) ;
109
+ println ! ( "{}" , json. to_string( ) ) ;
110
+ } ,
111
+ _ => {
112
+ for r in result. iter ( ) {
113
+ println ! ( "{r}" ) ;
114
+ }
115
+ }
116
+ }
92
117
}
0 commit comments