From c642cee0c0f0a82ccf901ab1a5db36803c1c9f7c Mon Sep 17 00:00:00 2001 From: Phil Leggetter Date: Tue, 20 Aug 2024 09:28:58 +0100 Subject: [PATCH 1/3] chore: improve config logging MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For example, set log level at the very beginning or the config logging is missed 🤦‍♂️ --- pkg/config/config.go | 48 +++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index f76b7ed..bc89042 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -56,11 +56,6 @@ type Config struct { func (c *Config) GetConfigFolder(xdgPath string) string { configPath := xdgPath - log.WithFields(log.Fields{ - "prefix": "config.Config.GetProfilesFolder", - "path": configPath, - }).Debug("Using profiles file") - if configPath == "" { home, err := homedir.Dir() if err != nil { @@ -71,6 +66,11 @@ func (c *Config) GetConfigFolder(xdgPath string) string { configPath = filepath.Join(home, ".config") } + log.WithFields(log.Fields{ + "prefix": "config.Config.GetProfilesFolder", + "path": configPath, + }).Debug("Using profiles folder") + return filepath.Join(configPath, "hookdeck") } @@ -78,6 +78,20 @@ func (c *Config) GetConfigFolder(xdgPath string) string { func (c *Config) InitConfig() { c.Profile.Config = c + // Set log level + switch c.LogLevel { + case "debug": + log.SetLevel(log.DebugLevel) + case "info": + log.SetLevel(log.InfoLevel) + case "warn": + log.SetLevel(log.WarnLevel) + case "error": + log.SetLevel(log.ErrorLevel) + default: + log.Fatalf("Unrecognized log level value: %s. Expected one of debug, info, warn, error.", c.LogLevel) + } + logFormatter := &prefixed.TextFormatter{ FullTimestamp: true, TimestampFormat: time.RFC1123, @@ -102,7 +116,7 @@ func (c *Config) InitConfig() { log.WithFields(log.Fields{ "prefix": "config.Config.InitConfig", "path": c.GlobalConfig.ConfigFileUsed(), - }).Debug("Using profiles file") + }).Debug("Using global profiles file") } // Read local config @@ -127,7 +141,7 @@ func (c *Config) InitConfig() { log.WithFields(log.Fields{ "prefix": "config.Config.InitConfig", "path": c.LocalConfig.ConfigFileUsed(), - }).Debug("Using profiles file") + }).Debug("Using local profiles file") } // Construct the config struct @@ -155,20 +169,6 @@ func (c *Config) InitConfig() { } log.SetFormatter(logFormatter) - - // Set log level - switch c.LogLevel { - case "debug": - log.SetLevel(log.DebugLevel) - case "info": - log.SetLevel(log.InfoLevel) - case "warn": - log.SetLevel(log.WarnLevel) - case "error": - log.SetLevel(log.ErrorLevel) - default: - log.Fatalf("Unrecognized log level value: %s. Expected one of debug, info, warn, error.", c.LogLevel) - } } // EditConfig opens the configuration file in the default editor. @@ -250,6 +250,12 @@ func (c *Config) WriteGlobalConfig() error { if err := makePath(c.GlobalConfig.ConfigFileUsed()); err != nil { return err } + + log.WithFields(log.Fields{ + "prefix": "config.Config.WriteGlobalConfig", + "path": c.GlobalConfig.ConfigFileUsed(), + }).Debug("Writing global config") + return c.GlobalConfig.WriteConfig() } From aa6dbd94c604fc04f8cea0371b137666006bbe73 Mon Sep 17 00:00:00 2001 From: Phil Leggetter Date: Tue, 20 Aug 2024 09:40:16 +0100 Subject: [PATCH 2/3] fix: save API key to profile --- pkg/login/client_login.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/pkg/login/client_login.go b/pkg/login/client_login.go index 19152b8..ccac6ef 100644 --- a/pkg/login/client_login.go +++ b/pkg/login/client_login.go @@ -6,11 +6,12 @@ import ( "fmt" "io" "io/ioutil" - "log" "net/http" "net/url" "os" + log "github.com/sirupsen/logrus" + "github.com/briandowns/spinner" "github.com/hookdeck/hookdeck-cli/pkg/ansi" @@ -36,6 +37,11 @@ func Login(config *config.Config, input io.Reader) error { var s *spinner.Spinner if config.Profile.APIKey != "" { + log.WithFields(log.Fields{ + "prefix": "login.Login", + "APIKey": config.Profile.APIKey, + }).Debug("Logging in with API key") + s = ansi.StartNewSpinner("Verifying credentials...", os.Stdout) response, err := ValidateKey(config.APIBaseURL, config.Profile.APIKey, config.Profile.TeamID) if err != nil { @@ -45,6 +51,8 @@ func Login(config *config.Config, input io.Reader) error { message := SuccessMessage(response.UserName, response.UserEmail, response.OrganizationName, response.TeamName, response.TeamMode == "console") ansi.StopSpinner(s, message, os.Stdout) + config.Profile.SaveProfile(config.LocalConfigFile != "") + return nil } From fb612d9ebfd13b32dd6680bdf1c149c66f56a62f Mon Sep 17 00:00:00 2001 From: Phil Leggetter Date: Tue, 20 Aug 2024 11:36:03 +0100 Subject: [PATCH 3/3] chore: handle errors on login/save profile and also persist profile --- pkg/login/client_login.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pkg/login/client_login.go b/pkg/login/client_login.go index ccac6ef..00a2c5a 100644 --- a/pkg/login/client_login.go +++ b/pkg/login/client_login.go @@ -51,7 +51,12 @@ func Login(config *config.Config, input io.Reader) error { message := SuccessMessage(response.UserName, response.UserEmail, response.OrganizationName, response.TeamName, response.TeamMode == "console") ansi.StopSpinner(s, message, os.Stdout) - config.Profile.SaveProfile(config.LocalConfigFile != "") + if err = config.Profile.SaveProfile(false); err != nil { + return err + } + if err = config.Profile.UseProfile(); err != nil { + return err + } return nil }