-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
160 lines (145 loc) · 3.6 KB
/
helpers.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package parameters
import (
"encoding/json"
"flag"
"fmt"
"os"
"strconv"
"strings"
"github.com/barchart/common-go/pkg/configuration/database"
"github.com/barchart/common-go/pkg/parameters/flags"
)
// convertStrings converts a string to a typeValue and returns it
func convertString(str string, typeValue string) interface{} {
switch typeValue {
case boolType:
{
value, _ := strconv.ParseBool(str)
return value
}
case float64Type:
{
value, _ := strconv.ParseFloat(str, 64)
return value
}
case intType:
{
value, _ := strconv.ParseInt(str, 10, 32)
return int(value)
}
case int64Type:
{
value, _ := strconv.ParseInt(str, 10, 64)
return value
}
case stringType:
{
return str
}
case uintType:
{
value, _ := strconv.ParseUint(str, 10, 32)
return uint(value)
}
case uint64Type:
{
value, _ := strconv.ParseUint(str, 10, 64)
return value
}
case databaseType:
{
value := database.Database{}
_ = json.Unmarshal([]byte(str), &value)
return value
}
}
return str
}
// getAWSSecretsRegion gets AwsRegionSecrets value from a AWS-REGION-SECRETS flag or env variable and returns it
func getAWSSecretsRegion() string {
flg := flag.Lookup(AwsRegionSecrets)
region := flg.Value.String()
if !flg.Value.(*flags.StringValue).IsSet() {
region = os.Getenv(AwsRegionSecrets)
if region == "" {
region = flg.DefValue
}
}
return region
}
// getValueFromAWSSecretsManager returns a Parameter value from the AWS Secrets Manager
func getValueFromAWSSecretsManager(param Parameter) interface{} {
if param.Options.SecretsManagerEnable {
if sm != nil && smError == nil {
name := param.Name
if param.Options.StageSensitive {
if stage, ok := defaultParams.result[StageParameter]; ok {
nameWithStage := fmt.Sprintf("%v_%v", name, strings.ToUpper(stage.(string)))
value, _, err := sm.GetValue(nameWithStage)
if err == nil {
return convertString(value, param.valueType)
}
}
} else {
if stage, ok := defaultParams.result[StageParameter]; ok {
nameWithStage := fmt.Sprintf("%v_%v", name, strings.ToUpper(stage.(string)))
value, _, err := sm.GetValue(nameWithStage)
if err == nil {
return convertString(value, param.valueType)
}
}
value, _, err := sm.GetValue(name)
if err == nil {
return convertString(value, param.valueType)
}
}
}
}
return nil
}
// getValueFromFlag returns the value of the desired type from the flag
func getValueFromFlag(flg *flag.Flag, typeValue string) (interface{}, bool) {
switch typeValue {
case boolType:
{
return flg.Value.(*flags.BoolValue).Get(), flg.Value.(*flags.BoolValue).IsSet()
}
case float64Type:
{
return flg.Value.(*flags.Float64Value).Get(), flg.Value.(*flags.Float64Value).IsSet()
}
case intType:
{
return flg.Value.(*flags.IntValue).Get(), flg.Value.(*flags.IntValue).IsSet()
}
case int64Type:
{
return flg.Value.(*flags.Int64Value).Get(), flg.Value.(*flags.Int64Value).IsSet()
}
case stringType:
{
return flg.Value.(*flags.StringValue).Get(), flg.Value.(*flags.StringValue).IsSet()
}
case uintType:
{
return flg.Value.(*flags.UintValue).Get(), flg.Value.(*flags.UintValue).IsSet()
}
case uint64Type:
{
return flg.Value.(*flags.Uint64Value).Get(), flg.Value.(*flags.Uint64Value).IsSet()
}
case databaseType:
{
return flg.Value.(*flags.DatabaseValue).Get(), flg.Value.(*flags.DatabaseValue).IsSet()
}
}
return nil, false
}
func parseOptions(awsSecret []Options) Options {
options := Options{}
lenOptions := len(awsSecret)
if lenOptions > 0 {
options = awsSecret[0]
}
return options
}