Check if the config file exists from the current directory

Try the executable directory if it fails

Then, go to that config file directory no matter what

Fixes #80
This commit is contained in:
Frank Denis 2018-02-03 10:46:47 +01:00
parent 67b0d95ea1
commit f513ab21fa
2 changed files with 19 additions and 20 deletions

View File

@ -6,6 +6,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"os" "os"
"path/filepath"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -119,8 +120,11 @@ type ServerSummary struct {
} }
func ConfigLoad(proxy *Proxy, svcFlag *string) error { func ConfigLoad(proxy *Proxy, svcFlag *string) error {
version := flag.Bool("version", false, "Prints current proxy version")
configFile := flag.String("config", DefaultConfigFileName, "Path to the configuration file") configFile := flag.String("config", DefaultConfigFileName, "Path to the configuration file")
if _, err := os.Stat(*configFile); os.IsNotExist(err) {
cdLocal()
}
version := flag.Bool("version", false, "Prints current proxy version")
resolve := flag.String("resolve", "", "resolve a name using system libraries") resolve := flag.String("resolve", "", "resolve a name using system libraries")
list := flag.Bool("list", false, "print the list of available resolvers for the enabled filters") list := flag.Bool("list", false, "print the list of available resolvers for the enabled filters")
listAll := flag.Bool("list-all", false, "print the complete list of available resolvers, ignoring filters") listAll := flag.Bool("list-all", false, "print the complete list of available resolvers, ignoring filters")
@ -141,6 +145,7 @@ func ConfigLoad(proxy *Proxy, svcFlag *string) error {
if _, err := toml.DecodeFile(*configFile, &config); err != nil { if _, err := toml.DecodeFile(*configFile, &config); err != nil {
return err return err
} }
cdFileDir(*configFile)
if config.LogLevel >= 0 && config.LogLevel < int(dlog.SeverityLast) { if config.LogLevel >= 0 && config.LogLevel < int(dlog.SeverityLast) {
dlog.SetLogLevel(dlog.Severity(config.LogLevel)) dlog.SetLogLevel(dlog.Severity(config.LogLevel))
} }
@ -384,3 +389,16 @@ func includesName(names []string, name string) bool {
} }
return false return false
} }
func cdFileDir(fileName string) {
os.Chdir(filepath.Dir(fileName))
}
func cdLocal() {
exeFileName, err := os.Executable()
if err != nil {
dlog.Warnf("Unable to determine the executable directory: [%s] -- You will need to specify absolute paths in the configuration file", err)
return
}
os.Chdir(filepath.Dir(exeFileName))
}

View File

@ -3,8 +3,6 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"os"
"path/filepath"
"sync" "sync"
"time" "time"
@ -25,9 +23,6 @@ type App struct {
func main() { func main() {
dlog.Init("dnscrypt-proxy", dlog.SeverityNotice, "DAEMON") dlog.Init("dnscrypt-proxy", dlog.SeverityNotice, "DAEMON")
cdLocal()
svcConfig := &service.Config{ svcConfig := &service.Config{
Name: "dnscrypt-proxy", Name: "dnscrypt-proxy",
DisplayName: "DNSCrypt client proxy", DisplayName: "DNSCrypt client proxy",
@ -43,7 +38,6 @@ func main() {
app.proxy = Proxy{} app.proxy = Proxy{}
app.proxy.xTransport = NewXTransport(30 * time.Second) app.proxy.xTransport = NewXTransport(30 * time.Second)
cdFileDir(DefaultConfigFileName)
if err := ConfigLoad(&app.proxy, svcFlag); err != nil { if err := ConfigLoad(&app.proxy, svcFlag); err != nil {
dlog.Fatal(err) dlog.Fatal(err)
} }
@ -106,16 +100,3 @@ func (app *App) Stop(service service.Service) error {
dlog.Notice("Stopped.") dlog.Notice("Stopped.")
return nil return nil
} }
func cdFileDir(fileName string) {
os.Chdir(filepath.Dir(fileName))
}
func cdLocal() {
exeFileName, err := os.Executable()
if err != nil {
dlog.Warnf("Unable to determine the executable directory: [%s] -- You will need to specify absolute paths in the configuration file", err)
return
}
os.Chdir(filepath.Dir(exeFileName))
}