Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLOUDP-238932: Validate maxDate and minDate before running command #2794

Merged
merged 8 commits into from Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions internal/cli/atlas/events/list.go
Expand Up @@ -130,6 +130,22 @@ func (opts *ListOpts) NewProjectListOptions() admin.ListProjectEventsApiParams {
return p
}

func (opts *EventListOpts) ValidateMaxAndMinDates() func() error {
blva marked this conversation as resolved.
Show resolved Hide resolved
return func() error {
if opts.MaxDate != "" {
if _, err := convert.ParseTimestamp(opts.MaxDate); err != nil {
return fmt.Errorf("invalid maxDate format, %w", err)
}
}
if opts.MinDate != "" {
if _, err := convert.ParseTimestamp(opts.MinDate); err != nil {
return fmt.Errorf("invalid minDate format, %w", err)
}
}
return nil
}
}

// ListBuilder
//
// atlas event(s) list
Expand Down Expand Up @@ -164,6 +180,11 @@ func ListBuilder() *cobra.Command {
return fmt.Errorf("--%s or --%s must be set", flag.ProjectID, flag.OrgID)
}
opts.OutWriter = cmd.OutOrStdout()

if err := opts.ValidateMaxAndMinDates()(); err != nil {
return err
}

return opts.initStore(cmd.Context())()
},
RunE: func(_ *cobra.Command, _ []string) error {
Expand Down
1 change: 1 addition & 0 deletions internal/cli/atlas/events/orgs_list.go
Expand Up @@ -106,6 +106,7 @@ func OrgListBuilder() *cobra.Command {
opts.ValidateOrgID,
opts.initStore(cmd.Context()),
opts.InitOutput(cmd.OutOrStdout(), listTemplate),
opts.ValidateMaxAndMinDates(),
)
},
RunE: func(_ *cobra.Command, _ []string) error {
Expand Down
1 change: 1 addition & 0 deletions internal/cli/atlas/events/projects_list.go
Expand Up @@ -110,6 +110,7 @@ func ProjectListBuilder() *cobra.Command {
opts.ValidateProjectID,
opts.initStore(cmd.Context()),
opts.InitOutput(cmd.OutOrStdout(), listTemplate),
opts.ValidateMaxAndMinDates(),
)
},
RunE: func(_ *cobra.Command, _ []string) error {
Expand Down
9 changes: 7 additions & 2 deletions internal/convert/time.go
Expand Up @@ -14,7 +14,12 @@

package convert

import "time"
import (
"fmt"
"time"
)

var ErrInvalidTimestamp = fmt.Errorf("supported timestamp formats examples: %s, 2006-01-02T15:04:05-0700", time.RFC3339)

func ParseTimestamp(timestamp string) (time.Time, error) {
layouts := []string{
Expand All @@ -31,5 +36,5 @@ func ParseTimestamp(timestamp string) (time.Time, error) {
return parsedTime, err
}
}
return parsedTime, err
return parsedTime, fmt.Errorf("%w, %w", err, ErrInvalidTimestamp)
}