From 42a097d490717265d55ea0aa226c65575accf9ce Mon Sep 17 00:00:00 2001 From: Amirhossein Shaerpour <87924605+shaerpour@users.noreply.github.com> Date: Mon, 22 Jul 2024 17:41:26 +0330 Subject: [PATCH] change - add default configuration paths (#121) --- README.md | 3 ++- cmd/wireproxy/main.go | 24 ++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index fd44869..71b6f96 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ of wireproxy by [@juev](https://github.com/juev). # Usage ``` -./wireproxy -c [path to config] +./wireproxy [-c path to config] ``` ``` @@ -47,6 +47,7 @@ Arguments: -h --help Print help information -c --config Path of configuration file + Default paths: /etc/wireproxy/wireproxy.conf, $HOME/.config/wireproxy.conf -s --silent Silent mode -d --daemon Make wireproxy run in background -i --info Specify the address and port for exposing health status diff --git a/cmd/wireproxy/main.go b/cmd/wireproxy/main.go index 48880c2..713943a 100644 --- a/cmd/wireproxy/main.go +++ b/cmd/wireproxy/main.go @@ -22,6 +22,12 @@ import ( // an argument to denote that this process was spawned by -d const daemonProcess = "daemon-process" +// default paths for wireproxy config file +var default_config_paths = []string { + "/etc/wireproxy/wireproxy.conf", + os.Getenv("HOME")+"/.config/wireproxy.conf", +} + var version = "1.0.8-dev" func panicIfError(err error) { @@ -51,6 +57,16 @@ func executablePath() string { return programPath } +// check if default config file paths exist +func configFilePath() (string, bool) { + for _, path := range default_config_paths { + if _, err := os.Stat(path); err == nil { + return path, true + } + } + return "", false +} + func lock(stage string) { switch stage { case "boot": @@ -177,8 +193,12 @@ func main() { } if *config == "" { - fmt.Println("configuration path is required") - return + if path, config_exist := configFilePath(); config_exist { + *config = path + } else { + fmt.Println("configuration path is required") + return + } } if !*daemon {