1
1
package main
2
2
3
3
import (
4
+ "flag"
4
5
"fmt"
5
6
"github.com/aws/aws-sdk-go/aws"
6
7
"github.com/aws/aws-sdk-go/aws/session"
7
8
"github.com/aws/aws-sdk-go/service/ssm"
8
9
"log"
9
10
"os"
10
11
"strings"
11
- "flag"
12
+ )
13
+
14
+ const (
15
+ formatExports = "exports"
16
+ formatDotenv = "dotenv"
12
17
)
13
18
14
19
func main () {
@@ -17,13 +22,19 @@ func main() {
17
22
return
18
23
}
19
24
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
+ }
22
33
23
34
sess := CreateSession ()
24
35
client := CreateClient (sess )
25
36
26
- ExportVariables (client , os .Getenv ("AWS_ENV_PATH" ), * recursivePtr , "" )
37
+ ExportVariables (client , os .Getenv ("AWS_ENV_PATH" ), * recursivePtr , * format , "" )
27
38
}
28
39
29
40
func CreateSession () * session.Session {
@@ -34,12 +45,12 @@ func CreateClient(sess *session.Session) *ssm.SSM {
34
45
return ssm .New (sess )
35
46
}
36
47
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
+ }
43
54
44
55
if nextToken != "" {
45
56
input .SetNextToken (nextToken )
@@ -52,20 +63,25 @@ func ExportVariables(client *ssm.SSM, path string, recursive bool, nextToken str
52
63
}
53
64
54
65
for _ , element := range output .Parameters {
55
- PrintExportParameter (path , element )
66
+ OutputParameter (path , element , format )
56
67
}
57
68
58
69
if output .NextToken != nil {
59
- ExportVariables (client , path , recursive , * output .NextToken )
70
+ ExportVariables (client , path , recursive , format , * output .NextToken )
60
71
}
61
72
}
62
73
63
- func PrintExportParameter (path string , parameter * ssm.Parameter ) {
74
+ func OutputParameter (path string , parameter * ssm.Parameter , format string ) {
64
75
name := * parameter .Name
65
76
value := * parameter .Value
66
77
67
78
env := strings .Replace (strings .Trim (name [len (path ):], "/" ), "/" , "_" , - 1 )
68
79
value = strings .Replace (value , "\n " , "\\ n" , - 1 )
69
80
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
+ }
71
87
}
0 commit comments