File: reset.go

package info (click to toggle)
deck 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,100 kB
  • sloc: makefile: 19; sh: 3
file content (138 lines) | stat: -rw-r--r-- 3,515 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package cmd

import (
	"fmt"
	"strings"

	"github.com/kong/deck/dump"
	"github.com/kong/deck/reset"
	"github.com/kong/deck/utils"
	"github.com/pkg/errors"
	"github.com/spf13/cobra"
)

var (
	resetCmdForce      bool
	resetWorkspace     string
	resetAllWorkspaces bool
)

// resetCmd represents the reset command
var resetCmd = &cobra.Command{
	Use:   "reset",
	Short: "Reset deletes all entities in Kong",
	Long: `Reset command will delete all entities in Kong's database.string

Use this command with extreme care as it is equivalent to running
"kong migrations reset" on your Kong instance.

By default, this command will ask for a confirmation prompt.`,
	Args: validateNoArgs,
	RunE: func(cmd *cobra.Command, args []string) error {
		if !resetCmdForce {
			ok, err := confirm()
			if err != nil {
				return err
			}
			if !ok {
				return nil
			}
		}

		rootClient, err := utils.GetKongClient(rootConfig)
		if err != nil {
			return err
		}
		// Kong OSS or default workspace
		if !resetAllWorkspaces && resetWorkspace == "" {
			state, err := dump.Get(rootClient, dumpConfig)
			if err != nil {
				return err
			}
			err = reset.Reset(state, rootClient)
			if err != nil {
				return err
			}
			return nil
		}

		if resetAllWorkspaces && resetWorkspace != "" {
			return errors.New("workspace cannot be specified with --all-workspace flag")
		}

		// Kong Enterprise
		var workspaces []string
		if resetAllWorkspaces {
			workspaces, err = listWorkspaces(rootClient, rootConfig.Address)
			if err != nil {
				return err
			}
		}
		if resetWorkspace != "" {
			exists, err := workspaceExists(rootConfig.ForWorkspace(resetWorkspace))
			if err != nil {
				return err
			}
			if !exists {
				return errors.Errorf("workspace '%v' does not exist in Kong", resetWorkspace)
			}

			workspaces = append(workspaces, resetWorkspace)
		}

		for _, workspace := range workspaces {
			wsClient, err := utils.GetKongClient(rootConfig.ForWorkspace(workspace))
			if err != nil {
				return err
			}
			state, err := dump.Get(wsClient, dumpConfig)
			if err != nil {
				return err
			}
			err = reset.Reset(state, wsClient)
			if err != nil {
				return err
			}
		}
		return nil
	},
}

// confirm prompts a user for a confirmation
// and returns true with no error if input is "yes" or "y" (case-insensitive),
// otherwise false.
func confirm() (bool, error) {
	fmt.Println("This will delete all configuration from Kong's database.")
	fmt.Print("> Are you sure? ")
	yes := []string{"yes", "y"}
	var input string
	_, err := fmt.Scanln(&input)
	if err != nil {
		return false, err
	}
	input = strings.ToLower(input)
	for _, valid := range yes {
		if input == valid {
			return true, nil
		}
	}
	return false, nil
}

func init() {
	rootCmd.AddCommand(resetCmd)
	resetCmd.Flags().BoolVarP(&resetCmdForce, "force", "f",
		false, "Skip interactive confirmation prompt before reset")
	resetCmd.Flags().BoolVar(&dumpConfig.SkipConsumers, "skip-consumers",
		false, "do not reset consumers or "+
			"any plugins associated with consumers")
	resetCmd.Flags().StringVarP(&resetWorkspace, "workspace", "w",
		"", "reset configuration of a specific workspace"+
			"(Kong Enterprise only).")
	resetCmd.Flags().BoolVar(&resetAllWorkspaces, "all-workspaces",
		false, "reset configuration of all workspaces (Kong Enterprise only).")
	resetCmd.Flags().StringSliceVar(&dumpConfig.SelectorTags,
		"select-tag", []string{},
		"only entities matching tags specified via this flag are deleted.\n"+
			"Multiple tags are ANDed together.")
}