-
Notifications
You must be signed in to change notification settings - Fork 2
/
format.go
62 lines (42 loc) · 1.1 KB
/
format.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
package envsec
import (
"fmt"
"io"
"strings"
"github.com/yawn/envmap"
)
// Formats exposes the Formatters known to envsec
var Formats = map[string]Formatter{
"cloudformation": cloudformation,
"shell": shell,
"terraform": terraform,
}
// Formatter defines the output function for a given slice of environment key/value tuples
type Formatter func(w io.Writer, result []string)
// cloudformation emits a formatting suitable for AWS CloudFormation stacks
func cloudformation(w io.Writer, result []string) {
var list []string
for k, v := range envmap.ToMap(result) {
list = append(list, fmt.Sprintf(`"%s": {
"Default": "%s",
"Type": "String"
}`, k, v))
}
fmt.Fprintln(w, strings.Join(list, ",\n"))
}
// shell emits a formatting suitable for shell exports
func shell(w io.Writer, result []string) {
for _, e := range result {
fmt.Fprintln(w, e)
}
}
// terraform emits a formatting suitable for Terraform
func terraform(w io.Writer, result []string) {
for k, v := range envmap.ToMap(result) {
fmt.Fprintf(w, `variable "%s" {
type = "string"
default = "%s"
}
`, k, v)
}
}