Skip to content

Commit a4de45a

Browse files
committed
allows to set dotenv format
1 parent 18266f5 commit a4de45a

File tree

2 files changed

+53
-17
lines changed

2 files changed

+53
-17
lines changed

README.md

+23-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ $ export DB_USERNAME=$'Username'
3333
$ export DB_PASSWORD=$'SecretPassword'
3434
```
3535

36-
You can also pass the `--recursive` flag. When specified, aws-env will recursively fetch parameters starting from the base path specified in
36+
### Optional Flags
37+
38+
*--recursive*
39+
You can pass the `--recursive` flag. When specified, aws-env will recursively fetch parameters starting from the base path specified in
3740
`AWS_ENV_PATH`. For the exported environment variables, any `/` characters from sub-paths will be converted to `_` characters. For example:
3841

3942
With the following parameters:
@@ -44,10 +47,27 @@ $ aws ssm put-parameter --name /prod/my-app/db1/DB_PASSWORD --value "OtherSecret
4447

4548
`eval $(AWS_ENV_PATH=/prod/my-app/ AWS_REGION=us-west-2 ./aws-env --recursive)` will output:
4649
```
47-
$ export db0_DB_PASSWORD=$'SecretPassword'
48-
$ export db1_DB_PASSWORD=$'OtherSecretPassword'
50+
export db0_DB_PASSWORD=$'SecretPassword'
51+
export db1_DB_PASSWORD=$'OtherSecretPassword'
4952
```
5053

54+
*--format*
55+
56+
Specify output format of parameters.
57+
58+
* exports (default) - export as environmental variables ready to be eval(...)
59+
* dotenv - used for generating dotenv files
60+
61+
`AWS_ENV_PATH=/prod/my-app/ AWS_REGION=us-west-2 ./aws-env --format=dotenv` will output:
62+
```
63+
FOO="bar"
64+
ACME="zaz"
65+
```
66+
67+
...which then can be easily used to create .env file:
68+
69+
`AWS_ENV_PATH=/prod/my-app/ AWS_REGION=us-west-2 ./aws-env --format=dotenv > .env`
70+
5171
## Example Dockerfile
5272

5373
```

aws-env.go

+30-14
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package main
22

33
import (
4+
"flag"
45
"fmt"
56
"github.com/aws/aws-sdk-go/aws"
67
"github.com/aws/aws-sdk-go/aws/session"
78
"github.com/aws/aws-sdk-go/service/ssm"
89
"log"
910
"os"
1011
"strings"
11-
"flag"
12+
)
13+
14+
const (
15+
formatExports = "exports"
16+
formatDotenv = "dotenv"
1217
)
1318

1419
func main() {
@@ -17,13 +22,19 @@ func main() {
1722
return
1823
}
1924

20-
recursivePtr := flag.Bool("recursive", false, "recursively process parameters on path")
21-
flag.Parse()
25+
recursivePtr := flag.Bool("recursive", false, "recursively process parameters on path")
26+
format := flag.String("format", formatExports, "output format")
27+
flag.Parse()
28+
29+
if *format == formatExports || *format == formatDotenv {
30+
} else {
31+
log.Fatal("Unsupported format option. Must be 'exports' or 'dotenv'")
32+
}
2233

2334
sess := CreateSession()
2435
client := CreateClient(sess)
2536

26-
ExportVariables(client, os.Getenv("AWS_ENV_PATH"), *recursivePtr, "")
37+
ExportVariables(client, os.Getenv("AWS_ENV_PATH"), *recursivePtr, *format, "")
2738
}
2839

2940
func CreateSession() *session.Session {
@@ -34,12 +45,12 @@ func CreateClient(sess *session.Session) *ssm.SSM {
3445
return ssm.New(sess)
3546
}
3647

37-
func ExportVariables(client *ssm.SSM, path string, recursive bool, nextToken string) {
38-
input := &ssm.GetParametersByPathInput{
39-
Path: &path,
40-
WithDecryption: aws.Bool(true),
41-
Recursive: aws.Bool(recursive),
42-
}
48+
func ExportVariables(client *ssm.SSM, path string, recursive bool, format string, nextToken string) {
49+
input := &ssm.GetParametersByPathInput{
50+
Path: &path,
51+
WithDecryption: aws.Bool(true),
52+
Recursive: aws.Bool(recursive),
53+
}
4354

4455
if nextToken != "" {
4556
input.SetNextToken(nextToken)
@@ -52,20 +63,25 @@ func ExportVariables(client *ssm.SSM, path string, recursive bool, nextToken str
5263
}
5364

5465
for _, element := range output.Parameters {
55-
PrintExportParameter(path, element)
66+
OutputParameter(path, element, format)
5667
}
5768

5869
if output.NextToken != nil {
59-
ExportVariables(client, path, recursive, *output.NextToken)
70+
ExportVariables(client, path, recursive, format, *output.NextToken)
6071
}
6172
}
6273

63-
func PrintExportParameter(path string, parameter *ssm.Parameter) {
74+
func OutputParameter(path string, parameter *ssm.Parameter, format string) {
6475
name := *parameter.Name
6576
value := *parameter.Value
6677

6778
env := strings.Replace(strings.Trim(name[len(path):], "/"), "/", "_", -1)
6879
value = strings.Replace(value, "\n", "\\n", -1)
6980

70-
fmt.Printf("export %s=$'%s'\n", env, value)
81+
switch format {
82+
case formatExports:
83+
fmt.Printf("export %s=$'%s'\n", env, value)
84+
case formatDotenv:
85+
fmt.Printf("%s=\"%s\"\n", env, value)
86+
}
7187
}

0 commit comments

Comments
 (0)