-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.go
150 lines (122 loc) · 4.29 KB
/
cli.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package ssmenv
import (
"bufio"
"fmt"
"os"
"sort"
"strings"
"github.com/aws/aws-sdk-go/service/ssm"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
type CSV []string
func (c *CSV) Set(value string) error {
vals := strings.Split(value, ",")
for _, v := range vals {
*c = append(*c, strings.Trim(v, " "))
}
return nil
}
func (c *CSV) String() string {
return ""
}
func ParseCSV(s kingpin.Settings) (target *[]string) {
target = &[]string{}
s.SetValue((*CSV)(target))
return
}
func Run() error {
var app = kingpin.New("ssmenv", "Expand environment variables from AWS EC2 Systems Manager Parameter Store")
var names = ParseCSV(app.Flag("names", "Names of the parameters (comma separated)").PlaceHolder("PARAM_NAME,..."))
var paths = ParseCSV(app.Flag("paths", "The hierarchy for the parameter names (comma separated)").PlaceHolder("PARAM_PATH,..."))
var tags = ParseCSV(app.Flag("tags", "Filter by tags (comma separated)").PlaceHolder("KEY=VALUE,..."))
var types = ParseCSV(app.Flag("types", "The type of parameters (comma separated)").Default("String,SecureString"))
var multiValues = ParseCSV(app.Flag("multi-values", "Names or paths with multiple values (comma separated)").PlaceHolder("PARAM_NAME,..."))
// does not work
//var nonRecursive = app.Flag("non-recursive", "Describes one level paths").Bool()
var withoutExport = app.Flag("without-export", "Without export").Bool()
var hideExists = app.Flag("hide-exists", "Hide environment variables if it already exists").Bool()
var failExists = app.Flag("fail-exists", "Fail if environment variables alerady exists").Bool()
var retries = app.Flag("retries", "Number of times of retry").Default("3").Int()
var awsAccessKeyID = app.Flag("access-key", "The AWS access key ID").String()
var awsSecretAccessKey = app.Flag("secret-key", "The AWS secret access key").String()
var awsArn = app.Flag("assume-role-arn", "The AWS assume role ARN").String()
var awsToken = app.Flag("token", "The AWS access token").String()
var awsRegion = app.Flag("region", "The AWS region").String()
var awsProfile = app.Flag("profile", "The AWS CLI profile").String()
var awsConfig = app.Flag("aws-config", "The AWS CLI Config file").String()
var awsCreds = app.Flag("credentials", "The AWS CLI Credential file").String()
app.Version("0.1.1")
kingpin.MustParse(app.Parse(os.Args[1:]))
//recursive := !*nonRecursive
recursive := true
ssmenv, err := NewSSMEnv(*awsAccessKeyID, *awsSecretAccessKey, *awsArn, *awsToken, *awsRegion, *awsProfile, *awsConfig, *awsCreds, *retries)
if err != nil {
return err
}
envs := make(map[string]string)
nameFilters := make([]string, 0)
if len(*paths) > 0 {
metadata, err := ssmenv.DescribeParametersFilterByPaths(*paths, *tags, *types, recursive)
if err != nil {
return err
}
for _, m := range metadata {
nameFilters = append(nameFilters, *(m.Name))
}
}
if len(*names) > 0 {
metadata, err := ssmenv.DescribeParametersFilterByNames(*names, *tags, *types)
if err != nil {
return err
}
for _, m := range metadata {
nameFilters = append(nameFilters, *(m.Name))
}
}
var params *ssm.GetParametersOutput
params, err = ssmenv.GetParameters(nameFilters)
if err != nil {
return err
}
rawValues := make([]string, 0, len(*multiValues))
LABEL:
for _, p := range params.Parameters {
for _, name := range *multiValues {
if *(p.Name) == name {
rawValues = append(rawValues, *(p.Value))
continue LABEL
}
}
envName := strings.ToUpper(ssmenv.GetSplitedName(*(p.Name)))
if *failExists && ssmenv.EnvIsExists(envName) {
return fmt.Errorf("%s already exists", envName)
}
if *hideExists && ssmenv.EnvIsExists(envName) {
continue
}
envs[envName] = *(p.Value)
}
envNames := make([]string, 0)
for ename, _ := range envs {
envNames = append(envNames, ename)
}
for _, val := range rawValues {
scanner := bufio.NewScanner(strings.NewReader(val))
for scanner.Scan() {
if *withoutExport {
fmt.Println(scanner.Text())
} else {
fmt.Println(fmt.Sprintf("export %s", scanner.Text()))
}
}
}
sort.SliceStable(envNames, func(i, j int) bool { return envNames[i] < envNames[j] })
for _, ename := range envNames {
if *withoutExport {
fmt.Println(fmt.Sprintf(`%s="%s"`, ename, envs[ename]))
} else {
fmt.Println(fmt.Sprintf(`export %s="%s"`, ename, envs[ename]))
}
}
return nil
}