Skip to content

Commit

Permalink
separate validate command into file and online
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske committed Jul 15, 2023
1 parent b8da522 commit 6acdd6d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 25 deletions.
51 changes: 36 additions & 15 deletions cmd/kong_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
Expand All @@ -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,
Expand All @@ -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"+
Expand Down
20 changes: 10 additions & 10 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
}
Expand Down

0 comments on commit 6acdd6d

Please sign in to comment.