forked from SAP/go-hdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag.go
141 lines (123 loc) · 4.05 KB
/
flag.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
package main
import (
"flag"
"fmt"
"os"
"strconv"
"sync"
"github.com/SAP/go-hdb/driver"
)
// Flag name constants.
const (
fnDSN = "dsn"
fnHost = "host"
fnPort = "port"
fnBufferSize = "bufferSize"
fnParameters = "parameters"
fnDrop = "drop"
fnWait = "wait"
)
var flagNames = []string{fnDSN, fnHost, fnPort, fnBufferSize, fnParameters, fnDrop, fnWait}
// Environment constants.
const (
envDSN = "GOHDBDSN"
envHost = "HOST"
envPort = "PORT"
envBufferSize = "BUFFERSIZE"
envParameters = "PARAMETERS"
envDrop = "DROP"
envWait = "WAIT"
envBlur = "BLUR"
)
var (
dsn, host, port string
bufferSize int
parameters = &prmValue{prms: []prm{{1, 100000}, {10, 10000}, {100, 1000}, {1, 1000000}, {10, 100000}, {100, 10000}, {1000, 1000}}}
drop bool
wait int
)
var initOnce sync.Once
const (
schemaPrefix = "goHdbTest_"
tablePrefix = "table_"
)
// blurFlagSet is used to 'blur' information on the web ui (currently DSN not to expose user, password and ip address)
var blurFlagSet = flag.NewFlagSet("blur", flag.PanicOnError)
func init() {
initOnce.Do(func() {
defaultBufferSize := driver.NewConnector().BufferSize()
flag.StringVar(&dsn, fnDSN, getStringEnv(envDSN, "hdb://MyUser:MyPassword@localhost:39013"), fmt.Sprintf("DNS (environment variable: %s)", envDSN))
flag.StringVar(&host, fnHost, getStringEnv(envHost, "localhost"), fmt.Sprintf("HTTP host (environment variable: %s)", envHost))
flag.StringVar(&port, fnPort, getStringEnv(envPort, "8080"), fmt.Sprintf("HTTP port (environment variable: %s)", envPort))
flag.IntVar(&bufferSize, fnBufferSize, getIntEnv(envBufferSize, defaultBufferSize), fmt.Sprintf("Buffer size in bytes (environment variable: %s)", envBufferSize))
flag.Var(parameters, fnParameters, fmt.Sprintf("Parameters (environment variable: %s)", envParameters))
flag.BoolVar(&drop, fnDrop, getBoolEnv(envDrop, true), fmt.Sprintf("Drop table before test (environment variable: %s)", envDrop))
flag.IntVar(&wait, fnWait, getIntEnv(envWait, 0), fmt.Sprintf("Wait time before starting test in seconds (environment variable: %s)", envWait))
if _, ok := os.LookupEnv(envBlur); ok {
blurFlagSet.String(fnDSN, "hdb://MyUser:MyPassword@localhost:39013", fmt.Sprintf("DNS (environment variable: %s)", envDSN))
}
})
}
func lookupFlag(name string) (*flag.Flag, bool) {
if fl := blurFlagSet.Lookup(name); fl != nil {
return fl, true
}
if fl := flag.Lookup(name); fl != nil {
return fl, true
}
return nil, false
}
// flags returns a slice containing all command-line flags defined in this package.
func flags() []*flag.Flag {
flags := make([]*flag.Flag, 0)
for _, name := range flagNames {
if fl, ok := lookupFlag(name); ok {
flags = append(flags, fl)
}
}
return flags
}
// visit visits the command-line flags defined in this package.
func visit(f func(f *flag.Flag)) {
for _, fl := range flags() {
f(fl)
}
}
// getStringEnv retrieves the string value of the environment variable named by the key.
// If the variable is present in the environment the value is returned.
// Otherwise the default value defValue is retuned.
func getStringEnv(key, defValue string) string {
value, ok := os.LookupEnv(key)
if !ok {
return defValue
}
return value
}
// getIntEnv retrieves the int value of the environment variable named by the key.
// If the variable is present in the environment the value is returned.
// Otherwise the default value defValue is retuned.
func getIntEnv(key string, defValue int) int {
value, ok := os.LookupEnv(key)
if !ok {
return defValue
}
i, err := strconv.Atoi(value)
if err != nil {
return defValue
}
return i
}
// getBoolEnv retrieves the bool value of the environment variable named by the key.
// If the variable is present in the environment the value is returned.
// Otherwise the default value defValue is retuned.
func getBoolEnv(key string, defValue bool) bool {
value, ok := os.LookupEnv(key)
if !ok {
return defValue
}
b, err := strconv.ParseBool(value)
if err != nil {
return defValue
}
return b
}