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

feature(cli): adding flag and parameter validations for commands #2600

Conversation

xoscar
Copy link
Collaborator

@xoscar xoscar commented May 26, 2023

This PR adds parameter based logic to validate different flags and args for each of the commands, returns all errors at once depending on the validation checks

Changes

  • Adds new middleware to support parameter validation
  • Adds new parameters interface and validators for commands

Fixes

Checklist

  • tested locally
  • added new dependencies
  • updated the docs
  • added a test

https://www.loom.com/share/9efaab50749045bf9ce4557b93230806

@xoscar xoscar added the CLI label May 26, 2023
@xoscar xoscar self-assigned this May 26, 2023
@xoscar xoscar linked an issue May 26, 2023 that may be closed by this pull request
@xoscar xoscar marked this pull request as ready for review May 26, 2023 22:57
@xoscar xoscar requested review from jorgeepc, danielbdias, schoren and mathnogueira and removed request for jorgeepc and danielbdias May 26, 2023 23:00
Copy link
Contributor

@danielbdias danielbdias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The overall PR is fine! Before merging it can you address just the len(args) comment, please?

Comment on lines +10 to +11
RunEnvironment installer.RunEnvironmentType
InstallationMode installer.InstallationModeType
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this new schema to validate our parameters, does it make sense to validate them on
https://github.com/kubeshop/tracetest/blob/main/cli/installer/types.go#L11 too?

Today they implement the Value interface on: https://github.com/spf13/pflag/blob/v1.0.5/flag.go#L187 to have an "enum-like" validation. (I forgot to document this inheritance, my bad =/)

I believe that we can simplify these types and focus on the validation here in the future.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think we should focus on validations for the moment, we can update types later on

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got your point now, the problem I see is that the validations are done one at a time, and ideally we'll want to show all possible errors at once, does it make sense?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense! Later we can drop this support for the Value interface and focus on the validation that we do here.

Comment on lines 20 to 32
if cmd.Flags().Lookup("run-environment").Changed && p.RunEnvironment != installer.NoneRunEnvironmentType && p.RunEnvironment != installer.DockerRunEnvironmentType && p.RunEnvironment != installer.KubernetesRunEnvironmentType {
errors = append(errors, ParamError{
Parameter: "run-environment",
Message: "run-environment must be one of 'none', 'docker' or 'kubernetes'",
})
}

if cmd.Flags().Lookup("mode").Changed && p.InstallationMode != installer.NotChosenInstallationModeType && p.InstallationMode != installer.WithDemoInstallationModeType && p.InstallationMode != installer.WithoutDemoInstallationModeType {
errors = append(errors, ParamError{
Parameter: "mode",
Message: "mode must be one of 'not-chosen', 'with-demo' or 'just-tracetest'",
})
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this validation to a "Contains" style? Something like this:

// after the imports
var (
  AllowedRunEnvironments = []installation.RunEnvironmentType{
    installer.DockerRunEnvironmentType,
    installer.KubernetesRunEnvironmentType,
    installer.NoneRunEnvironmentType,
  }
  AllowedInstallationMode = []installation.InstallationModeType{
    installer.WithDemoInstallationModeType,
    installer.WithoutDemoInstallationModeType,
    installer.NotChosenInstallationModeType,
  }
)

// here on the validations
if cmd.Flags().Lookup("run-environment").Changed && !slices.Contains(AllowedRunEnvironments, p.RunEnvironment) {
 // same code
}

if cmd.Flags().Lookup("mode").Changed && !slices.Contains(AllowedInstallationMode, p. InstallationMode) {
 // same code
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know about the slices lib haha thanks for pointing that out!

func (p *ResourceParams) Validate(cmd *cobra.Command, args []string) []ParamError {
errors := make([]ParamError, 0)

p.ResourceName = args[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should validate if the resource name came in args (i.e., if len(args) > 0). Otherwise, we can have a panic here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Donerino 😄

@xoscar xoscar requested a review from danielbdias May 29, 2023 16:00
Copy link
Contributor

@jorgeepc jorgeepc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it. Muy bueno! 🔥

@xoscar xoscar merged commit 07f4ec2 into main May 29, 2023
27 checks passed
@xoscar xoscar deleted the 2305-cli-improvements-add-validations-for-commands-flags-and-arguments branch May 29, 2023 16:21
schoren pushed a commit that referenced this pull request Jun 5, 2023
* feature(cli): adding flag and parameter validations for commands

* fixing get command

* PR review commentsc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[CLI Improvements] add validations for command's flags and arguments
3 participants