-
Notifications
You must be signed in to change notification settings - Fork 0
/
generic.go
52 lines (47 loc) · 1.36 KB
/
generic.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
package main
import (
"fmt"
"github.com/mannemsolutions/pghba/pkg/hba"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func getArg(ns, key string) string {
return viper.GetString(fmt.Sprintf("%s.%s", ns, key))
}
func argsToRules(namespace string) *hba.Rules {
var rowNum int
if namespace != "add" || viper.GetString("line") == "auto" {
rowNum = -1
} else {
rowNum = viper.GetInt("line")
}
rules, err := hba.NewRules(
rowNum, getArg(namespace, "connection_type"), getArg(namespace, "database"),
getArg(namespace, "user"), getArg(namespace, "source"), getArg(namespace, "mask"),
getArg(namespace, "authMethod"), getArg(namespace, "authOptions"))
if err != nil {
log.Fatalf("cannot parse arguments into rule: %e", err)
}
return rules
}
func bindArgument(ns string, key string, cmd *cobra.Command, envVars []string, defaultValue string) {
var err error
var viperKey string
if ns == "" {
viperKey = key
} else {
viperKey = fmt.Sprintf("%s.%s", ns, key)
}
err = viper.BindPFlag(viperKey, cmd.PersistentFlags().Lookup(key))
if err != nil {
log.Fatalf("error while binding argument for %s: %e", key, err)
}
if len(envVars) > 0 {
envVars = append([]string{key}, envVars...)
err = viper.BindEnv(envVars...)
if err != nil {
log.Fatal("error while binding env var for %s: %e", viperKey, err)
}
}
viper.SetDefault(viperKey, defaultValue)
}