@@ -36,14 +36,23 @@ const (
3636 outputOption = "output"
3737 configOption = "config"
3838 forceOption = "force"
39+ crdsOption = "crds"
3940
40- readOnly = os .O_RDONLY
41+ crdsDefaultValue = "all"
42+ readOnly = os .O_RDONLY
4143)
4244
4345func initConfig () {
4446 viper .AutomaticEnv ()
4547}
4648
49+ type RunnerConfig struct {
50+ Input string
51+ Output string
52+ Overwrite bool
53+ Kinds map [string ]struct {}
54+ }
55+
4756func RunCmd (ctx context.Context ) * cobra.Command {
4857 cmd := & cobra.Command {
4958 Use : "openapi2crd SPEC_FILE" ,
@@ -57,41 +66,59 @@ func RunCmd(ctx context.Context) *cobra.Command {
5766 configPath := viper .GetString (configOption )
5867 outputPath := viper .GetString (outputOption )
5968 forceOverwrite := viper .GetBool (forceOption )
69+ crds := viper .GetString (crdsOption )
70+
71+ c := & RunnerConfig {
72+ Input : configPath ,
73+ Output : outputPath ,
74+ Overwrite : forceOverwrite ,
75+ Kinds : make (map [string ]struct {}),
76+ }
77+
78+ if crds != crdsDefaultValue {
79+ kinds := strings .Split (crds , "," )
80+ c .Kinds = make (map [string ]struct {}, len (kinds ))
81+ for _ , kind := range kinds {
82+ c .Kinds [strings .TrimSpace (kind )] = struct {}{}
83+ }
84+ }
6085
6186 fs := afero .NewOsFs ()
6287
63- return runOpenapi2crd (ctx , fs , configPath , outputPath , forceOverwrite )
88+ return runOpenapi2crd (ctx , fs , c )
6489 },
6590 }
6691
6792 cmd .Flags ().StringP (outputOption , "o" , "" , "Path to output file (required)" )
6893 _ = cmd .MarkFlagRequired (outputOption )
6994 cmd .Flags ().StringP (configOption , "c" , "" , "Path to the config file (required)" )
95+ _ = cmd .MarkFlagRequired (configOption )
7096 cmd .Flags ().BoolP (forceOption , "f" , false , "Force overwrite the output file if it exists" )
97+ cmd .Flags ().String (crdsOption , crdsDefaultValue , "One or more Kind names to generate, separated by comma. Use 'all' to generate all CRDs." )
7198 cobra .OnInitialize (initConfig )
7299
73100 viper .SetEnvKeyReplacer (strings .NewReplacer ("-" , "_" ))
74101
75102 return cmd
76103}
77104
78- func runOpenapi2crd (ctx context.Context , fs afero.Fs , input , output string , overwrite bool ) error {
79- file , err := fs .OpenFile (input , readOnly , 0o644 )
105+ func runOpenapi2crd (ctx context.Context , fs afero.Fs , runnerConfig * RunnerConfig ) error {
106+ file , err := fs .OpenFile (runnerConfig . Input , readOnly , 0o644 )
80107 if err != nil {
81- return fmt .Errorf ("error opening the file %s: %w" , input , err )
108+ return fmt .Errorf ("error opening the file %s: %w" , runnerConfig . Input , err )
82109 }
83110
84111 configData , err := afero .ReadAll (file )
85112 if err != nil {
86- return fmt .Errorf ("error reading the file %s: %w" , input , err )
113+ return fmt .Errorf ("error reading the file %s: %w" , runnerConfig . Input , err )
87114 }
88115
89116 cfg , err := config .Parse (configData )
90117 if err != nil {
91118 return fmt .Errorf ("error parsing config: %w" , err )
92119 }
93120
94- fsExporter , err := exporter .New (fs , output , overwrite )
121+ fsExporter , err := exporter .New (fs , runnerConfig . Output , runnerConfig . Overwrite )
95122 if err != nil {
96123 return fmt .Errorf ("error creating the exporter: %w" , err )
97124 }
@@ -116,6 +143,11 @@ func runOpenapi2crd(ctx context.Context, fs afero.Fs, input, output string, over
116143 atlasLoader := config .NewAtlas (openapiLoader )
117144
118145 for _ , crdConfig := range cfg .Spec .CRDConfig {
146+ _ , shouldGen := runnerConfig .Kinds [crdConfig .GVK .Kind ]
147+ if len (runnerConfig .Kinds ) > 0 && ! shouldGen {
148+ continue
149+ }
150+
119151 pluginSet , err := plugins .GetPluginSet (pluginSets , crdConfig .PluginSet )
120152 if err != nil {
121153 return fmt .Errorf ("error getting plugin set %q: %w" , crdConfig .PluginSet , err )
0 commit comments