1
1
use clap:: { Parser , ValueEnum } ;
2
+ use relative_path:: RelativePathBuf ;
2
3
use serde:: Deserialize ;
3
4
use serde_json:: json;
4
5
use serde_yaml;
5
6
use std:: {
7
+ env:: current_dir,
6
8
fs,
7
9
io,
8
10
path:: PathBuf ,
@@ -13,7 +15,7 @@ use std::{
13
15
#[ command( ) ]
14
16
struct Args {
15
17
/// Path to the kustomization file or directory
16
- path : PathBuf ,
18
+ path : RelativePathBuf ,
17
19
18
20
/// Output format
19
21
#[ arg( short, long, value_enum, default_value = "text" ) ]
@@ -37,35 +39,33 @@ struct Kustomization {
37
39
}
38
40
39
41
40
- fn canonical_path ( path : PathBuf ) -> io :: Result < PathBuf > {
41
- let mut canonical = path. canonicalize ( ) ? ;
42
+ fn normalize ( root : & PathBuf , mut path : RelativePathBuf ) -> RelativePathBuf {
43
+ path. normalize ( ) ;
42
44
43
- if canonical . is_file ( ) {
44
- return Ok ( canonical ) ;
45
+ if path . to_logical_path ( & root ) . is_dir ( ) {
46
+ path . push ( "kustomization.yml" ) ;
45
47
}
46
48
47
- if canonical. is_dir ( ) {
48
- canonical. push ( "kustomization.yml" ) ;
49
-
50
- if canonical. is_file ( ) {
51
- return Ok ( canonical) ;
52
- }
49
+ if path. to_logical_path ( & root) . is_file ( ) {
50
+ return path;
51
+ } else {
52
+ path. set_extension ( "yaml" ) ;
53
+ }
54
+
55
+ if path. to_logical_path ( & root) . is_file ( ) {
56
+ return path;
53
57
}
54
58
55
- let error = io:: Error :: new (
56
- io:: ErrorKind :: Other ,
57
- format ! ( "Invalid path {}" , canonical. display( ) )
58
- ) ;
59
- return Err ( error) ;
59
+ panic ! ( "Unable to normalize path {path}" ) ;
60
60
}
61
61
62
62
63
- fn read_file ( path : PathBuf ) -> io:: Result < String > {
63
+ fn read_file ( path : & PathBuf ) -> io:: Result < String > {
64
64
return fs:: read_to_string ( path) ;
65
65
}
66
66
67
67
68
- fn deserialize ( path : PathBuf ) -> Vec < Kustomization > {
68
+ fn deserialize ( path : & PathBuf ) -> Vec < Kustomization > {
69
69
let content = read_file ( path) . unwrap ( ) ;
70
70
71
71
return serde_yaml:: Deserializer :: from_str ( & content)
@@ -74,34 +74,37 @@ fn deserialize(path: PathBuf) -> Vec<Kustomization> {
74
74
}
75
75
76
76
77
- fn run ( path : PathBuf , result : & mut Vec < String > ) {
78
- if let Ok ( canonical) = canonical_path ( path. clone ( ) ) {
79
- result. push ( format ! ( "{}" , canonical. display( ) ) ) ;
77
+ fn run ( root : & PathBuf , path : RelativePathBuf , result : & mut Vec < String > ) {
78
+ let current_path = normalize ( & root, path. clone ( ) ) ;
80
79
81
- let resources: Vec < String > = deserialize ( canonical. clone ( ) )
82
- . iter ( )
83
- . map ( |doc| doc. resources . clone ( ) )
84
- . flatten ( )
85
- . collect ( ) ;
80
+ result. push ( format ! ( "{}" , current_path) ) ;
86
81
87
- for r in resources {
88
- let mut next_path = canonical
89
- . parent ( )
90
- . unwrap ( )
91
- . to_path_buf ( ) ;
92
- next_path. push ( PathBuf :: from ( r) ) ;
82
+ let resources: Vec < String > = deserialize ( & current_path. to_logical_path ( root) )
83
+ . iter ( )
84
+ . map ( |doc| doc. resources . clone ( ) )
85
+ . flatten ( )
86
+ . collect ( ) ;
87
+
88
+ for r in resources {
89
+ let next_path = current_path
90
+ . parent ( )
91
+ . unwrap ( )
92
+ . join_normalized ( r) ;
93
93
94
- run ( next_path, result) ;
95
- } ;
94
+ run ( root, next_path, result) ;
96
95
} ;
97
96
}
98
97
99
98
100
99
fn main ( ) {
101
100
let args = Args :: parse ( ) ;
101
+ let root = current_dir ( ) . unwrap ( ) ;
102
102
let mut result = Vec :: new ( ) ;
103
103
104
- run ( args. path , & mut result) ;
104
+ run ( & root, args. path , & mut result) ;
105
+
106
+ result. sort ( ) ;
107
+ result. dedup ( ) ;
105
108
106
109
match args. format {
107
110
Some ( Format :: Json ) => {
0 commit comments