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"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"
@ -119,8 +120,11 @@ type ServerSummary struct {
}
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")
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")
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")
@ -141,6 +145,7 @@ func ConfigLoad(proxy *Proxy, svcFlag *string) error {
if _, err := toml.DecodeFile(*configFile, &config); err != nil {
return err
}
cdFileDir(*configFile)
if config.LogLevel >= 0 && config.LogLevel < int(dlog.SeverityLast) {
dlog.SetLogLevel(dlog.Severity(config.LogLevel))
}
@ -384,3 +389,16 @@ func includesName(names []string, name string) bool {
}
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 (
"flag"
"fmt"
"os"
"path/filepath"
"sync"
"time"
@ -25,9 +23,6 @@ type App struct {
func main() {
dlog.Init("dnscrypt-proxy", dlog.SeverityNotice, "DAEMON")
cdLocal()
svcConfig := &service.Config{
Name: "dnscrypt-proxy",
DisplayName: "DNSCrypt client proxy",
@ -43,7 +38,6 @@ func main() {
app.proxy = Proxy{}
app.proxy.xTransport = NewXTransport(30 * time.Second)
cdFileDir(DefaultConfigFileName)
if err := ConfigLoad(&app.proxy, svcFlag); err != nil {
dlog.Fatal(err)
}
@ -106,16 +100,3 @@ func (app *App) Stop(service service.Service) error {
dlog.Notice("Stopped.")
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))
}