1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15- use clap:: Parser ;
15+ use clap:: { Args , Parser } ;
1616use golem_openapi_client_generator:: gen;
1717use openapiv3:: OpenAPI ;
1818use std:: fs:: File ;
@@ -21,7 +21,15 @@ use std::path::PathBuf;
2121
2222#[ derive( Parser , Debug ) ]
2323#[ command( author, version, about, long_about = None , rename_all = "kebab-case" ) ]
24- struct Command {
24+ enum Cli {
25+ /// Generate a client from an OpenAPI spec
26+ Generate ( GenerateArgs ) ,
27+ /// Merge multiple OpenAPI specs into a single one
28+ Merge ( MergeArgs ) ,
29+ }
30+
31+ #[ derive( Debug , Args ) ]
32+ struct GenerateArgs {
2533 #[ arg( short, long, value_name = "spec" , value_hint = clap:: ValueHint :: FilePath , num_args = 1 .., required = true ) ]
2634 spec_yaml : Vec < PathBuf > ,
2735
@@ -35,26 +43,46 @@ struct Command {
3543 name : String ,
3644}
3745
46+ #[ derive( Debug , Args ) ]
47+ struct MergeArgs {
48+ #[ arg( short, long, value_name = "specs" , value_hint = clap:: ValueHint :: FilePath , num_args = 1 .., required = true ) ]
49+ spec_yaml : Vec < PathBuf > ,
50+ #[ arg( short, long, value_name = "output" , value_hint = clap:: ValueHint :: FilePath ) ]
51+ output_file : PathBuf ,
52+ }
53+
3854fn main ( ) {
39- let command = Command :: parse ( ) ;
55+ let command = Cli :: parse ( ) ;
56+
57+ match command {
58+ Cli :: Generate ( args) => {
59+ let openapi_specs = parse_openapi_specs ( & args. spec_yaml ) ;
60+ gen (
61+ openapi_specs,
62+ & args. output_directory ,
63+ & args. name ,
64+ & args. client_version ,
65+ )
66+ . unwrap ( ) ;
67+ }
68+ Cli :: Merge ( args) => {
69+ let openapi_specs = parse_openapi_specs ( & args. spec_yaml ) ;
70+ let openapi =
71+ golem_openapi_client_generator:: merge_all_openapi_specs ( openapi_specs) . unwrap ( ) ;
72+ let file = File :: create ( & args. output_file ) . unwrap ( ) ;
73+ serde_yaml:: to_writer ( file, & openapi) . unwrap ( ) ;
74+ }
75+ }
76+ }
4077
41- let openapi_specs = command
42- . spec_yaml
43- . into_iter ( )
78+ fn parse_openapi_specs ( spec : & Vec < PathBuf > ) -> Vec < OpenAPI > {
79+ spec. into_iter ( )
4480 . map ( |spec| {
4581 let file = File :: open ( & spec) . unwrap ( ) ;
4682 let reader = BufReader :: new ( file) ;
4783 let openapi: OpenAPI = serde_yaml:: from_reader ( reader)
4884 . expect ( format ! ( "Could not deserialize input: {:?}" , spec) . as_str ( ) ) ;
4985 openapi
5086 } )
51- . collect :: < Vec < _ > > ( ) ;
52-
53- gen (
54- openapi_specs,
55- & command. output_directory ,
56- & command. name ,
57- & command. client_version ,
58- )
59- . unwrap ( ) ;
87+ . collect :: < Vec < _ > > ( )
6088}
0 commit comments