From 1df568cd25d6ccac79e56451406e021ead49c0c4 Mon Sep 17 00:00:00 2001 From: Michael Aldridge Date: Wed, 31 Aug 2022 16:44:40 -0500 Subject: [PATCH] pkg/netauth: Handle config loading in library layer --- cmd/netauth/main.go | 7 ------- internal/ctl/root.go | 9 --------- pkg/netauth/netauth.go | 13 +++++++++++++ 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/cmd/netauth/main.go b/cmd/netauth/main.go index d66dd8a..7695cd5 100644 --- a/cmd/netauth/main.go +++ b/cmd/netauth/main.go @@ -4,7 +4,6 @@ import ( "os" "github.com/hashicorp/go-hclog" - "github.com/spf13/viper" "github.com/netauth/netauth/internal/ctl" @@ -22,12 +21,6 @@ var ( ) func main() { - // This runs here so we can reset the defaults that are set - // during various init() methods. - viper.SetDefault("token.cache", "fs") - viper.SetDefault("token.keyprovider", "fs") - viper.SetDefault("token.backend", "jwt-rsa") - level, set := os.LookupEnv("NETAUTH_LOGLEVEL") if !set { appLogger = hclog.NewNullLogger() diff --git a/internal/ctl/root.go b/internal/ctl/root.go index faf35b0..b20c0d2 100644 --- a/internal/ctl/root.go +++ b/internal/ctl/root.go @@ -61,15 +61,6 @@ func onInit() { viper.BindPFlags(pflag.CommandLine) if cfg != "" { viper.SetConfigFile(cfg) - } else { - viper.SetConfigName("config") - viper.AddConfigPath(".") - viper.AddConfigPath("$HOME/.netauth") - viper.AddConfigPath("/etc/netauth/") - } - if err := viper.ReadInConfig(); err != nil { - fmt.Println("Error reading config:", err) - os.Exit(1) } viper.Set("client.ServiceName", "netauth") diff --git a/pkg/netauth/netauth.go b/pkg/netauth/netauth.go index 831d64f..77a203c 100644 --- a/pkg/netauth/netauth.go +++ b/pkg/netauth/netauth.go @@ -17,6 +17,14 @@ import ( func init() { viper.SetDefault("core.port", 1729) viper.SetDefault("tls.certificate", "keys/tls.pem") + viper.SetDefault("token.cache", "fs") + viper.SetDefault("token.keyprovider", "fs") + viper.SetDefault("token.backend", "jwt-rsa") + + viper.SetConfigName("config") + viper.AddConfigPath(".") + viper.AddConfigPath("$HOME/.netauth") + viper.AddConfigPath("/etc/netauth/") } // NewWithLog uses the specified logger to contruct a NetAuth client. @@ -24,6 +32,11 @@ func init() { // handler that is provided should have the correct name and be // parented to the correct point on the log tree. func NewWithLog(l hclog.Logger) (*Client, error) { + if err := viper.ReadInConfig(); err != nil { + fmt.Println("Error reading config:", err) + os.Exit(1) + } + if viper.GetString("core.conf") == "" { viper.Set("core.conf", filepath.Dir(viper.ConfigFileUsed())) l.Debug("Config relative load path set", "path", viper.GetString("core.conf"))