Skip to content

Commit de17905

Browse files
committed
feat: simpler config loading; separate paths for windows/nix
1 parent b973e2e commit de17905

5 files changed

Lines changed: 77 additions & 52 deletions

File tree

bin/vproxy/config.go

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ func fileExists(name string) bool {
4444
return true
4545
}
4646

47+
// findConfig locates a config file at the given locations with either a .conf or .toml extension
48+
// (file format must be TOML, however)
4749
func findConfig(files ...string) string {
4850
for _, config := range files {
4951
if config != "" {
@@ -70,14 +72,6 @@ func homeConfPath() string {
7072
return ""
7173
}
7274

73-
func findClientConfig(path string) string {
74-
return findConfig(path, ".vproxy.conf", homeConfPath(), "/usr/local/etc/vproxy.conf", "/etc/vproxy.conf")
75-
}
76-
77-
func findDaemonConfig(path string) string {
78-
return findConfig(path, homeConfPath(), "/usr/local/etc/vproxy.conf", "/etc/vproxy.conf")
79-
}
80-
8175
func loadConfigFile(path string) (*Config, error) {
8276
t, err := toml.LoadFile(path)
8377
if err != nil {
@@ -102,48 +96,21 @@ func cleanListenAddr(c *cli.Context) {
10296
}
10397

10498
func loadClientConfig(c *cli.Context) error {
105-
conf := findClientConfig(c.String("config"))
106-
if cf := c.String("config"); c.IsSet("config") && conf != cf {
107-
log.Fatalf("error: config file not found: %s\n", cf)
108-
}
109-
if conf == "" {
110-
return nil
111-
}
112-
verbose(c, "Loading config file %s", conf)
113-
config, err := loadConfigFile(conf)
114-
if err != nil {
115-
return err
116-
}
117-
if config != nil {
118-
if v := (config.Client.Verbose || config.Verbose); v && !c.IsSet("verbose") {
119-
c.Lineage()[1].Set("verbose", "true")
120-
verbose(c, "Loading config file %s", conf)
121-
verbose(c, "via conf: verbose=true")
122-
}
123-
if v := config.Client.Host; v != "" && !c.IsSet("host") {
124-
verbose(c, "via conf: host=%s", v)
125-
c.Set("host", v)
126-
}
127-
if v := config.Client.HTTP; v > 0 && !c.IsSet("http") {
128-
verbose(c, "via conf: http=%d", v)
129-
c.Set("http", strconv.Itoa(v))
130-
}
131-
if v := config.Client.Bind; v != "" && !c.IsSet("bind") {
132-
verbose(c, "via conf: bind=%s", v)
133-
c.Set("bind", v)
134-
}
135-
if v := config.Server.CaRootPath; v != "" {
136-
os.Setenv("CAROOT_PATH", v)
137-
verbose(c, "via conf: CAROOT_PATH=%s", v)
138-
}
139-
}
140-
return nil
99+
conf := findConfigFile(c.String("config"), false)
100+
return loadConfig(c, conf)
141101
}
142102

143103
func loadDaemonConfig(c *cli.Context) error {
144-
conf := findClientConfig(c.String("config"))
145-
if cf := c.String("config"); c.IsSet("config") && conf != cf {
146-
log.Fatalf("error: config file not found: %s\n", cf)
104+
conf := findConfigFile(c.String("config"), true)
105+
return loadConfig(c, conf)
106+
}
107+
108+
func loadConfig(c *cli.Context, conf string) error {
109+
if c.IsSet("config") {
110+
if cf := c.String("config"); conf != cf {
111+
// config flag was passed but file does not exist
112+
log.Fatalf("error: config file not found: %s\n", cf)
113+
}
147114
}
148115
if conf == "" {
149116
return nil
@@ -181,6 +148,29 @@ func loadDaemonConfig(c *cli.Context) error {
181148
os.Setenv("CERT_PATH", v)
182149
verbose(c, "via conf: CERT_PATH=%s", v)
183150
}
151+
152+
// client configs
153+
if v := (config.Client.Verbose || config.Verbose); v && !c.IsSet("verbose") {
154+
c.Lineage()[1].Set("verbose", "true")
155+
verbose(c, "Loading config file %s", conf)
156+
verbose(c, "via conf: verbose=true")
157+
}
158+
if v := config.Client.Host; v != "" && !c.IsSet("host") {
159+
verbose(c, "via conf: host=%s", v)
160+
c.Set("host", v)
161+
}
162+
if v := config.Client.HTTP; v > 0 && !c.IsSet("http") {
163+
verbose(c, "via conf: http=%d", v)
164+
c.Set("http", strconv.Itoa(v))
165+
}
166+
if v := config.Client.Bind; v != "" && !c.IsSet("bind") {
167+
verbose(c, "via conf: bind=%s", v)
168+
c.Set("bind", v)
169+
}
170+
if v := config.Server.CaRootPath; v != "" {
171+
os.Setenv("CAROOT_PATH", v)
172+
verbose(c, "via conf: CAROOT_PATH=%s", v)
173+
}
184174
}
185175
cleanListenAddr(c)
186176
return nil

bin/vproxy/config_nix.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//go:build linux || darwin
2+
3+
package main
4+
5+
func findConfigFile(path string, isDaemon bool) string {
6+
paths := []string{path}
7+
if !isDaemon {
8+
// look for dot file only for clients
9+
paths = append(paths, ".vproxy.conf")
10+
}
11+
paths = append(paths, homeConfPath(), "/usr/local/etc/vproxy.conf", "/etc/vproxy.conf")
12+
return findConfig(paths...)
13+
}

bin/vproxy/config_windows.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//go:build windows
2+
3+
package main
4+
5+
func findConfigFile(path string, isDaemon bool) string {
6+
paths := []string{path}
7+
if !isDaemon {
8+
// look for dot file only for clients
9+
paths = append(paths, ".vproxy.conf")
10+
}
11+
paths = append(paths, homeConfPath())
12+
return findConfig(paths...)
13+
}

bin/vproxy/flags.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,11 @@ vproxy connect hello.local:8888 -- vproxy hello
227227
},
228228
},
229229
{
230-
Name: "info",
231-
Usage: "Print vproxy configuration",
232-
Before: loadDaemonConfig,
233-
Action: printInfo,
230+
Name: "info",
231+
Usage: "Print vproxy configuration",
232+
Description: `More verbose info: vproxy -v info`,
233+
Before: loadDaemonConfig,
234+
Action: printInfo,
234235
},
235236
{
236237
Name: "hello",

bin/vproxy/main.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,16 @@ func printInfo(c *cli.Context) error {
279279
printVersion(c)
280280
fmt.Printf(" CAROOT=%s\n", vproxy.CARootPath())
281281
fmt.Printf(" CERT_PATH=%s\n", vproxy.CertPath())
282+
283+
confFile := findConfigFile(c.String("config"), false)
284+
if confFile == "" {
285+
fmt.Printf("\n Config file: [not found]\n")
286+
} else {
287+
fmt.Printf("\n Detected Config file: %s (loaded)\n", confFile)
288+
}
289+
282290
certs, _ := filepath.Glob(filepath.Join(vproxy.CertPath(), "*-key.pem"))
283-
fmt.Printf("\n Nubmer of installed certs: %d\n", len(certs))
291+
fmt.Printf("\n Number of installed certs: %d\n", len(certs))
284292
fmt.Println(" Certs:")
285293
for _, cert := range certs {
286294
host := strings.TrimPrefix(strings.TrimSuffix(cert, "-key.pem"), vproxy.CertPath()+string(filepath.Separator))

0 commit comments

Comments
 (0)