Skip to content

Commit c63bacb

Browse files
author
Josh Newman
committed
add recursive delete on rm command
1 parent fa51b7b commit c63bacb

File tree

1 file changed

+59
-7
lines changed

1 file changed

+59
-7
lines changed

cmd/rm.go

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,93 @@
11
package cmd
22

33
import (
4+
"bufio"
45
"errors"
56
"fmt"
7+
"os"
68

9+
"github.com/aws/aws-sdk-go/aws"
710
ssm "github.com/aws/aws-sdk-go/service/ssm"
811
"github.com/jedib0t/go-pretty/text"
912
"github.com/spf13/cobra"
1013
)
1114

1215
var rmCmd = &cobra.Command{
1316
Use: "rm <path>",
14-
Short: "Remove parameter by path",
17+
Short: "Remove parameter(s) by path",
1518
Args: func(cmd *cobra.Command, args []string) error {
1619
if len(args) == 0 {
1720
return errors.New("requires a path")
1821
}
1922

2023
return nil
2124
},
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)
2452

25-
client := ssm.New(session)
53+
fmt.Println(text.FgYellow.Sprint("The following parameters will be removed..."))
2654

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 {
2777
_, err := client.DeleteParameters(&ssm.DeleteParametersInput{
28-
Names: []*string{&path},
78+
Names: chunk,
2979
})
3080

3181
if err != nil {
3282
panic(err)
3383
}
84+
}
3485

35-
fmt.Println(text.FgGreen.Sprintf("Deleted parameter \"%s\"", path))
36-
},
86+
fmt.Println(text.FgGreen.Sprintf("Deleted %d parameters successfully", len(names)))
3787
}
3888

3989
func init() {
90+
rmCmd.Flags().Bool("recursive", false, "remove all children on path recursively")
91+
4092
rootCmd.AddCommand(rmCmd)
4193
}

0 commit comments

Comments
 (0)