Renamed vars and switched to use zerolog

This commit is contained in:
David Allen 2024-07-30 14:02:12 -06:00
parent 606a7b47cc
commit b27e6b6a73
No known key found for this signature in database
GPG key ID: 717C593FF60A2ACC
7 changed files with 68 additions and 103 deletions

View file

@ -7,9 +7,9 @@ import (
"os"
magellan "github.com/OpenCHAMI/magellan/internal"
"github.com/OpenCHAMI/magellan/internal/log"
"github.com/OpenCHAMI/magellan/internal/util"
"github.com/lestrrat-go/jwx/jwt"
"github.com/sirupsen/logrus"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
@ -27,47 +27,50 @@ var loginCmd = &cobra.Command{
Short: "Log in with identity provider for access token",
Long: "",
Run: func(cmd *cobra.Command, args []string) {
// make application logger
l := log.NewLogger(logrus.New(), logrus.DebugLevel)
// check if we have a valid JWT before starting login
if !forceLogin {
// try getting the access token from env var
testToken, err := LoadAccessToken()
testToken, err := util.LoadAccessToken(tokenPath)
if err != nil {
l.Log.Errorf("failed to load access token: %v", err)
log.Error().Err(err).Msgf("failed to load access token")
}
// parse into jwt.Token to validate
token, err := jwt.Parse([]byte(testToken))
if err != nil {
fmt.Printf("failed to parse access token contents: %v\n", err)
log.Error().Err(err).Msgf("failed to parse access token contents")
return
}
// check if the token is invalid and we need a new one
err = jwt.Validate(token)
if err != nil {
fmt.Printf("failed to validate access token...fetching a new one")
log.Error().Err(err).Msgf("failed to validate access token...fetching a new one")
} else {
fmt.Printf("found a valid token...skipping login (use the '-f/--force' flag to login anyway)")
log.Printf("found a valid token...skipping login (use the '-f/--force' flag to login anyway)")
return
}
}
if verbose {
log.Printf("Listening for token on %s:%d", targetHost, targetPort)
}
// start the login flow
var err error
accessToken, err = magellan.Login(loginUrl, targetHost, targetPort)
if errors.Is(err, http.ErrServerClosed) {
fmt.Printf("\n=========================================\nServer closed.\n=========================================\n\n")
if verbose {
fmt.Printf("\n=========================================\nServer closed.\n=========================================\n\n")
}
} else if err != nil {
fmt.Printf("failed to start server: %v\n", err)
log.Error().Err(err).Msgf("failed to start server")
}
// if we got a new token successfully, save it to the token path
if accessToken != "" && tokenPath != "" {
err := os.WriteFile(tokenPath, []byte(accessToken), os.ModePerm)
if err != nil {
fmt.Printf("failed to write access token to file: %v\n", err)
log.Error().Err(err).Msgf("failed to write access token to file")
}
}
},