Skip to content

Commit

Permalink
feature(cli): adding flag and parameter validations for commands (#2600)
Browse files Browse the repository at this point in the history
* feature(cli): adding flag and parameter validations for commands

* fixing get command

* PR review commentsc
  • Loading branch information
xoscar committed May 29, 2023
1 parent c1af6b5 commit 07f4ec2
Show file tree
Hide file tree
Showing 17 changed files with 374 additions and 117 deletions.
27 changes: 11 additions & 16 deletions cli/cmd/apply_cmd.go
Expand Up @@ -8,24 +8,19 @@ import (
"github.com/kubeshop/tracetest/cli/actions"
"github.com/kubeshop/tracetest/cli/analytics"
"github.com/kubeshop/tracetest/cli/formatters"
"github.com/kubeshop/tracetest/cli/parameters"
"github.com/spf13/cobra"
)

var definitionFile string
var applyParams = &parameters.ApplyParams{}

var applyCmd = &cobra.Command{
GroupID: cmdGroupResources.ID,
Use: fmt.Sprintf("apply %s", strings.Join(validArgs, "|")),
Short: "Apply resources",
Long: "Apply (create/update) resources to your Tracetest server",
PreRun: setupCommand(),
Args: cobra.MatchAll(cobra.MinimumNArgs(1), cobra.OnlyValidArgs),
ValidArgs: validArgs,
Run: WithResultHandler(func(cmd *cobra.Command, args []string) (string, error) {
if definitionFile == "" {
return "", fmt.Errorf("file with definition must be specified")
}

GroupID: cmdGroupResources.ID,
Use: fmt.Sprintf("apply %s", strings.Join(parameters.ValidResources, "|")),
Short: "Apply resources",
Long: "Apply (create/update) resources to your Tracetest server",
PreRun: setupCommand(),
Run: WithResourceMiddleware(func(_ *cobra.Command, args []string) (string, error) {
resourceType := args[0]
ctx := context.Background()

Expand All @@ -40,7 +35,7 @@ var applyCmd = &cobra.Command{
}

applyArgs := actions.ApplyArgs{
File: definitionFile,
File: applyParams.DefinitionFile,
}

resource, _, err := resourceActions.Apply(ctx, applyArgs)
Expand All @@ -57,11 +52,11 @@ var applyCmd = &cobra.Command{
}

return result, nil
}),
}, applyParams),
PostRun: teardownCommand,
}

func init() {
applyCmd.Flags().StringVarP(&definitionFile, "file", "f", "", "file path with name where to export the resource")
applyCmd.Flags().StringVarP(&applyParams.DefinitionFile, "file", "f", "", "file path with name where to export the resource")
rootCmd.AddCommand(applyCmd)
}
13 changes: 8 additions & 5 deletions cli/cmd/config.go
Expand Up @@ -9,17 +9,20 @@ import (
"github.com/kubeshop/tracetest/cli/analytics"
"github.com/kubeshop/tracetest/cli/config"
"github.com/kubeshop/tracetest/cli/formatters"
"github.com/kubeshop/tracetest/cli/parameters"
"github.com/kubeshop/tracetest/cli/utils"
"github.com/spf13/cobra"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

var cliConfig config.Config
var cliLogger *zap.Logger
var resourceRegistry = actions.NewResourceRegistry()
var validArgs = []string{"config", "datastore", "demo", "environment", "pollingprofile", "transaction"}
var versionText string
var (
cliConfig config.Config
cliLogger *zap.Logger
resourceRegistry = actions.NewResourceRegistry()
versionText string
resourceParams = &parameters.ResourceParams{}
)

type setupConfig struct {
shouldValidateConfig bool
Expand Down
21 changes: 10 additions & 11 deletions cli/cmd/configure_cmd.go
Expand Up @@ -5,43 +5,42 @@ import (

"github.com/kubeshop/tracetest/cli/actions"
"github.com/kubeshop/tracetest/cli/analytics"
"github.com/kubeshop/tracetest/cli/parameters"
"github.com/kubeshop/tracetest/cli/utils"
"github.com/spf13/cobra"
)

var analyticsEnabled bool
var endpoint string
var global bool
var configParams = &parameters.ConfigureParams{}

var configureCmd = &cobra.Command{
GroupID: cmdGroupConfig.ID,
Use: "configure",
Short: "Configure your tracetest CLI",
Long: "Configure your tracetest CLI",
PreRun: setupLogger,
Run: WithResultHandler(func(cmd *cobra.Command, _ []string) (string, error) {
Run: WithResultHandler(WithParamsHandler(configParams)(func(cmd *cobra.Command, _ []string) (string, error) {
analytics.Track("Configure", "cmd", map[string]string{})

ctx := context.Background()
client := utils.GetAPIClient(cliConfig)
action := actions.NewConfigureAction(cliConfig, cliLogger, client)

actionConfig := actions.ConfigureConfig{
Global: global,
Global: configParams.Global,
SetValues: actions.ConfigureConfigSetValues{},
}

if flagProvided(cmd, "endpoint") {
actionConfig.SetValues.Endpoint = &endpoint
actionConfig.SetValues.Endpoint = &configParams.Endpoint
}

if flagProvided(cmd, "analytics") {
actionConfig.SetValues.AnalyticsEnabled = &analyticsEnabled
actionConfig.SetValues.AnalyticsEnabled = &configParams.AnalyticsEnabled
}

err := action.Run(ctx, actionConfig)
return "", err
}),
})),
PostRun: teardownCommand,
}

Expand All @@ -50,8 +49,8 @@ func flagProvided(cmd *cobra.Command, name string) bool {
}

func init() {
configureCmd.PersistentFlags().BoolVarP(&global, "global", "g", false, "configuration will be saved in your home dir")
configureCmd.PersistentFlags().StringVarP(&endpoint, "endpoint", "e", "", "set the value for the endpoint, so the CLI won't ask for this value")
configureCmd.PersistentFlags().BoolVarP(&analyticsEnabled, "analytics", "a", true, "configure the analytic state, so the CLI won't ask for this value")
configureCmd.PersistentFlags().BoolVarP(&configParams.Global, "global", "g", false, "configuration will be saved in your home dir")
configureCmd.PersistentFlags().StringVarP(&configParams.Endpoint, "endpoint", "e", "", "set the value for the endpoint, so the CLI won't ask for this value")
configureCmd.PersistentFlags().BoolVarP(&configParams.AnalyticsEnabled, "analytics", "a", true, "configure the analytic state, so the CLI won't ask for this value")
rootCmd.AddCommand(configureCmd)
}
4 changes: 2 additions & 2 deletions cli/cmd/datastore_legacy_cmd.go
Expand Up @@ -33,7 +33,7 @@ var dataStoreApplyCmd = &cobra.Command{
PreRun: setupCommand(),
Run: func(cmd *cobra.Command, args []string) {
// call new apply command
definitionFile = dataStoreApplyFile
applyParams.DefinitionFile = dataStoreApplyFile
applyCmd.Run(applyCmd, []string{"datastore"})
},
PostRun: teardownCommand,
Expand Down Expand Up @@ -62,7 +62,7 @@ var dataStoreListCmd = &cobra.Command{
PreRun: setupCommand(),
Run: func(cmd *cobra.Command, args []string) {
// call new get command
resourceID = "current"
getParams.ResourceId = "current"
getCmd.Run(getCmd, []string{"datastore"})
},
PostRun: teardownCommand,
Expand Down
27 changes: 11 additions & 16 deletions cli/cmd/delete_cmd.go
Expand Up @@ -6,24 +6,19 @@ import (
"strings"

"github.com/kubeshop/tracetest/cli/analytics"
"github.com/kubeshop/tracetest/cli/parameters"
"github.com/spf13/cobra"
)

var deletedResourceID string
var deleteParams = &parameters.ResourceIdParams{}

var deleteCmd = &cobra.Command{
GroupID: cmdGroupResources.ID,
Use: fmt.Sprintf("delete %s", strings.Join(validArgs, "|")),
Short: "Delete resources",
Long: "Delete resources from your Tracetest server",
PreRun: setupCommand(),
Args: cobra.MatchAll(cobra.MinimumNArgs(1), cobra.OnlyValidArgs),
ValidArgs: validArgs,
Run: WithResultHandler(func(_ *cobra.Command, args []string) (string, error) {
if deletedResourceID == "" {
return "", fmt.Errorf("id of the resource to delete must be specified")
}

GroupID: cmdGroupResources.ID,
Use: fmt.Sprintf("delete %s", strings.Join(parameters.ValidResources, "|")),
Short: "Delete resources",
Long: "Delete resources from your Tracetest server",
PreRun: setupCommand(),
Run: WithResourceMiddleware(func(_ *cobra.Command, args []string) (string, error) {
resourceType := args[0]
ctx := context.Background()

Expand All @@ -36,17 +31,17 @@ var deleteCmd = &cobra.Command{
return "", err
}

message, err := resourceActions.Delete(ctx, deletedResourceID)
message, err := resourceActions.Delete(ctx, deleteParams.ResourceId)
if err != nil {
return "", err
}

return fmt.Sprintf("✔ %s", message), nil
}),
}, deleteParams),
PostRun: teardownCommand,
}

func init() {
deleteCmd.Flags().StringVar(&deletedResourceID, "id", "", "id of the resource to delete")
deleteCmd.Flags().StringVar(&deleteParams.ResourceId, "id", "", "id of the resource to delete")
rootCmd.AddCommand(deleteCmd)
}
2 changes: 1 addition & 1 deletion cli/cmd/environment_legacy_cmd.go
Expand Up @@ -27,7 +27,7 @@ var environmentApplyCmd = &cobra.Command{
PreRun: setupCommand(),
Run: func(cmd *cobra.Command, args []string) {
// call new apply command
definitionFile = dataStoreApplyFile
applyParams.DefinitionFile = dataStoreApplyFile
applyCmd.Run(applyCmd, []string{"environment"})
},
PostRun: teardownCommand,
Expand Down
7 changes: 4 additions & 3 deletions cli/cmd/export_cmd.go
Expand Up @@ -3,8 +3,10 @@ package cmd
import (
"context"
"fmt"
"strings"

"github.com/kubeshop/tracetest/cli/analytics"
"github.com/kubeshop/tracetest/cli/parameters"
"github.com/spf13/cobra"
)

Expand All @@ -15,12 +17,11 @@ var (

var exportCmd = &cobra.Command{
GroupID: cmdGroupResources.ID,
Use: "export [resource type]",
Use: fmt.Sprintf("export %s", strings.Join(parameters.ValidResources, "|")),
Long: "Export a resource from your Tracetest server",
Short: "Export resource",
PreRun: setupCommand(),
Args: cobra.MinimumNArgs(1),
Run: WithResultHandler(func(cmd *cobra.Command, args []string) (string, error) {
Run: WithResultHandler(func(_ *cobra.Command, args []string) (string, error) {
resourceType := args[0]
ctx := context.Background()

Expand Down
29 changes: 12 additions & 17 deletions cli/cmd/get_cmd.go
Expand Up @@ -8,25 +8,20 @@ import (

"github.com/kubeshop/tracetest/cli/analytics"
"github.com/kubeshop/tracetest/cli/formatters"
"github.com/kubeshop/tracetest/cli/parameters"
"github.com/kubeshop/tracetest/cli/utils"
"github.com/spf13/cobra"
)

var resourceID string
var getParams = &parameters.ResourceIdParams{}

var getCmd = &cobra.Command{
GroupID: cmdGroupResources.ID,
Use: fmt.Sprintf("get %s", strings.Join(validArgs, "|")),
Short: "Get resource",
Long: "Get a resource from your Tracetest server",
PreRun: setupCommand(),
Args: cobra.MatchAll(cobra.MinimumNArgs(1), cobra.OnlyValidArgs),
ValidArgs: validArgs,
Run: WithResultHandler(func(cmd *cobra.Command, args []string) (string, error) {
if resourceID == "" {
return "", fmt.Errorf("id of the resource to get must be specified")
}

GroupID: cmdGroupResources.ID,
Use: fmt.Sprintf("get %s", strings.Join(parameters.ValidResources, "|")),
Short: "Get resource",
Long: "Get a resource from your Tracetest server",
PreRun: setupCommand(),
Run: WithResourceMiddleware(func(_ *cobra.Command, args []string) (string, error) {
resourceType := args[0]
ctx := context.Background()

Expand All @@ -43,9 +38,9 @@ var getCmd = &cobra.Command{
ctx = context.WithValue(ctx, "X-Tracetest-Augmented", true)
}

resource, err := resourceActions.Get(ctx, resourceID)
resource, err := resourceActions.Get(ctx, getParams.ResourceId)
if err != nil && errors.Is(err, utils.ResourceNotFound) {
return fmt.Sprintf("Resource %s with ID %s not found", resourceType, resourceID), nil
return fmt.Sprintf("Resource %s with ID %s not found", resourceType, getParams.ResourceId), nil
} else if err != nil {
return "", err
}
Expand All @@ -59,11 +54,11 @@ var getCmd = &cobra.Command{
}

return result, nil
}),
}, getParams),
PostRun: teardownCommand,
}

func init() {
getCmd.Flags().StringVar(&resourceID, "id", "", "id of the resource to get")
getCmd.Flags().StringVar(&getParams.ResourceId, "id", "", "id of the resource to get")
rootCmd.AddCommand(getCmd)
}
45 changes: 19 additions & 26 deletions cli/cmd/list_cmd.go
Expand Up @@ -7,27 +7,20 @@ import (

"github.com/kubeshop/tracetest/cli/analytics"
"github.com/kubeshop/tracetest/cli/formatters"
"github.com/kubeshop/tracetest/cli/parameters"
"github.com/kubeshop/tracetest/cli/utils"
"github.com/spf13/cobra"
)

var (
listTake int32
listSkip int32
listSortBy string
listSortDirection string
listAll bool
)
var listParams = &parameters.ListParams{}

var listCmd = &cobra.Command{
GroupID: cmdGroupResources.ID,
Use: fmt.Sprintf("list %s", strings.Join(validArgs, "|")),
Short: "List resources",
Long: "List resources from your Tracetest server",
PreRun: setupCommand(),
Args: cobra.MatchAll(cobra.MinimumNArgs(1), cobra.OnlyValidArgs),
ValidArgs: validArgs,
Run: WithResultHandler(func(cmd *cobra.Command, args []string) (string, error) {
GroupID: cmdGroupResources.ID,
Use: fmt.Sprintf("list %s", strings.Join(parameters.ValidResources, "|")),
Short: "List resources",
Long: "List resources from your Tracetest server",
PreRun: setupCommand(),
Run: WithResourceMiddleware(func(_ *cobra.Command, args []string) (string, error) {
resourceType := args[0]
ctx := context.Background()

Expand All @@ -41,11 +34,11 @@ var listCmd = &cobra.Command{
}

listArgs := utils.ListArgs{
Take: listTake,
Skip: listSkip,
SortDirection: listSortDirection,
SortBy: listSortBy,
All: listAll,
Take: listParams.Take,
Skip: listParams.Skip,
SortDirection: listParams.SortDirection,
SortBy: listParams.SortBy,
All: listParams.All,
}

resource, err := resourceActions.List(ctx, listArgs)
Expand All @@ -62,16 +55,16 @@ var listCmd = &cobra.Command{
}

return result, nil
}),
}, listParams),
PostRun: teardownCommand,
}

func init() {
listCmd.Flags().Int32Var(&listTake, "take", 20, "Take number")
listCmd.Flags().Int32Var(&listSkip, "skip", 0, "Skip number")
listCmd.Flags().StringVar(&listSortBy, "sortBy", "", "Sort by")
listCmd.Flags().StringVar(&listSortDirection, "sortDirection", "desc", "Sort direction")
listCmd.Flags().BoolVar(&listAll, "all", false, "All")
listCmd.Flags().Int32Var(&listParams.Take, "take", 20, "Take number")
listCmd.Flags().Int32Var(&listParams.Skip, "skip", 0, "Skip number")
listCmd.Flags().StringVar(&listParams.SortBy, "sortBy", "", "Sort by")
listCmd.Flags().StringVar(&listParams.SortDirection, "sortDirection", "desc", "Sort direction")
listCmd.Flags().BoolVar(&listParams.All, "all", false, "All")

rootCmd.AddCommand(listCmd)
}

0 comments on commit 07f4ec2

Please sign in to comment.