mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-22 04:55:33 +03:00
Get rid of hardcoded binary name
This commit is contained in:
parent
93ea27077f
commit
0fbfa057b1
3 changed files with 22 additions and 25 deletions
28
app.go
28
app.go
|
@ -8,11 +8,9 @@ import (
|
|||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
|
@ -26,9 +24,6 @@ import (
|
|||
var VersionString = "undefined"
|
||||
|
||||
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
|
||||
configSyslog = "syslog"
|
||||
)
|
||||
|
@ -65,7 +60,7 @@ func run(args options) {
|
|||
}
|
||||
|
||||
// configure working dir and config path
|
||||
initWorkingDir()
|
||||
initWorkingDir(args)
|
||||
|
||||
// configure log level and output
|
||||
configureLogger(args)
|
||||
|
@ -160,21 +155,17 @@ func run(args options) {
|
|||
}
|
||||
|
||||
// initWorkingDir initializes the ourBinaryDir (basically, we use it as a working dir)
|
||||
func initWorkingDir() {
|
||||
func initWorkingDir(args options) {
|
||||
exec, err := os.Executable()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
currentExecutableName := filepath.Base(exec)
|
||||
currentExecutableName = strings.TrimSuffix(currentExecutableName, path.Ext(currentExecutableName))
|
||||
if currentExecutableName == executableName {
|
||||
// Binary build
|
||||
config.ourBinaryDir = filepath.Dir(exec)
|
||||
if args.configFilename != "" {
|
||||
// If there is a custom config file, use it's directory as our working dir
|
||||
config.ourBinaryDir = filepath.Dir(args.configFilename)
|
||||
} else {
|
||||
// Most likely we're debugging -- using current working directory in this case
|
||||
workDir, _ := os.Getwd()
|
||||
config.ourBinaryDir = workDir
|
||||
config.ourBinaryDir = filepath.Dir(exec)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -356,11 +347,8 @@ func promptAndGetPassword(prompt string) (string, error) {
|
|||
}
|
||||
|
||||
func askUsernamePasswordIfPossible() error {
|
||||
configfile := config.ourConfigFilename
|
||||
if !filepath.IsAbs(configfile) {
|
||||
configfile = filepath.Join(config.ourBinaryDir, config.ourConfigFilename)
|
||||
}
|
||||
_, err := os.Stat(configfile)
|
||||
configFile := config.getConfigFilename()
|
||||
_, err := os.Stat(configFile)
|
||||
if !os.IsNotExist(err) {
|
||||
// do nothing, file exists
|
||||
return nil
|
||||
|
|
15
config.go
15
config.go
|
@ -87,6 +87,15 @@ var config = configuration{
|
|||
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.
|
||||
// we do it in a separate method in order to configure logger before the actual configuration is parsed and applied.
|
||||
func getLogSettings() logSettings {
|
||||
|
@ -104,7 +113,7 @@ func getLogSettings() logSettings {
|
|||
|
||||
// parseConfig loads configuration from the YAML file
|
||||
func parseConfig() error {
|
||||
configFile := filepath.Join(config.ourBinaryDir, config.ourConfigFilename)
|
||||
configFile := config.getConfigFilename()
|
||||
log.Printf("Reading YAML file: %s", configFile)
|
||||
yamlFile, err := readConfigFile()
|
||||
if err != nil {
|
||||
|
@ -131,7 +140,7 @@ func parseConfig() error {
|
|||
|
||||
// readConfigFile reads config file contents if it exists
|
||||
func readConfigFile() ([]byte, error) {
|
||||
configFile := filepath.Join(config.ourBinaryDir, config.ourConfigFilename)
|
||||
configFile := config.getConfigFilename()
|
||||
if _, err := os.Stat(configFile); os.IsNotExist(err) {
|
||||
// do nothing, file doesn't exist
|
||||
return nil, nil
|
||||
|
@ -143,7 +152,7 @@ func readConfigFile() ([]byte, error) {
|
|||
func (c *configuration) write() error {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
configFile := filepath.Join(config.ourBinaryDir, config.ourConfigFilename)
|
||||
configFile := config.getConfigFilename()
|
||||
log.Printf("Writing YAML file: %s", configFile)
|
||||
yamlText, err := yaml.Marshal(&config)
|
||||
if err != nil {
|
||||
|
|
|
@ -15,7 +15,7 @@ const currentSchemaVersion = 2 // used for upgrading from old configs to new con
|
|||
// Performs necessary upgrade operations if needed
|
||||
func upgradeConfig() error {
|
||||
// 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) {
|
||||
log.Printf("config file %s does not exist, nothing to upgrade", configFile)
|
||||
return nil
|
||||
|
@ -74,7 +74,7 @@ func upgradeConfigSchema(oldVersion int, diskConfig *map[string]interface{}) err
|
|||
return err
|
||||
}
|
||||
|
||||
configFile := filepath.Join(config.ourBinaryDir, config.ourConfigFilename)
|
||||
configFile := config.getConfigFilename()
|
||||
body, err := yaml.Marshal(diskConfig)
|
||||
if err != nil {
|
||||
log.Printf("Couldn't generate YAML file: %s", err)
|
||||
|
|
Loading…
Reference in a new issue