-
Notifications
You must be signed in to change notification settings - Fork 5
/
format.go
109 lines (93 loc) · 2.59 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package secrets
import (
"encoding/json"
"fmt"
"os"
"regexp"
"strings"
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v3"
)
// Result holds the result of all the secrets pulled from Vault
type Result map[string]interface{}
// Add will add a new secret to the Result
func (result Result) Add(key string, value interface{}, prefix string, upperCase bool) {
result[ToUpperOrNotToUpper(fmt.Sprintf("%s%s", prefix, key), &upperCase)] = value
}
// ToJSON will format a map[string]interface{} to json
func (result Result) ToJSON() string {
log.Debug().Msg("Exporting as JSON")
jsonString, err := json.Marshal(result)
if err != nil {
log.Fatal().Err(err).Msg("Unable to convert result to json")
os.Exit(1)
}
return string(jsonString)
}
func (result Result) toKV(prefix string) string {
var resturnString string
for key, val := range result {
leKey := fixEnvName(key)
log.Info().Msgf("Exporting key: %s", leKey)
resturnString += fmt.Sprintf("%s%s=%s\n", prefix, leKey, getStringRepresentation(val))
}
return resturnString
}
func (result Result) toSecretKV() string {
var resturnString string
for key, val := range result {
log.Info().Msgf("Exporting key: %s", key)
resturnString += fmt.Sprintf("%s=%s\n", key, getStringRepresentation(val))
}
return resturnString
}
// ToENV will format a map[string]string to a env file
//
// export KEY='value'
func (result Result) ToENV() string {
log.Debug().Msg("Exporting as env values")
return result.toKV("export ")
}
// ToK8sSecret exports secrets as raw key values
func (result Result) ToK8sSecret() string {
log.Debug().Msg("Exporting as raw key values")
return result.toSecretKV()
}
// ToYaml exports secrets as yaml
func (result Result) ToYAML() string {
log.Debug().Msg("Exporting as YAML")
yamlString, err := yaml.Marshal(result)
if err != nil {
log.Fatal().Err(err).Msg("Unable to convert result to yaml")
os.Exit(1)
}
return string(yamlString)
}
// fixEnvName replaces all unsported env characters with "_"
func fixEnvName(currentName string) string {
reg, _ := regexp.Compile("[^a-zA-Z0-9_]+")
envVar := reg.ReplaceAllString(currentName, "_")
return envVar
}
func ToUpperOrNotToUpper(something string, currentUpper *bool) string {
if *currentUpper {
return strings.ToUpper(something)
}
return something
}
func getStringRepresentation(val interface{}) string {
switch val.(type) {
case string:
return fmt.Sprintf("'%s'", val)
case int:
return fmt.Sprintf("%d", val)
case float64:
return fmt.Sprintf("%f", val)
case bool:
return fmt.Sprintf("%t", val)
case nil:
return "null"
default:
return fmt.Sprintf("'%s'", val)
}
}