|
1 | 1 | package cmd |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bufio" |
4 | 5 | "errors" |
5 | 6 | "fmt" |
| 7 | + "os" |
6 | 8 |
|
| 9 | + "github.com/aws/aws-sdk-go/aws" |
7 | 10 | ssm "github.com/aws/aws-sdk-go/service/ssm" |
8 | 11 | "github.com/jedib0t/go-pretty/text" |
9 | 12 | "github.com/spf13/cobra" |
10 | 13 | ) |
11 | 14 |
|
12 | 15 | var rmCmd = &cobra.Command{ |
13 | 16 | Use: "rm <path>", |
14 | | - Short: "Remove parameter by path", |
| 17 | + Short: "Remove parameter(s) by path", |
15 | 18 | Args: func(cmd *cobra.Command, args []string) error { |
16 | 19 | if len(args) == 0 { |
17 | 20 | return errors.New("requires a path") |
18 | 21 | } |
19 | 22 |
|
20 | 23 | return nil |
21 | 24 | }, |
22 | | - Run: func(cmd *cobra.Command, args []string) { |
23 | | - path := args[0] |
| 25 | + Run: runRmCmd, |
| 26 | +} |
| 27 | + |
| 28 | +func runRmCmd(cmd *cobra.Command, args []string) { |
| 29 | + path := args[0] |
| 30 | + recursive, _ := cmd.Flags().GetBool("recursive") |
| 31 | + |
| 32 | + client := ssm.New(session) |
| 33 | + |
| 34 | + var names []*string |
| 35 | + |
| 36 | + if recursive { |
| 37 | + opts := &getParamsOptions{ |
| 38 | + Client: client, |
| 39 | + Path: &path, |
| 40 | + Recursive: aws.Bool(true), |
| 41 | + Decrypt: aws.Bool(false), |
| 42 | + } |
| 43 | + |
| 44 | + params := getParams(opts, []*ssm.Parameter{}, nil) |
| 45 | + |
| 46 | + if len(params) == 0 { |
| 47 | + fmt.Println("No parameters to delete at the specified path.") |
| 48 | + os.Exit(0) |
| 49 | + } |
| 50 | + |
| 51 | + sortParams(params) |
24 | 52 |
|
25 | | - client := ssm.New(session) |
| 53 | + fmt.Println(text.FgYellow.Sprint("The following parameters will be removed...")) |
26 | 54 |
|
| 55 | + for _, param := range params { |
| 56 | + names = append(names, param.Name) |
| 57 | + |
| 58 | + fmt.Println(*param.Name) |
| 59 | + } |
| 60 | + |
| 61 | + fmt.Println(text.FgYellow.Sprint("Enter \"yes\" to delete the parameters above. Any other input will cancel the removal.")) |
| 62 | + |
| 63 | + input := bufio.NewScanner(os.Stdin) |
| 64 | + input.Scan() |
| 65 | + |
| 66 | + if input.Text() != "yes" { |
| 67 | + fmt.Println("Canceled the recursive delete!") |
| 68 | + os.Exit(0) |
| 69 | + } |
| 70 | + } else { |
| 71 | + names = append(names, &path) |
| 72 | + } |
| 73 | + |
| 74 | + chunks := getStringChunks(names, 10) |
| 75 | + |
| 76 | + for _, chunk := range chunks { |
27 | 77 | _, err := client.DeleteParameters(&ssm.DeleteParametersInput{ |
28 | | - Names: []*string{&path}, |
| 78 | + Names: chunk, |
29 | 79 | }) |
30 | 80 |
|
31 | 81 | if err != nil { |
32 | 82 | panic(err) |
33 | 83 | } |
| 84 | + } |
34 | 85 |
|
35 | | - fmt.Println(text.FgGreen.Sprintf("Deleted parameter \"%s\"", path)) |
36 | | - }, |
| 86 | + fmt.Println(text.FgGreen.Sprintf("Deleted %d parameters successfully", len(names))) |
37 | 87 | } |
38 | 88 |
|
39 | 89 | func init() { |
| 90 | + rmCmd.Flags().Bool("recursive", false, "remove all children on path recursively") |
| 91 | + |
40 | 92 | rootCmd.AddCommand(rmCmd) |
41 | 93 | } |
0 commit comments