Skip to content

Commit

Permalink
chore(cli): create 'kong' subcommand for network commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske committed Jul 15, 2023
1 parent 95bc95f commit 4c87068
Show file tree
Hide file tree
Showing 11 changed files with 389 additions and 271 deletions.
4 changes: 2 additions & 2 deletions cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"fmt"
"log"
"os"

"github.com/kong/deck/convert"
Expand Down Expand Up @@ -73,7 +72,8 @@ func newConvertCmd(deprecated bool) *cobra.Command {
if deprecated {
short = "[deprecated] use 'file convert' instead"
execute = func(cmd *cobra.Command, args []string) error {
log.Println("Warning: the 'deck convert' command was deprecated and moved to 'deck file convert'")
cprint.UpdatePrintf("Warning: 'deck convert' is DEPRECATED and will be removed in a future version. " +
"Use 'deck file convert' instead.\n")
return executeConvert(cmd, args)
}
}
Expand Down
26 changes: 20 additions & 6 deletions cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"

"github.com/kong/deck/cprint"
"github.com/spf13/cobra"
)

Expand All @@ -13,22 +14,35 @@ var (
diffWorkspace string
)

func executeDiff(cmd *cobra.Command, _ []string) error {
return syncMain(cmd.Context(), diffCmdKongStateFile, true,
diffCmdParallelism, 0, diffWorkspace)
}

