/
util.go
54 lines (49 loc) · 1.44 KB
/
util.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
package util
import (
"fmt"
"golang.org/x/exp/maps"
"regexp"
"sort"
"strings"
)
type ParameterStorePath struct {
components []string
}
func (p ParameterStorePath) String() string {
output := "/" + strings.Join(p.components, "/") + "/"
return output
}
func NewParameterStorePath(input string) *ParameterStorePath {
split := strings.Split(input, "/")
var filtered []string
for _, str := range split {
if str != "" {
filtered = append(filtered, str)
}
}
return &ParameterStorePath{filtered}
}
// ParametersToSlice accepts a map of string key/value pairs and returns an array of strings.
// The elements of the returned array are keys and values conjoined by an `=` sign.
// Keys that do not start with a letter or underscore will result in an error.
// All values are enclosed in single quotes.
// Values that contain single quote characters will first be converted to double quotes.
func ParametersToSlice(parameters map[string]string) ([]string, error) {
lines := make([]string, 0)
keys := maps.Keys(parameters)
sort.Strings(keys)
//envvar names must begin with a letter or underscore
invalidKey := regexp.MustCompile(`^[^a-zA-Z_]`)
for _, key := range keys {
if invalidKey.MatchString(key) {
return nil, fmt.Errorf("Key %s invalid", key)
}
v := parameters[key]
if strings.Contains(v, "'") {
v = strings.ReplaceAll(v, "'", "\"")
}
line := fmt.Sprintf("%s='%s'", key, v)
lines = append(lines, line)
}
return lines, nil
}