Skip to content

Commit

Permalink
fix(kumactl) Only warn about version compatibility where it makes sense
Browse files Browse the repository at this point in the history
Warn for: apply, config, delete, generate, get, inspect
Don't warn for: completion, install, uninstall, version

Signed-off-by: Michael Beaumont <mjboamail@gmail.com>
  • Loading branch information
michaelbeaumont committed Sep 23, 2021
1 parent bbe7b6b commit 90f74a9
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 26 deletions.
4 changes: 4 additions & 0 deletions app/kumactl/cmd/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ Apply a resource from external URL
$ kumactl apply -f https://example.com/resource.yaml
`,
RunE: func(cmd *cobra.Command, args []string) error {
if err := pctx.CheckServerVersionCompatibility(); err != nil {
cmd.PrintErrln(err)
}

var b []byte
var err error

Expand Down
17 changes: 13 additions & 4 deletions app/kumactl/cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,22 @@ import (
)

func NewConfigCmd(pctx *kumactl_cmd.RootContext) *cobra.Command {
cmd := &cobra.Command{
configCmd := &cobra.Command{
Use: "config",
Short: "Manage kumactl config",
Long: `Manage kumactl config.`,
}
configCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
if err := kumactl_cmd.RunParentPreRunE(configCmd, args); err != nil {
return err
}
if err := pctx.CheckServerVersionCompatibility(); err != nil {
cmd.PrintErrln(err)
}
return nil
}
// sub-commands
cmd.AddCommand(newConfigViewCmd(pctx))
cmd.AddCommand(newConfigControlPlanesCmd(pctx))
return cmd
configCmd.AddCommand(newConfigViewCmd(pctx))
configCmd.AddCommand(newConfigControlPlanesCmd(pctx))
return configCmd
}
4 changes: 4 additions & 0 deletions app/kumactl/cmd/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func NewDeleteCmd(pctx *kumactl_cmd.RootContext) *cobra.Command {
Long: `Delete Kuma resources.`,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
if err := pctx.CheckServerVersionCompatibility(); err != nil {
cmd.PrintErrln(err)
}

resourceTypeArg := args[0]
name := args[1]

Expand Down
19 changes: 14 additions & 5 deletions app/kumactl/cmd/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,23 @@ import (
)

func NewGenerateCmd(pctx *kumactl_cmd.RootContext) *cobra.Command {
cmd := &cobra.Command{
generateCmd := &cobra.Command{
Use: "generate",
Short: "Generate resources, tokens, etc",
Long: `Generate resources, tokens, etc.`,
}
generateCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
if err := kumactl_cmd.RunParentPreRunE(generateCmd, args); err != nil {
return err
}
if err := pctx.CheckServerVersionCompatibility(); err != nil {
cmd.PrintErrln(err)
}
return nil
}
// sub-commands
cmd.AddCommand(NewGenerateDataplaneTokenCmd(pctx))
cmd.AddCommand(NewGenerateZoneIngressTokenCmd(pctx))
cmd.AddCommand(NewGenerateCertificateCmd(pctx))
return cmd
generateCmd.AddCommand(NewGenerateDataplaneTokenCmd(pctx))
generateCmd.AddCommand(NewGenerateZoneIngressTokenCmd(pctx))
generateCmd.AddCommand(NewGenerateCertificateCmd(pctx))
return generateCmd
}
19 changes: 14 additions & 5 deletions app/kumactl/cmd/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,27 @@ import (
)

func NewGetCmd(pctx *kumactl_cmd.RootContext) *cobra.Command {
cmd := &cobra.Command{
getCmd := &cobra.Command{
Use: "get",
Short: "Show Kuma resources",
Long: `Show Kuma resources.`,
}
getCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
if err := kumactl_cmd.RunParentPreRunE(getCmd, args); err != nil {
return err
}
if err := pctx.CheckServerVersionCompatibility(); err != nil {
cmd.PrintErrln(err)
}
return nil
}
// flags
cmd.PersistentFlags().StringVarP(&pctx.GetContext.Args.OutputFormat, "output", "o", string(output.TableFormat), kuma_cmd.UsageOptions("output format", output.TableFormat, output.YAMLFormat, output.JSONFormat))
getCmd.PersistentFlags().StringVarP(&pctx.GetContext.Args.OutputFormat, "output", "o", string(output.TableFormat), kuma_cmd.UsageOptions("output format", output.TableFormat, output.YAMLFormat, output.JSONFormat))
for _, cmdInst := range pctx.Runtime.Registry.ObjectDescriptors(model.HasKumactlEnabled()) {
cmd.AddCommand(WithPaginationArgs(NewGetResourcesCmd(pctx, cmdInst), &pctx.ListContext))
cmd.AddCommand(NewGetResourceCmd(pctx, cmdInst))
getCmd.AddCommand(WithPaginationArgs(NewGetResourcesCmd(pctx, cmdInst), &pctx.ListContext))
getCmd.AddCommand(NewGetResourceCmd(pctx, cmdInst))
}
return cmd
return getCmd
}

func WithPaginationArgs(cmd *cobra.Command, ctx *get_context.ListContext) *cobra.Command {
Expand Down
25 changes: 17 additions & 8 deletions app/kumactl/cmd/inspect/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,27 @@ import (
)

func NewInspectCmd(pctx *kumactl_cmd.RootContext) *cobra.Command {
cmd := &cobra.Command{
inspectCmd := &cobra.Command{
Use: "inspect",
Short: "Inspect Kuma resources",
Long: `Inspect Kuma resources.`,
}
inspectCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
if err := kumactl_cmd.RunParentPreRunE(inspectCmd, args); err != nil {
return err
}
if err := pctx.CheckServerVersionCompatibility(); err != nil {
cmd.PrintErrln(err)
}
return nil
}
// flags
cmd.PersistentFlags().StringVarP(&pctx.InspectContext.Args.OutputFormat, "output", "o", string(output.TableFormat), kuma_cmd.UsageOptions("output format", output.TableFormat, output.YAMLFormat, output.JSONFormat))
inspectCmd.PersistentFlags().StringVarP(&pctx.InspectContext.Args.OutputFormat, "output", "o", string(output.TableFormat), kuma_cmd.UsageOptions("output format", output.TableFormat, output.YAMLFormat, output.JSONFormat))
// sub-commands
cmd.AddCommand(newInspectDataplanesCmd(pctx))
cmd.AddCommand(newInspectZoneIngressesCmd(pctx))
cmd.AddCommand(newInspectZonesCmd(pctx))
cmd.AddCommand(newInspectMeshesCmd(pctx))
cmd.AddCommand(newInspectServicesCmd(pctx))
return cmd
inspectCmd.AddCommand(newInspectDataplanesCmd(pctx))
inspectCmd.AddCommand(newInspectZoneIngressesCmd(pctx))
inspectCmd.AddCommand(newInspectZonesCmd(pctx))
inspectCmd.AddCommand(newInspectMeshesCmd(pctx))
inspectCmd.AddCommand(newInspectServicesCmd(pctx))
return inspectCmd
}
4 changes: 0 additions & 4 deletions app/kumactl/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ func NewRootCmd(root *kumactl_cmd.RootContext) *cobra.Command {
return err
}

if err := root.CheckServerVersionCompatibility(); err != nil {
cmd.PrintErrln(err)
}

return nil
},
}
Expand Down
15 changes: 15 additions & 0 deletions app/kumactl/pkg/cmd/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cmd

import "github.com/spf13/cobra"

// RunParentPreRunE checks if the parent command has a PersistentPreRunE set and
// executes it. This is necessary because only the first PersistentPreRun* in
// the ancestroy is executed by cobra.
func RunParentPreRunE(cmd *cobra.Command, args []string) error {
if p := cmd.Parent(); p != nil && p.PersistentPreRunE != nil {
if err := p.PersistentPreRunE(p, args); err != nil {
return err
}
}
return nil
}

0 comments on commit 90f74a9

Please sign in to comment.