-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
197 lines (165 loc) · 4.69 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/joeshaw/envdecode"
"github.com/spf13/cobra"
)
func init() {
cobra.OnInitialize(loadS3Object)
Root.PersistentFlags().BoolVarP(&outputShell, "shell", "s", true, "output config vars in shell format")
Root.PersistentFlags().BoolVarP(&outputJSON, "json", "", false, "output config vars in JSON format")
Root.AddCommand(configCmd)
Root.AddCommand(configGetCmd)
Root.AddCommand(configSetCmd)
Root.AddCommand(configUnsetCmd)
Root.AddCommand(runCmd)
}
func main() {
if err := Root.Execute(); err != nil {
os.Exit(1)
}
}
var (
outputShell bool
outputJSON bool
s3vars = make(map[string]string)
cfg config
client s3iface.S3API
)
// Root represents the base command when called without any subcommands
var Root = &cobra.Command{
Use: "s3env <command> [FLAGS]",
Short: "s3env manages config vars and stores them on an s3 object.",
Long: `s3env wraps an existing command and sets ENV vars for them.
PREREQUISITES
The following ENV vars are required to use s3env:
S3ENV_KEY (defaults to env.json)
S3ENV_BUCKET
S3ENV_AWS_ACCESS_KEY_ID
S3ENV_AWS_REGION
S3ENV_AWS_SECRET_ACCESS_KEY
EXAMPLES
s3env config # show all config vars
s3env config:set FOO=1 BAR=2 # set two vars
s3env config:get FOO # display FOO
s3env config:unset FOO # remove FOO
s3env run hello-world # hello-world will get BAR=2 defined in its ENV
CONTEXT
One of the limitations of heroku config vars presently is the total
size you can configure on any given app (32kb). If you're managing
lots of TLS certificates, that limit quickly runs out.
`,
}
func displayUsage(cmd *cobra.Command) {
fmt.Fprintln(os.Stderr, "Usage: s3env "+cmd.Use)
os.Exit(1)
}
func displayErr(err error) {
fmt.Fprintln(os.Stderr, "s3env: "+err.Error())
os.Exit(1)
}
func loadS3Object() {
if err := envdecode.StrictDecode(&cfg); err != nil {
fmt.Printf("s3env: %s (continuing with empty config)\n", err)
return
}
sess, err := session.NewSession(&aws.Config{
Region: aws.String(cfg.Region),
Credentials: credentials.NewStaticCredentials(
cfg.AccessID,
cfg.SecretKey,
"",
),
})
if err != nil {
fmt.Printf("s3env: aws error: %s\n", err)
return
}
client = s3.New(sess)
in, err := input()
if err != nil {
fmt.Printf("s3env: read input error: %s\n", err)
return
}
defer in.Close()
if err = json.NewDecoder(in).Decode(&s3vars); err != nil {
fmt.Printf("s3env: decode input error: %s\n", err)
return
}
}
func displayVars(vars map[string]string) {
if outputJSON {
enc := json.NewEncoder(os.Stdout)
if err := enc.Encode(vars); err != nil {
panic(err)
}
return
}
for k, v := range vars {
if strings.Contains(v, "\n") {
fmt.Printf("%s='%s'\n", k, v)
} else {
fmt.Printf("%s=%s\n", k, v)
}
}
}
func parseEnvironStrings(environ []string) (map[string]string, error) {
vars := make(map[string]string)
for _, v := range environ {
chunks := strings.SplitN(v, "=", 2)
if len(chunks) != 2 {
return nil, fmt.Errorf("unable to parse %q (expected format KEY=VAL)", v)
}
vars[chunks[0]] = chunks[1]
}
return vars, nil
}
func persistVars() error {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(s3vars); err != nil {
return fmt.Errorf("encode failed: %s", err)
}
_, err := client.PutObject(&s3.PutObjectInput{
Bucket: aws.String(cfg.Bucket),
Key: aws.String(cfg.Key),
Body: bytes.NewReader(buf.Bytes()),
ServerSideEncryption: aws.String(cfg.ServerSideEncryption),
})
if err != nil {
return fmt.Errorf("saving to s3 failed with error: %s", err)
}
return nil
}
// input gets the appropriate input source. If there was any data pumped into
// STDIN, we'll choose that. Otherwise we'll try to load the s3 object that
// was configured.
func input() (io.ReadCloser, error) {
out, err := client.GetObject(&s3.GetObjectInput{
Bucket: aws.String(cfg.Bucket),
Key: aws.String(cfg.Key),
})
if err != nil {
// Cast err to awserr.Error to handle specific error codes.
aerr, ok := err.(awserr.Error)
if ok && aerr.Code() == s3.ErrCodeNoSuchKey {
fmt.Fprintf(os.Stderr, "s3env: object not found. using empty config\n")
buf := new(bytes.Buffer)
buf.Write([]byte("{}"))
return ioutil.NopCloser(buf), nil
}
return nil, err
}
return out.Body, nil
}