@@ -20,6 +20,7 @@ import (
2020 "fmt"
2121 "strings"
2222
23+ "github.com/mongodb/openapi/tools/cli/internal/apiversion"
2324 "github.com/mongodb/openapi/tools/cli/internal/cli/flag"
2425 "github.com/mongodb/openapi/tools/cli/internal/cli/usage"
2526 "github.com/mongodb/openapi/tools/cli/internal/openapi"
@@ -29,11 +30,12 @@ import (
2930)
3031
3132type Opts struct {
32- fs afero.Fs
33- basePath string
34- outputPath string
35- format string
36- env string
33+ fs afero.Fs
34+ basePath string
35+ outputPath string
36+ format string
37+ env string
38+ stabilityLevel string
3739}
3840
3941func (o * Opts ) Run () error {
@@ -44,12 +46,7 @@ func (o *Opts) Run() error {
4446 }
4547
4648 var versions []string
47- if o .env == "" {
48- versions , err = openapi .ExtractVersions (specInfo .Spec )
49- } else {
50- versions , err = openapi .ExtractVersionsWithEnv (specInfo .Spec , o .env )
51- }
52-
49+ versions , err = openapi .ExtractVersionsWithEnv (specInfo .Spec , o .env )
5350 if err != nil {
5451 return err
5552 }
@@ -58,7 +55,8 @@ func (o *Opts) Run() error {
5855 return errors .New ("no versions found in the OpenAPI specification" )
5956 }
6057
61- bytes , err := o .getVersionBytes (versions )
58+ versions = o .filterStabilityLevelVersions (versions )
59+ bytes , err := o .versionsAsBytes (versions )
6260 if err != nil {
6361 return err
6462 }
@@ -71,7 +69,26 @@ func (o *Opts) Run() error {
7169 return nil
7270}
7371
74- func (o * Opts ) getVersionBytes (versions []string ) ([]byte , error ) {
72+ func (o * Opts ) filterStabilityLevelVersions (apiVersions []string ) []string {
73+ if o .stabilityLevel == "" || apiVersions == nil {
74+ return apiVersions
75+ }
76+
77+ var out []string
78+ for _ , v := range apiVersions {
79+ if o .stabilityLevel == apiversion .PreviewStabilityLevel && strings .Contains (v , "preview" ) {
80+ out = append (out , v )
81+ }
82+
83+ if o .stabilityLevel == apiversion .StableStabilityLevel && ! strings .Contains (v , "preview" ) {
84+ out = append (out , v )
85+ }
86+ }
87+
88+ return out
89+ }
90+
91+ func (o * Opts ) versionsAsBytes (versions []string ) ([]byte , error ) {
7592 data , err := json .MarshalIndent (versions , "" , " " )
7693 if err != nil {
7794 return nil , err
@@ -95,32 +112,38 @@ func (o *Opts) getVersionBytes(versions []string) ([]byte, error) {
95112}
96113
97114func (o * Opts ) PreRunE (_ []string ) error {
115+ o .stabilityLevel = strings .ToUpper (o .stabilityLevel )
116+ if o .stabilityLevel != "" && o .stabilityLevel != apiversion .PreviewStabilityLevel && o .stabilityLevel != apiversion .StableStabilityLevel {
117+ return fmt .Errorf ("stability level must be %q or %q, got %q" , apiversion .PreviewStabilityLevel , apiversion .StableStabilityLevel , o .stabilityLevel )
118+ }
119+
98120 if o .basePath == "" {
99- return fmt .Errorf ("no OAS detected. Please, use the flag %s to include the base OAS" , flag .Base )
121+ return fmt .Errorf ("no OAS detected. Please, use the flag %q to include the base OAS" , flag .Base )
100122 }
101123
102124 if o .outputPath != "" && ! strings .Contains (o .outputPath , ".json" ) && ! strings .Contains (o .outputPath , ".yaml" ) {
103- return fmt .Errorf ("output file must be either a JSON or YAML file, got %s " , o .outputPath )
125+ return fmt .Errorf ("output file must be either a JSON or YAML file, got %q " , o .outputPath )
104126 }
105127
106128 if o .format != "json" && o .format != "yaml" {
107- return fmt .Errorf ("output format must be either 'json' or 'yaml', got %s " , o .format )
129+ return fmt .Errorf ("output format must be either 'json' or 'yaml', got %q " , o .format )
108130 }
109131
110132 return nil
111133}
112134
113135// Builder builds the versions command with the following signature:
114- // versions -s oas.
136+ // versions -s oas --env dev|qa|staging|prod -stability-level STABLE|PREVIEW .
115137func Builder () * cobra.Command {
116138 opts := & Opts {
117139 fs : afero .NewOsFs (),
118140 }
119141
120142 cmd := & cobra.Command {
121- Use : "versions -s spec " ,
122- Short : "Get a list of versions from an OpenAPI specification." ,
123- Args : cobra .NoArgs ,
143+ Use : "versions -s spec " ,
144+ Aliases : []string {"versions list" , "versions ls" },
145+ Short : "Get a list of versions from an OpenAPI specification." ,
146+ Args : cobra .NoArgs ,
124147 PreRunE : func (_ * cobra.Command , args []string ) error {
125148 return opts .PreRunE (args )
126149 },
@@ -131,6 +154,7 @@ func Builder() *cobra.Command {
131154
132155 cmd .Flags ().StringVarP (& opts .basePath , flag .Spec , flag .SpecShort , "" , usage .Spec )
133156 cmd .Flags ().StringVar (& opts .env , flag .Environment , "" , usage .Environment )
157+ cmd .Flags ().StringVarP (& opts .stabilityLevel , flag .StabilityLevel , flag .StabilityLevelShort , "" , usage .StabilityLevel )
134158 cmd .Flags ().StringVarP (& opts .outputPath , flag .Output , flag .OutputShort , "" , usage .Output )
135159 cmd .Flags ().StringVarP (& opts .format , flag .Format , flag .FormatShort , "json" , usage .Format )
136160 return cmd
0 commit comments