-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmain.go
169 lines (146 loc) · 3.7 KB
/
main.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"errors"
"io/ioutil"
"os"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
"strings"
)
var VersionString string
func init() {
log.SetLevel(log.InfoLevel)
}
func main() {
app := cli.NewApp()
app.Name = "env-aws-params"
app.Usage = "Application entry-point that injects SSM Parameter Store values as Environment Variables"
app.UsageText = "env-aws-params [global options] -p prefix command [command arguments]"
app.Version = VersionString
app.Flags = cliFlags()
app.Action = func(c *cli.Context) error {
return action(c)
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func action(c *cli.Context) error {
if c.GlobalBool("debug") {
log.SetLevel(log.DebugLevel)
}
if c.GlobalBool("silent") {
log.SetOutput(ioutil.Discard)
} else {
log.SetOutput(os.Stdout)
}
code, err := validateArgs(c)
if code > 0 {
return cli.NewExitError(errorPrefix(err), code)
}
params, err := getParameters(c)
if err != nil {
return cli.NewExitError(errorPrefix(err), -1)
}
envVars := BuildEnvVars(
params,
c.GlobalBool("sanitize"),
c.GlobalBool("strip"),
c.GlobalBool("upcase"))
for _, v := range envVars {
log.Debugf("Setting %s", v)
}
if c.GlobalBool("pristine") == false {
envVars = append(os.Environ(), envVars...)
}
err = RunCommand(c.Args()[0], c.Args()[1:], envVars)
if err != nil {
if cmdError, ok := err.(*CommandFailedError); ok {
return cli.NewExitError(errorPrefix(err), cmdError.ExitCode)
}
return cli.NewExitError(errorPrefix(err), 128)
}
return nil
}
func cliFlags() []cli.Flag {
return []cli.Flag{
cli.StringFlag{
Name: "aws-region",
Usage: "The AWS region to use for the Parameter Store API",
EnvVar: "AWS_REGION",
},
cli.StringFlag{
Name: "profile",
Usage: "Optional AWS profile to use for the Parameter Store API",
EnvVar: "AWS_PROFILE",
},
cli.StringSliceFlag{
Name: "prefix, p",
Usage: "Key prefix that is used to retrieve the environment variables - supports multiple use",
EnvVar: "PARAMS_PREFIX",
},
cli.BoolFlag{
Name: "pristine",
Usage: "Only use values retrieved from Parameter Store, do not inherit the existing environment variables",
EnvVar: "PARAMS_PRISTINE",
},
cli.BoolFlag{
Name: "sanitize",
Usage: "Replace invalid characters in keys to underscores",
EnvVar: "PARAMS_SANITIZE",
},
cli.BoolFlag{
Name: "strip",
Usage: "Strip invalid characters in keys",
EnvVar: "PARAMS_STRIP",
},
cli.BoolFlag{
Name: "upcase",
Usage: "Force keys to uppercase",
EnvVar: "PARAMS_UPCASE",
},
cli.BoolFlag{
Name: "debug",
Usage: "Log additional debugging information",
EnvVar: "PARAMS_DEBUG",
},
cli.BoolFlag{
Name: "silent",
Usage: "Silence all logs",
EnvVar: "PARAMS_SILENT",
},
}
}
func errorPrefix(err error) string {
return strings.Join([]string{"ERROR:", err.Error()}, " ")
}
func getParameters(c *cli.Context) (map[string]string, error) {
values := make(map[string]string)
client, err := NewSSMClient(c.GlobalString("aws-region"), c.GlobalString("profile"))
if err != nil {
return values, err
}
for _, path := range c.GlobalStringSlice("prefix") {
params, err := client.GetParametersByPath(path)
if err != nil {
return values, err
}
for k, v := range params {
values[k] = v
}
}
return values, nil
}
func validateArgs(c *cli.Context) (int, error) {
if len(c.GlobalStringSlice("prefix")) == 0 {
return 1, errors.New("prefix is required")
}
if c.NArg() == 0 {
return 2, errors.New("command not specified")
}
if c.GlobalBool("sanitize") == true && c.GlobalBool("strip") == true {
return 3, errors.New("--sanitize and --strip are mutually exclusive behaviors")
}
return 0, nil
}