-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcommands.go
118 lines (104 loc) · 3.25 KB
/
commands.go
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
package commands
import (
"encoding/json"
"strings"
"github.com/abiosoft/ishell"
"github.com/bwhaley/ssmsh/config"
"github.com/bwhaley/ssmsh/parameterstore"
)
type fn func(*ishell.Context)
var (
shell *ishell.Shell
ps *parameterstore.ParameterStore
cfg *config.Config
)
// Init initializes the ssmsh subcommands
func Init(iShell *ishell.Shell, iPs *parameterstore.ParameterStore, iCfg *config.Config) {
shell = iShell
ps = iPs
cfg = iCfg
registerCommand("cd", "change your relative location within the parameter store", cd, cdUsage)
registerCommand("cp", "copy source to dest", cp, cpUsage)
registerCommand("decrypt", "toggle parameter decryption", decrypt, decryptUsage)
registerCommand("get", "get parameters", get, getUsage)
registerCommand("history", "get parameter history", history, historyUsage)
registerCommand("key", "set the KMS key", key, keyUsage)
registerCommand("ls", "list parameters", ls, lsUsage)
registerCommand("mv", "move parameters", mv, mvUsage)
registerCommand("policy", "create named parameter policy", policy, policyUsage)
registerCommand("profile", "switch to a different AWS IAM profile", profile, profileUsage)
registerCommand("put", "set parameter", put, putUsage)
registerCommand("region", "change region", region, regionUsage)
registerCommand("rm", "remove parameters", rm, rmUsage)
setPrompt(parameterstore.Delimiter)
}
// registerCommand adds a command to the shell
func registerCommand(name string, helpText string, f fn, usageText string) {
shell.AddCmd(&ishell.Cmd{
Name: name,
Help: helpText,
LongHelp: usageText,
Func: f,
})
}
// setPrompt configures the shell prompt
func setPrompt(prompt string) {
shell.SetPrompt(prompt + ">")
}
// remove deletes an element from a slice of strings
func remove(slice []string, i int) []string {
return append(slice[:i], slice[i+1:]...)
}
// checkRecursion searches a slice of strings for an element matching -r or -R
func checkRecursion(paths []string) ([]string, bool) {
for i, p := range paths {
if strings.EqualFold(p, "-r") {
paths = remove(paths, i)
return paths, true
}
}
return paths, false
}
// parsePath determines whether a path includes a region
func parsePath(path string) (parameterPath parameterstore.ParameterPath) {
pathParts := strings.Split(path, ":")
switch len(pathParts) {
case 1:
parameterPath.Name = pathParts[0]
parameterPath.Region = ps.Region
case 2:
parameterPath.Region = pathParts[0]
parameterPath.Name = pathParts[1]
}
ps.InitClient(parameterPath.Region)
return parameterPath
}
func groupByRegion(params []parameterstore.ParameterPath) map[string][]string {
paramsByRegion := make(map[string][]string)
for _, p := range params {
paramsByRegion[p.Region] = append(paramsByRegion[p.Region], p.Name)
}
return paramsByRegion
}
func trim(with []string) (without []string) {
for i := range with {
without = append(without, strings.TrimSpace(with[i]))
}
return without
}
func printResult(result interface{}) {
switch cfg.Default.Output {
case "json":
printJSON(result)
default:
shell.Printf("%+v\n", result)
}
}
func printJSON(result interface{}) {
resultJSON, err := json.MarshalIndent(result, "", " ")
if err != nil {
shell.Println("Error with result: ", err)
} else {
shell.Println(string(resultJSON))
}
}