Skip to content

Commit

Permalink
Merge pull request #101 from hookdeck/fix/persist-api-key-with-login
Browse files Browse the repository at this point in the history
Fix: persist api key with login
  • Loading branch information
leggetter authored Aug 20, 2024
2 parents 45b17e5 + fb612d9 commit d457025
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
48 changes: 27 additions & 21 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -71,13 +66,32 @@ 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")
}

// InitConfig reads in profiles file and ENV variables if set.
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,
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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()
}

Expand Down
15 changes: 14 additions & 1 deletion pkg/login/client_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -45,6 +51,13 @@ 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)

if err = config.Profile.SaveProfile(false); err != nil {
return err
}
if err = config.Profile.UseProfile(); err != nil {
return err
}

return nil
}

Expand Down

0 comments on commit d457025

Please sign in to comment.