// newDiffCmd represents the diff command
func newDiffCmd() *cobra.Command {
func newDiffCmd(deprecated bool) *cobra.Command {
short := "Diff the current entities in Kong with the one on disks"
execute := executeDiff
if deprecated {
short = "[deprecated] use 'kong diff' instead"
execute = func(cmd *cobra.Command, args []string) error {
cprint.UpdatePrintf("Warning: 'deck diff' is DEPRECATED and will be removed in a future version. " +
"Use 'deck kong diff' instead.\n")
return executeDiff(cmd, args)
}
}

diffCmd := &cobra.Command{
Use: "diff",
Short: "Diff the current entities in Kong with the one on disks",
Short: short,
Long: `The diff command is similar to a dry run of the 'decK sync' command.
It loads entities from Kong and performs a diff with
the entities in local files. This allows you to see the entities
that will be created, updated, or deleted.
`,
Args: validateNoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return syncMain(cmd.Context(), diffCmdKongStateFile, true,
diffCmdParallelism, 0, diffWorkspace)
},
RunE: execute,
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(diffCmdKongStateFile) == 0 {
return fmt.Errorf("a state file with Kong's configuration " +
Expand Down
202 changes: 108 additions & 94 deletions cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strings"

"github.com/kong/deck/cprint"
"github.com/kong/deck/dump"
"github.com/kong/deck/file"
"github.com/kong/deck/state"
Expand Down Expand Up @@ -34,101 +35,50 @@ func listWorkspaces(ctx context.Context, client *kong.Client) ([]string, error)
return res, nil
}

// newDumpCmd represents the dump command
func newDumpCmd() *cobra.Command {
dumpCmd := &cobra.Command{
Use: "dump",
Short: "Export Kong configuration to a file",
Long: `The dump command reads all entities present in Kong
and writes them to a local file.
func executeDump(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()

The file can then be read using the sync command or diff command to
configure Kong.`,
Args: validateNoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

if yes, err := utils.ConfirmFileOverwrite(dumpCmdKongStateFile, dumpCmdStateFormat, assumeYes); err != nil {
return err
} else if !yes {
return nil
}
if yes, err := utils.ConfirmFileOverwrite(dumpCmdKongStateFile, dumpCmdStateFormat, assumeYes); err != nil {
return err
} else if !yes {
return nil
}

if inKonnectMode(nil) {
_ = sendAnalytics("dump", "", modeKonnect)
return dumpKonnectV2(ctx)
}
if inKonnectMode(nil) {
_ = sendAnalytics("dump", "", modeKonnect)
return dumpKonnectV2(ctx)
}

wsClient, err := utils.GetKongClient(rootConfig)
if err != nil {
return err
}
wsClient, err := utils.GetKongClient(rootConfig)
if err != nil {
return err
}

format := file.Format(strings.ToUpper(dumpCmdStateFormat))
format := file.Format(strings.ToUpper(dumpCmdStateFormat))

kongVersion, err := fetchKongVersion(ctx, rootConfig.ForWorkspace(dumpWorkspace))
kongVersion, err := fetchKongVersion(ctx, rootConfig.ForWorkspace(dumpWorkspace))
if err != nil {
return fmt.Errorf("reading Kong version: %w", err)
}
_ = sendAnalytics("dump", kongVersion, modeKong)

// Kong Enterprise dump all workspace
if dumpAllWorkspaces {
if dumpWorkspace != "" {
return fmt.Errorf("workspace cannot be specified with --all-workspace flag")
}
if dumpCmdKongStateFile != "kong" {
return fmt.Errorf("output-file cannot be specified with --all-workspace flag")
}
workspaces, err := listWorkspaces(ctx, wsClient)
if err != nil {
return err
}

for _, workspace := range workspaces {
wsClient, err := utils.GetKongClient(rootConfig.ForWorkspace(workspace))
if err != nil {
return fmt.Errorf("reading Kong version: %w", err)
}
_ = sendAnalytics("dump", kongVersion, modeKong)

// Kong Enterprise dump all workspace
if dumpAllWorkspaces {
if dumpWorkspace != "" {
return fmt.Errorf("workspace cannot be specified with --all-workspace flag")
}
if dumpCmdKongStateFile != "kong" {
return fmt.Errorf("output-file cannot be specified with --all-workspace flag")
}
workspaces, err := listWorkspaces(ctx, wsClient)
if err != nil {
return err
}

for _, workspace := range workspaces {
wsClient, err := utils.GetKongClient(rootConfig.ForWorkspace(workspace))
if err != nil {
return err
}

rawState, err := dump.Get(ctx, wsClient, dumpConfig)
if err != nil {
return fmt.Errorf("reading configuration from Kong: %w", err)
}
ks, err := state.Get(rawState)
if err != nil {
return fmt.Errorf("building state: %w", err)
}

if err := file.KongStateToFile(ks, file.WriteConfig{
SelectTags: dumpConfig.SelectorTags,
Workspace: workspace,
Filename: workspace,
FileFormat: format,
WithID: dumpWithID,
KongVersion: kongVersion,
}); err != nil {
return err
}
}
return nil
}

// Kong OSS
// or Kong Enterprise single workspace
if dumpWorkspace != "" {
wsConfig := rootConfig.ForWorkspace(dumpWorkspace)
exists, err := workspaceExists(ctx, rootConfig, dumpWorkspace)
if err != nil {
return err
}
if !exists {
return fmt.Errorf("workspace '%v' does not exist in Kong", dumpWorkspace)
}
wsClient, err = utils.GetKongClient(wsConfig)
if err != nil {
return err
}
return err
}

rawState, err := dump.Get(ctx, wsClient, dumpConfig)
Expand All @@ -139,15 +89,79 @@ configure Kong.`,
if err != nil {
return fmt.Errorf("building state: %w", err)
}
return file.KongStateToFile(ks, file.WriteConfig{

if err := file.KongStateToFile(ks, file.WriteConfig{
SelectTags: dumpConfig.SelectorTags,
Workspace: dumpWorkspace,
Filename: dumpCmdKongStateFile,
Workspace: workspace,
Filename: workspace,
FileFormat: format,
WithID: dumpWithID,
KongVersion: kongVersion,
})
},
}); err != nil {
return err
}
}
return nil
}

// Kong OSS
// or Kong Enterprise single workspace
if dumpWorkspace != "" {
wsConfig := rootConfig.ForWorkspace(dumpWorkspace)
exists, err := workspaceExists(ctx, rootConfig, dumpWorkspace)
if err != nil {
return err
}
if !exists {
return fmt.Errorf("workspace '%v' does not exist in Kong", dumpWorkspace)
}
wsClient, err = utils.GetKongClient(wsConfig)
if err != nil {
return err
}
}

rawState, err := dump.Get(ctx, wsClient, dumpConfig)
if err != nil {
return fmt.Errorf("reading configuration from Kong: %w", err)
}
ks, err := state.Get(rawState)
if err != nil {
return fmt.Errorf("building state: %w", err)
}
return file.KongStateToFile(ks, file.WriteConfig{
SelectTags: dumpConfig.SelectorTags,
Workspace: dumpWorkspace,
Filename: dumpCmdKongStateFile,
FileFormat: format,
WithID: dumpWithID,
KongVersion: kongVersion,
})
}

// newDumpCmd represents the dump command
func newDumpCmd(deprecated bool) *cobra.Command {
short := "Export Kong configuration to a file"
execute := executeDump
if deprecated {
short = "[deprecated] use 'kong dump' instead"
execute = func(cmd *cobra.Command, args []string) error {
cprint.UpdatePrintf("Warning: 'deck dump' is DEPRECATED and will be removed in a future version. " +
"Use 'deck kong dump' instead.\n")
return executeDump(cmd, args)
}
}

dumpCmd := &cobra.Command{
Use: "dump",
Short: short,
Long: `The dump command reads all entities present in Kong
and writes them to a local file.
The file can then be read using the sync command or diff command to
configure Kong.`,
Args: validateNoArgs,
RunE: execute,
}

dumpCmd.Flags().StringVarP(&dumpCmdKongStateFile, "output-file", "o",
Expand Down
10 changes: 5 additions & 5 deletions cmd/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import (
//
//

func newAddFileCmd() *cobra.Command {
addFileCmd := &cobra.Command{
func newFileSubCmd() *cobra.Command {
fileCmd := &cobra.Command{
Use: "file [sub-command]...",
Short: "Sub-command to host the decK file manipulation operations",
Long: `Sub-command to host the decK file manipulation operations`,
Short: "Sub-command to host the decK file operations",
Long: `Sub-command to host the decK file operations`,
}

return addFileCmd
return fileCmd
}
24 changes: 24 additions & 0 deletions cmd/kong.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"github.com/spf13/cobra"
)

//
//
// Define the CLI data for the kong sub-command
//
//

func newKongSubCmd() *cobra.Command {
kongSubCmd := &cobra.Command{
Use: "kong [sub-command]...",
Short: "Sub-command to host the decK network operations",
Long: `Sub-command to host the decK network operations`,
}

return kongSubCmd
}
6 changes: 3 additions & 3 deletions cmd/konnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (

var konnectAlphaState = `
WARNING: This command is currently in alpha state. This command
might have breaking changes in future releases.`
WARNING: This command was in alpha state and has been deprecated.
This command will be removed in a future release.`

// newKonnectCmd represents the konnect command
func newKonnectCmd() *cobra.Command {
konnectCmd := &cobra.Command{
Use: "konnect",
Short: "Configuration tool for Konnect (in alpha)",
Short: "[deprecated] Configuration tool for Konnect",
Long: `The konnect command prints subcommands that can be used to
configure Konnect.` + konnectAlphaState,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
Expand Down
Loading

0 comments on commit 4c87068

Please sign in to comment.