From 6acdd6d5a021373b6403f60b144b75919b3c44c6 Mon Sep 17 00:00:00 2001 From: Thijs Schreijer Date: Sat, 15 Jul 2023 21:07:17 +0200 Subject: [PATCH] separate validate command into file and online --- cmd/kong_validate.go | 51 +++++++++++++++++++++++++++++++------------- cmd/root.go | 20 ++++++++--------- 2 files changed, 46 insertions(+), 25 deletions(-) diff --git a/cmd/kong_validate.go b/cmd/kong_validate.go index f53ded12f..888e05c06 100644 --- a/cmd/kong_validate.go +++ b/cmd/kong_validate.go @@ -72,9 +72,15 @@ func executeValidate(cmd *cobra.Command, _ []string) error { } // newValidateCmd represents the diff command -func newValidateCmd(deprecated bool) *cobra.Command { +func newValidateCmd(deprecated bool, online bool) *cobra.Command { use := "validate [flags] [kong-state-files...]" short := "Validate the state file" + long := `The validate command reads the state file and ensures validity. +It reads all the specified state files and reports YAML/JSON +parsing issues. It also checks for foreign relationships +and alerts if there are broken relationships, or missing links present. + +` execute := executeValidate argsValidator := cobra.MinimumNArgs(0) preRun := func(cmd *cobra.Command, args []string) error { @@ -88,6 +94,15 @@ func newValidateCmd(deprecated bool) *cobra.Command { if deprecated { use = "validate" short = "[deprecated] use 'kong validate' instead" + long = `The validate command reads the state file and ensures validity. +It reads all the specified state files and reports YAML/JSON +parsing issues. It also checks for foreign relationships +and alerts if there are broken relationships, or missing links present. + +No communication takes places between decK and Kong during the execution of +this command unless --online flag is used. +` + execute = func(cmd *cobra.Command, args []string) error { cprint.UpdatePrintf("Warning: 'deck validate' is DEPRECATED and will be removed in a future version. " + "Use 'deck kong validate' instead.\n") @@ -101,19 +116,25 @@ func newValidateCmd(deprecated bool) *cobra.Command { } return preRunSilenceEventsFlag() } + } else { + validateOnline = online + if validateOnline { + short = short + " (online)" + long = long + "Validates against the Kong API, via communication with Kong. This increases the\n" + + "time for validation but catches significant errors. No resource is created in Kong.\n" + + "For offline validation see 'deck file validate'.\n" + } else { + short = short + " (locally)" + long = long + "No communication takes places between decK and Kong during the execution of\n" + + "this command. This is faster than the online validation, but catches fewer errors.\n" + + "For online validation see 'deck kong validate'.\n" + } } validateCmd := &cobra.Command{ - Use: use, - Short: short, - Long: `The validate command reads the state file and ensures validity. -It reads all the specified state files and reports YAML/JSON -parsing issues. It also checks for foreign relationships -and alerts if there are broken relationships, or missing links present. - -No communication takes places between decK and Kong during the execution of -this command unless --online flag is used. -`, + Use: use, + Short: short, + Long: long, Args: argsValidator, RunE: execute, PreRunE: preRun, @@ -126,11 +147,11 @@ this command unless --online flag is used. "state", "s", []string{"kong.yaml"}, "file(s) containing Kong's configuration.\n"+ "This flag can be specified multiple times for multiple files.\n"+ "Use '-' to read from stdin.") + validateCmd.Flags().BoolVar(&validateOnline, "online", + false, "perform validations against Kong API. When this flag is used, validation is done\n"+ + "via communication with Kong. This increases the time for validation but catches \n"+ + "significant errors. No resource is created in Kong.") } - validateCmd.Flags().BoolVar(&validateOnline, "online", - false, "perform validations against Kong API. When this flag is used, validation is done\n"+ - "via communication with Kong. This increases the time for validation but catches \n"+ - "significant errors. No resource is created in Kong.") validateCmd.Flags().StringVarP(&validateWorkspace, "workspace", "w", "", "validate configuration of a specific workspace "+ "(Kong Enterprise only).\n"+ diff --git a/cmd/root.go b/cmd/root.go index fc4c017c4..8cc531f08 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -206,19 +206,19 @@ It can be used to export, import, or sync entities to Kong.`, rootCmd.AddCommand(newVersionCmd()) rootCmd.AddCommand(newCompletionCmd()) - rootCmd.AddCommand(newSyncCmd(true)) // deprecated, to exist under the `kong` subcommand only - rootCmd.AddCommand(newValidateCmd(true)) // deprecated, to exist under both `kong` and `file` subcommands - rootCmd.AddCommand(newResetCmd(true)) // deprecated, to exist under the `kong` subcommand only - rootCmd.AddCommand(newPingCmd(true)) // deprecated, to exist under the `kong` subcommand only - rootCmd.AddCommand(newDumpCmd(true)) // deprecated, to exist under the `kong` subcommand only - rootCmd.AddCommand(newDiffCmd(true)) // deprecated, to exist under the `kong` subcommand only - rootCmd.AddCommand(newConvertCmd(true)) // deprecated, to exist under the `file` subcommand only - rootCmd.AddCommand(newKonnectCmd()) // deprecated, to be removed + rootCmd.AddCommand(newSyncCmd(true)) // deprecated, to exist under the `kong` subcommand only + rootCmd.AddCommand(newValidateCmd(true, false)) // deprecated, to exist under both `kong` and `file` subcommands + rootCmd.AddCommand(newResetCmd(true)) // deprecated, to exist under the `kong` subcommand only + rootCmd.AddCommand(newPingCmd(true)) // deprecated, to exist under the `kong` subcommand only + rootCmd.AddCommand(newDumpCmd(true)) // deprecated, to exist under the `kong` subcommand only + rootCmd.AddCommand(newDiffCmd(true)) // deprecated, to exist under the `kong` subcommand only + rootCmd.AddCommand(newConvertCmd(true)) // deprecated, to exist under the `file` subcommand only + rootCmd.AddCommand(newKonnectCmd()) // deprecated, to be removed { kongCmd := newKongSubCmd() rootCmd.AddCommand(kongCmd) kongCmd.AddCommand(newSyncCmd(false)) - kongCmd.AddCommand(newValidateCmd(false)) + kongCmd.AddCommand(newValidateCmd(false, true)) // online validation kongCmd.AddCommand(newResetCmd(false)) kongCmd.AddCommand(newPingCmd(false)) kongCmd.AddCommand(newDumpCmd(false)) @@ -235,7 +235,7 @@ It can be used to export, import, or sync entities to Kong.`, fileCmd.AddCommand(newPatchCmd()) fileCmd.AddCommand(newOpenapi2KongCmd()) fileCmd.AddCommand(newConvertCmd(false)) - fileCmd.AddCommand(newValidateCmd(false)) // alias; since this does both file+online + fileCmd.AddCommand(newValidateCmd(false, false)) // file-based validation } return rootCmd }