From 97397a7924887acfc3ec3697a8ab7c5f3e553189 Mon Sep 17 00:00:00 2001 From: Sam Willcocks Date: Fri, 8 Dec 2023 17:53:01 +0000 Subject: [PATCH 1/2] Add explicit config arg This is useful for when your config file isn't in one of the usual locations. --- main.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index c77e355..ab30194 100644 --- a/main.go +++ b/main.go @@ -31,14 +31,19 @@ type IRCCat struct { func main() { debug := flag.Bool("debug", false, "Print raw IRC lines") + configFile := flag.String("config", "", "Path to config file to use") flag.Parse() loggo.ConfigureLoggers("=INFO") log.Infof("IRCCat %s (%s) starting...", branch, revision) - viper.SetConfigName("irccat") - viper.AddConfigPath("/run/secrets") - viper.AddConfigPath("/etc") - viper.AddConfigPath(".") + if *configFile != "" { + viper.SetConfigFile(*configFile) + } else { + viper.SetConfigName("irccat") + viper.AddConfigPath("/run/secrets") + viper.AddConfigPath("/etc") + viper.AddConfigPath(".") + } var err error err = viper.ReadInConfig() From 3c98cb1a683f561bd3838b1e830cbb766db03f24 Mon Sep 17 00:00:00 2001 From: Sam Willcocks Date: Fri, 8 Dec 2023 18:13:42 +0000 Subject: [PATCH 2/2] Put config file search path in one place --- main.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index ab30194..be14267 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ import ( "github.com/thoj/go-ircevent" "os" "os/signal" + "strings" "syscall" ) @@ -20,6 +21,12 @@ var log = loggo.GetLogger("main") var branch string var revision string +var CONFIG_FILE_LOCATIONS = []string{ + "/etc", + "/run/secrets", + ".", +} + type IRCCat struct { auth_channel string channels mapset.Set @@ -40,15 +47,15 @@ func main() { viper.SetConfigFile(*configFile) } else { viper.SetConfigName("irccat") - viper.AddConfigPath("/run/secrets") - viper.AddConfigPath("/etc") - viper.AddConfigPath(".") + for _, p := range CONFIG_FILE_LOCATIONS { + viper.AddConfigPath(p) + } } var err error err = viper.ReadInConfig() if err != nil { - log.Errorf("Error reading config file - exiting. I'm looking for irccat.[json|yaml|toml|hcl] in . or /etc") + log.Errorf("Error reading config file - exiting. I'm looking for irccat.[json|yaml|toml|hcl] in one of %s", strings.Join(CONFIG_FILE_LOCATIONS, ", ")) return }