Skip to content

Commit 8315494

Browse files
Addressed PR comments - Part 1
1 parent feac427 commit 8315494

File tree

10 files changed

+79
-22
lines changed

10 files changed

+79
-22
lines changed

tools/cli/internal/cli/breakingchanges/breakingchanges.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ func Builder() *cobra.Command {
2323
cmd := &cobra.Command{
2424
Use: "breaking-changes",
2525
Short: "Manage API Breaking changes related commands.",
26-
Annotations: map[string]string{
27-
"toc": "true",
28-
},
2926
}
3027

3128
cmd.AddCommand(

tools/cli/internal/cli/breakingchanges/exemptions/exemptions.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ func Builder() *cobra.Command {
2222
cmd := &cobra.Command{
2323
Use: "exemptions",
2424
Short: "Manage exemptions.",
25-
Annotations: map[string]string{
26-
"toc": "true",
27-
},
2825
}
2926

3027
cmd.AddCommand(ParseBuilder())

tools/cli/internal/cli/changelog/changelog.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ func Builder() *cobra.Command {
2424
cmd := &cobra.Command{
2525
Use: "changelog",
2626
Short: "Manage the API Changelog for the OpenAPI spec.",
27-
Annotations: map[string]string{
28-
"toc": "true",
29-
},
3027
}
3128

3229
cmd.AddCommand(

tools/cli/internal/cli/changelog/convert/convert.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ func Builder() *cobra.Command {
2222
cmd := &cobra.Command{
2323
Use: "convert",
2424
Short: "Convert API Changelog entries into another format.",
25-
Annotations: map[string]string{
26-
"toc": "true",
27-
},
2825
}
2926

3027
cmd.AddCommand(SlackBuilder())

tools/cli/internal/cli/changelog/metadata/metadata.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ func Builder() *cobra.Command {
2222
cmd := &cobra.Command{
2323
Use: "metadata",
2424
Short: "Manage the API Changelog Metadata.",
25-
Annotations: map[string]string{
26-
"toc": "true",
27-
},
2825
}
2926

3027
cmd.AddCommand(CreateBuilder())

tools/cli/internal/cli/flag/flag.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,6 @@ const (
4545
MessageID = "msg-id"
4646
ChannelID = "channel-id"
4747
ChannelIDShort = "c"
48+
From = "from"
49+
To = "to"
4850
)

tools/cli/internal/cli/split/split.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (o *Opts) Run() error {
6868
}
6969

7070
func (o *Opts) filter(oas *openapi3.T, version string) (result *openapi3.T, err error) {
71-
log.Printf("Filtering OpenAPI document by version '%s'", version)
71+
log.Printf("Filtering OpenAPI document by version %q", version)
7272
apiVersion, err := apiversion.New(apiversion.WithVersion(version))
7373
if err != nil {
7474
return nil, err

tools/cli/internal/cli/sunset/list.go

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"encoding/json"
1919
"fmt"
2020
"strings"
21+
"time"
2122

2223
"github.com/mongodb/openapi/tools/cli/internal/cli/flag"
2324
"github.com/mongodb/openapi/tools/cli/internal/cli/usage"
@@ -32,6 +33,10 @@ type ListOpts struct {
3233
basePath string
3334
outputPath string
3435
format string
36+
from string
37+
to string
38+
toDate *time.Time
39+
fromDate *time.Time
3540
}
3641

3742
func (o *ListOpts) Run() error {
@@ -41,7 +46,12 @@ func (o *ListOpts) Run() error {
4146
return err
4247
}
4348

44-
bytes, err := o.newSunsetListBytes(openapi.NewSunsetListFromSpec(specInfo))
49+
sunsets, err := o.newSunsetInRange(openapi.NewSunsetListFromSpec(specInfo))
50+
if err != nil {
51+
return err
52+
}
53+
54+
bytes, err := o.newSunsetListBytes(sunsets)
4555
if err != nil {
4656
return err
4757
}
@@ -53,6 +63,42 @@ func (o *ListOpts) Run() error {
5363
return nil
5464
}
5565

66+
func (o *ListOpts) newSunsetInRange(sunsets []*openapi.Sunset) ([]*openapi.Sunset, error) {
67+
var out []*openapi.Sunset
68+
if o.from == "" && o.to == "" {
69+
return sunsets, nil
70+
}
71+
72+
for _, s := range sunsets {
73+
sunsetDate, err := time.Parse("2006-01-02", s.SunsetDate)
74+
if err != nil {
75+
return nil, err
76+
}
77+
78+
if isDateInRange(&sunsetDate, o.fromDate, o.toDate) {
79+
out = append(out, s)
80+
}
81+
}
82+
83+
return out, nil
84+
}
85+
86+
func isDateInRange(date, from, to *time.Time) bool {
87+
if date == nil {
88+
return false
89+
}
90+
91+
if from != nil && date.Before(*from) {
92+
return false
93+
}
94+
95+
if to != nil && date.After(*to) {
96+
return false
97+
}
98+
99+
return true
100+
}
101+
56102
func (o *ListOpts) newSunsetListBytes(versions []*openapi.Sunset) ([]byte, error) {
57103
data, err := json.MarshalIndent(versions, "", " ")
58104
if err != nil {
@@ -63,7 +109,7 @@ func (o *ListOpts) newSunsetListBytes(versions []*openapi.Sunset) ([]byte, error
63109
return data, nil
64110
}
65111

66-
var jsonData interface{}
112+
var jsonData any
67113
if mErr := json.Unmarshal(data, &jsonData); mErr != nil {
68114
return nil, mErr
69115
}
@@ -76,8 +122,28 @@ func (o *ListOpts) newSunsetListBytes(versions []*openapi.Sunset) ([]byte, error
76122
return yamlData, nil
77123
}
78124

125+
func (o *ListOpts) validate() error {
126+
if o.from != "" {
127+
value, err := time.Parse("2006-01-02", o.from)
128+
if err != nil {
129+
return err
130+
}
131+
o.fromDate = &value
132+
}
133+
134+
if o.to != "" {
135+
value, err := time.Parse("2006-01-02", o.to)
136+
if err != nil {
137+
return err
138+
}
139+
o.toDate = &value
140+
}
141+
142+
return nil
143+
}
144+
79145
// ListBuilder builds the merge command with the following signature:
80-
// changelog create -b path_folder -r path_folder --dry-run
146+
// sunset ls -s spec.json -f 2024-01-01 -t 2024-09-22
81147
func ListBuilder() *cobra.Command {
82148
opts := &ListOpts{
83149
fs: afero.NewOsFs(),
@@ -88,13 +154,18 @@ func ListBuilder() *cobra.Command {
88154
Short: "List API endpoints with a Sunset date for a given OpenAPI spec.",
89155
Aliases: []string{"ls"},
90156
Args: cobra.NoArgs,
157+
PreRunE: func(_ *cobra.Command, _ []string) error {
158+
return opts.validate()
159+
},
91160
RunE: func(_ *cobra.Command, _ []string) error {
92161
return opts.Run()
93162
},
94163
}
95164

96165
cmd.Flags().StringVarP(&opts.basePath, flag.Spec, flag.SpecShort, "", usage.Spec)
97166
cmd.Flags().StringVarP(&opts.outputPath, flag.Output, flag.OutputShort, "", usage.Output)
167+
cmd.Flags().StringVar(&opts.from, flag.From, "", usage.From)
168+
cmd.Flags().StringVar(&opts.to, flag.To, "", usage.To)
98169
cmd.Flags().StringVarP(&opts.format, flag.Format, flag.FormatShort, "json", usage.Format)
99170

100171
_ = cmd.MarkFlagRequired(flag.Spec)

tools/cli/internal/cli/sunset/sunset.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ func Builder() *cobra.Command {
2222
cmd := &cobra.Command{
2323
Use: "sunset",
2424
Short: "Manage the Sunset API for the OpenAPI spec.",
25-
Annotations: map[string]string{
26-
"toc": "true",
27-
},
2825
}
2926

3027
cmd.AddCommand(ListBuilder())

tools/cli/internal/cli/usage/usage.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,6 @@ const (
3535
Path = "Path to the changelog file."
3636
MessageID = "Message ID of the slack message. This ID is used to add the message as slack thread."
3737
SlackChannelID = "Slack Channel ID."
38+
From = "Date in the format YYYY-MM-DD that indicates the start of a date range"
39+
To = "Date in the format YYYY-MM-DD that indicates the end of a date range"
3840
)

0 commit comments

Comments
 (0)