-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
157 lines (129 loc) · 2.93 KB
/
init.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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
version = "dev"
//nolint:gochecknoglobals // necessary for cobra init
cfgPath string
)
const (
defaultConfigDir = "/etc/void"
defaultConfigName = "config"
defaultPort = 53
)
//nolint:gochecknoglobals // necessary for cobra root command
var root = &cobra.Command{
Use: "void [flags]",
Short: "void is a simple cluster based dns provider/sink",
Version: version,
Run: exec,
}
// TODO: Make DNS sink a subcommand
// TODO: Make Proxy a subcommand (future)
// TODO: Root command should base function on config file
func init() {
var err error
defer func() {
if err != nil {
log.Fatal(err)
}
}()
cobra.OnInitialize(initConfig)
root.PersistentFlags().StringVar(&cfgPath, "config", "", "config path")
root.PersistentFlags().BoolP(
"verbose",
"v",
false,
"verbose output",
)
err = viper.BindPFlag("verbose", root.PersistentFlags().Lookup("verbose"))
if err != nil {
return
}
root.PersistentFlags().String(
"cache",
"/etc/void/cache",
"cache folder for remote sources",
)
err = viper.BindPFlag("config", root.PersistentFlags().Lookup("config"))
if err != nil {
return
}
if viper.GetString("config") != "" {
viper.SetConfigFile(viper.GetString("config"))
}
root.PersistentFlags().Uint16P(
"port",
"p",
defaultPort,
"DNS listening port",
)
root.PersistentFlags().StringSliceP(
"upstream",
"u",
[]string{
"tcp-tls://1.1.1.1:853",
"tcp-tls://1.0.0.1:853",
},
"Upstream DNS Servers",
)
root.PersistentFlags().StringSlice(
"peers",
[]string{},
"DNS cluster peers (example: tcp://192.168.0.10, tcp-tls://, quic://)",
)
err = viper.BindPFlag("dns.port", root.PersistentFlags().Lookup("port"))
if err != nil {
return
}
err = viper.BindPFlag("dns.upstream", root.PersistentFlags().Lookup("upstream"))
if err != nil {
return
}
err = viper.BindPFlag("dns.cache", root.PersistentFlags().Lookup("cache"))
if err != nil {
return
}
err = viper.BindPFlag("dns.peers", root.PersistentFlags().Lookup("peers"))
if err != nil {
return
}
}
func initConfig() {
if cfgPath != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgPath)
} else {
viper.SetConfigName(defaultConfigName)
viper.AddConfigPath(defaultConfigDir)
// Check working directory for config
wd, err := os.Getwd()
if err == nil {
viper.AddConfigPath(wd)
}
}
// Check home directory/.void for config
home, err := os.UserHomeDir()
if err == nil {
viper.AddConfigPath(filepath.Join(home, ".void"))
}
// Check working directory for config
wd, err := os.Getwd()
if err == nil {
viper.AddConfigPath(wd)
}
// Use VOID as a prefix for all environment variables that
// map to configuration values
viper.SetEnvPrefix("VOID")
viper.AutomaticEnv()
err = viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("fatal error config file: %w", err))
}
}