Skip to content

Commit 136f8df

Browse files
committed
Adding format options for env
1 parent 57b3f81 commit 136f8df

4 files changed

Lines changed: 40 additions & 11 deletions

File tree

build.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
# Set version
44
VERSION_MAJOR=0
5-
VERSION_MINOR=3
6-
VERSION_PATCH=2
5+
VERSION_MINOR=4
6+
VERSION_PATCH=0
77
VERSION_SPECIAL=
88
VERSION=""
99

main.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ func main() {
2323
flagPath := flag.String("path", "", "Parameter Store path.")
2424
flagBase64 := flag.Bool("base64", false, "Base64 encode collected values.")
2525
flagFormat := flag.String("format", "line", fmt.Sprintf("Format for output. Supported values: %s.", strings.Join(parameterstore.ValidFormats(), ",")))
26+
flagUpperCase := flag.Bool("upper-case", false, "Upper case the path. Only works with format 'env'.")
27+
flagPrefix := flag.String("prefix", "", "Prefix the keys with this value. Only works with format 'env'.")
2628
flagRecursive := flag.Bool("recursive", false, "Look up all keys in branch.")
2729
flagDecrypt := flag.Bool("decrypt", false, "Request decrypted keys.")
2830
flagAccessKey := flag.String("access-key", "", "Access key for AWS API.")
@@ -34,6 +36,7 @@ func main() {
3436
flagFileOutput := flag.String("f", "", "Output to specified file.")
3537
flagHelp := flag.Bool("h", false, "Help menu.")
3638
flagVersion := flag.Bool("v", false, "Show application Version.")
39+
3740
flag.Parse()
3841

3942
if *flagVersion {
@@ -81,12 +84,16 @@ func main() {
8184
var output string
8285

8386
if *flagRecursive {
84-
psMap, err := ps.CollectPath()
87+
psMap, err := ps.CollectPath(*flagUpperCase)
8588
if err != nil {
8689
log.Fatalf("Failed to read from parameter store. Error: %s", err)
8790
}
88-
89-
formattedOutput, err := ps.FormatOutput(psMap, *flagFormat)
91+
formatOptions := parameterstore.FormatOptions{
92+
Format: *flagFormat,
93+
Prefix: *flagPrefix,
94+
UpperCase: *flagUpperCase,
95+
}
96+
formattedOutput, err := ps.FormatOutput(psMap, formatOptions)
9097
if err != nil {
9198
log.Fatal(err)
9299
}

parameterStore/formatting.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@ import (
66
)
77

88
var (
9-
formats = []string{"line", "json", "pretty-json", "yaml", "env"}
9+
formats = []string{"line", "json", "pretty-json", "yaml", "env"}
10+
globalformatOptions = FormatOptions{}
1011
)
1112

13+
type FormatOptions struct {
14+
Format string
15+
Prefix string
16+
UpperCase bool
17+
}
18+
1219
func ValidFormats() []string {
1320
return formats
1421
}
@@ -25,6 +32,20 @@ func FormatValidation(formatString string) bool {
2532
return valid
2633
}
2734

35+
func upperIfNeeded(s string) string {
36+
if globalformatOptions.UpperCase {
37+
return strings.ToUpper(s)
38+
}
39+
return s
40+
}
41+
42+
func prefixIfNeeded(s string) string {
43+
if globalformatOptions.Prefix != "" {
44+
return fmt.Sprintf("%s%s", globalformatOptions.Prefix, s)
45+
}
46+
return s
47+
}
48+
2849
func lineFormat(data map[string]string) []byte {
2950
output := []string{}
3051
for key, value := range data {
@@ -39,7 +60,7 @@ func envFormat(data map[string]string) []byte {
3960

4061
for key, value := range data {
4162
brokenKeys := strings.Split(key, "/")
42-
out = append(out, fmt.Sprintf("%s=%s", brokenKeys[len(brokenKeys)-1], value))
63+
out = append(out, fmt.Sprintf("%s=%s", upperIfNeeded(prefixIfNeeded(brokenKeys[len(brokenKeys)-1])), value))
4364
}
4465

4566
return []byte(strings.Join(out, "\n"))

parameterStore/parameterstore.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func New(session *session.Session, path string, recursive, decrypt, includePath,
3939
return ps
4040
}
4141

42-
func (ps *ParameterStore) CollectPath() (map[string]string, error) {
42+
func (ps *ParameterStore) CollectPath(upperCase bool) (map[string]string, error) {
4343
inputObjects := ps.pathInput("")
4444
values, err := ps.values(inputObjects)
4545
if err != nil {
@@ -145,8 +145,9 @@ func (ps *ParameterStore) b64(data string) string {
145145
return base64.StdEncoding.EncodeToString([]byte(data))
146146
}
147147

148-
func (ps ParameterStore) FormatOutput(data map[string]string, format string) ([]byte, error) {
149-
switch format {
148+
func (ps ParameterStore) FormatOutput(data map[string]string, formatOptions FormatOptions) ([]byte, error) {
149+
globalformatOptions = formatOptions
150+
switch globalformatOptions.Format {
150151
case "json":
151152
return json.Marshal(convertTree(data))
152153
case "pretty-json":
@@ -158,6 +159,6 @@ func (ps ParameterStore) FormatOutput(data map[string]string, format string) ([]
158159
case "env":
159160
return envFormat(data), nil
160161
default:
161-
return []byte{}, fmt.Errorf("output format '%s' is not valid", format)
162+
return []byte{}, fmt.Errorf("output format '%s' is not valid", globalformatOptions.Format)
162163
}
163164
}

0 commit comments

Comments
 (0)