|
| 1 | +// Copyright 2025 MongoDB Inc |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package sunset |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/json" |
| 19 | + "fmt" |
| 20 | + "strings" |
| 21 | + |
| 22 | + "github.com/mongodb/openapi/tools/cli/internal/cli/flag" |
| 23 | + "github.com/mongodb/openapi/tools/cli/internal/cli/usage" |
| 24 | + "github.com/mongodb/openapi/tools/cli/internal/openapi" |
| 25 | + "github.com/spf13/afero" |
| 26 | + "github.com/spf13/cobra" |
| 27 | + "gopkg.in/yaml.v3" |
| 28 | +) |
| 29 | + |
| 30 | +type ListOpts struct { |
| 31 | + fs afero.Fs |
| 32 | + basePath string |
| 33 | + outputPath string |
| 34 | + format string |
| 35 | +} |
| 36 | + |
| 37 | +func (o *ListOpts) Run() error { |
| 38 | + loader := openapi.NewOpenAPI3() |
| 39 | + specInfo, err := loader.CreateOpenAPISpecFromPath(o.basePath) |
| 40 | + if err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + |
| 44 | + bytes, err := o.newSunsetListBytes(openapi.NewSunsetListFromSpec(specInfo)) |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + if o.outputPath != "" { |
| 49 | + return afero.WriteFile(o.fs, o.outputPath, bytes, 0o600) |
| 50 | + } |
| 51 | + |
| 52 | + fmt.Println(string(bytes)) |
| 53 | + return nil |
| 54 | +} |
| 55 | + |
| 56 | +func (o *ListOpts) newSunsetListBytes(versions []*openapi.Sunset) ([]byte, error) { |
| 57 | + data, err := json.MarshalIndent(versions, "", " ") |
| 58 | + if err != nil { |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + |
| 62 | + if format := strings.ToLower(o.format); format == "json" { |
| 63 | + return data, nil |
| 64 | + } |
| 65 | + |
| 66 | + var jsonData interface{} |
| 67 | + if mErr := json.Unmarshal(data, &jsonData); mErr != nil { |
| 68 | + return nil, mErr |
| 69 | + } |
| 70 | + |
| 71 | + yamlData, err := yaml.Marshal(jsonData) |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + |
| 76 | + return yamlData, nil |
| 77 | +} |
| 78 | + |
| 79 | +// ListBuilder builds the merge command with the following signature: |
| 80 | +// changelog create -b path_folder -r path_folder --dry-run |
| 81 | +func ListBuilder() *cobra.Command { |
| 82 | + opts := &ListOpts{ |
| 83 | + fs: afero.NewOsFs(), |
| 84 | + } |
| 85 | + |
| 86 | + cmd := &cobra.Command{ |
| 87 | + Use: "list -s spec.json -o json", |
| 88 | + Short: "List API endpoints with a Sunset date for a given OpenAPI spec.", |
| 89 | + Aliases: []string{"ls"}, |
| 90 | + Args: cobra.NoArgs, |
| 91 | + RunE: func(_ *cobra.Command, _ []string) error { |
| 92 | + return opts.Run() |
| 93 | + }, |
| 94 | + } |
| 95 | + |
| 96 | + cmd.Flags().StringVarP(&opts.basePath, flag.Spec, flag.SpecShort, "", usage.Spec) |
| 97 | + cmd.Flags().StringVarP(&opts.outputPath, flag.Output, flag.OutputShort, "", usage.Output) |
| 98 | + cmd.Flags().StringVarP(&opts.format, flag.Format, flag.FormatShort, "json", usage.Format) |
| 99 | + |
| 100 | + _ = cmd.MarkFlagRequired(flag.Spec) |
| 101 | + |
| 102 | + return cmd |
| 103 | +} |
0 commit comments