Get rid of hardcoded binary name

This commit is contained in:
Andrey Meshkov 2019-02-05 20:35:48 +03:00
parent 93ea27077f
commit 0fbfa057b1
3 changed files with 22 additions and 25 deletions

28
app.go
View file

@ -8,11 +8,9 @@ import (
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
"path"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strconv" "strconv"
"strings"
"syscall" "syscall"
"time" "time"
@ -26,9 +24,6 @@ import (
var VersionString = "undefined" var VersionString = "undefined"
const ( const (
// We use it to detect the working dir
executableName = "AdGuardHome"
// Used in config to indicate that syslog or eventlog (win) should be used for logger output // Used in config to indicate that syslog or eventlog (win) should be used for logger output
configSyslog = "syslog" configSyslog = "syslog"
) )
@ -65,7 +60,7 @@ func run(args options) {
} }
// configure working dir and config path // configure working dir and config path
initWorkingDir() initWorkingDir(args)
// configure log level and output // configure log level and output
configureLogger(args) configureLogger(args)
@ -160,21 +155,17 @@ func run(args options) {
} }
// initWorkingDir initializes the ourBinaryDir (basically, we use it as a working dir) // initWorkingDir initializes the ourBinaryDir (basically, we use it as a working dir)
func initWorkingDir() { func initWorkingDir(args options) {
exec, err := os.Executable() exec, err := os.Executable()
if err != nil { if err != nil {
panic(err) panic(err)
} }
currentExecutableName := filepath.Base(exec) if args.configFilename != "" {
currentExecutableName = strings.TrimSuffix(currentExecutableName, path.Ext(currentExecutableName)) // If there is a custom config file, use it's directory as our working dir
if currentExecutableName == executableName { config.ourBinaryDir = filepath.Dir(args.configFilename)
// Binary build
config.ourBinaryDir = filepath.Dir(exec)
} else { } else {
// Most likely we're debugging -- using current working directory in this case config.ourBinaryDir = filepath.Dir(exec)
workDir, _ := os.Getwd()
config.ourBinaryDir = workDir
} }
} }
@ -356,11 +347,8 @@ func promptAndGetPassword(prompt string) (string, error) {
} }
func askUsernamePasswordIfPossible() error { func askUsernamePasswordIfPossible() error {
configfile := config.ourConfigFilename configFile := config.getConfigFilename()
if !filepath.IsAbs(configfile) { _, err := os.Stat(configFile)
configfile = filepath.Join(config.ourBinaryDir, config.ourConfigFilename)
}
_, err := os.Stat(configfile)
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
// do nothing, file exists // do nothing, file exists
return nil return nil

View file

@ -87,6 +87,15 @@ var config = configuration{
SchemaVersion: currentSchemaVersion, SchemaVersion: currentSchemaVersion,
} }
// getConfigFilename returns path to the current config file
func (c *configuration) getConfigFilename() string {
configFile := config.ourConfigFilename
if !filepath.IsAbs(configFile) {
configFile = filepath.Join(config.ourBinaryDir, config.ourConfigFilename)
}
return configFile
}
// getLogSettings reads logging settings from the config file. // getLogSettings reads logging settings from the config file.
// we do it in a separate method in order to configure logger before the actual configuration is parsed and applied. // we do it in a separate method in order to configure logger before the actual configuration is parsed and applied.
func getLogSettings() logSettings { func getLogSettings() logSettings {
@ -104,7 +113,7 @@ func getLogSettings() logSettings {
// parseConfig loads configuration from the YAML file // parseConfig loads configuration from the YAML file
func parseConfig() error { func parseConfig() error {
configFile := filepath.Join(config.ourBinaryDir, config.ourConfigFilename) configFile := config.getConfigFilename()
log.Printf("Reading YAML file: %s", configFile) log.Printf("Reading YAML file: %s", configFile)
yamlFile, err := readConfigFile() yamlFile, err := readConfigFile()
if err != nil { if err != nil {
@ -131,7 +140,7 @@ func parseConfig() error {
// readConfigFile reads config file contents if it exists // readConfigFile reads config file contents if it exists
func readConfigFile() ([]byte, error) { func readConfigFile() ([]byte, error) {
configFile := filepath.Join(config.ourBinaryDir, config.ourConfigFilename) configFile := config.getConfigFilename()
if _, err := os.Stat(configFile); os.IsNotExist(err) { if _, err := os.Stat(configFile); os.IsNotExist(err) {
// do nothing, file doesn't exist // do nothing, file doesn't exist
return nil, nil return nil, nil
@ -143,7 +152,7 @@ func readConfigFile() ([]byte, error) {
func (c *configuration) write() error { func (c *configuration) write() error {
c.Lock() c.Lock()
defer c.Unlock() defer c.Unlock()
configFile := filepath.Join(config.ourBinaryDir, config.ourConfigFilename) configFile := config.getConfigFilename()
log.Printf("Writing YAML file: %s", configFile) log.Printf("Writing YAML file: %s", configFile)
yamlText, err := yaml.Marshal(&config) yamlText, err := yaml.Marshal(&config)
if err != nil { if err != nil {

View file

@ -15,7 +15,7 @@ const currentSchemaVersion = 2 // used for upgrading from old configs to new con
// Performs necessary upgrade operations if needed // Performs necessary upgrade operations if needed
func upgradeConfig() error { func upgradeConfig() error {
// read a config file into an interface map, so we can manipulate values without losing any // read a config file into an interface map, so we can manipulate values without losing any
configFile := filepath.Join(config.ourBinaryDir, config.ourConfigFilename) configFile := config.getConfigFilename()
if _, err := os.Stat(configFile); os.IsNotExist(err) { if _, err := os.Stat(configFile); os.IsNotExist(err) {
log.Printf("config file %s does not exist, nothing to upgrade", configFile) log.Printf("config file %s does not exist, nothing to upgrade", configFile)
return nil return nil
@ -74,7 +74,7 @@ func upgradeConfigSchema(oldVersion int, diskConfig *map[string]interface{}) err
return err return err
} }
configFile := filepath.Join(config.ourBinaryDir, config.ourConfigFilename) configFile := config.getConfigFilename()
body, err := yaml.Marshal(diskConfig) body, err := yaml.Marshal(diskConfig)
if err != nil { if err != nil {
log.Printf("Couldn't generate YAML file: %s", err) log.Printf("Couldn't generate YAML file: %s", err)