From 63f66240eda0b4ab394fbecbaca476dc18210fba Mon Sep 17 00:00:00 2001 From: Mike Landau Date: Thu, 21 Sep 2023 13:42:13 -0700 Subject: [PATCH] [auth] change GetSession so it returns ok (#158) ## Summary This will make code more readable and reduce mistakes when using library. An invalid token is not super useful to client. Updated error messages for new interface. I still think an error is better (can help with refresh failures, etc) but @loreto you mentioned preferring this. ## How was it tested? Untested. --- internal/envcli/auth.go | 8 ++++---- internal/envcli/flags.go | 5 +++-- internal/envcli/init.go | 5 ++++- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/internal/envcli/auth.go b/internal/envcli/auth.go index ca51f47..9144d90 100644 --- a/internal/envcli/auth.go +++ b/internal/envcli/auth.go @@ -6,6 +6,7 @@ package envcli import ( "fmt" + "github.com/pkg/errors" "github.com/spf13/cobra" "go.jetpack.io/envsec/internal/envvar" "go.jetpack.io/pkg/sandbox/auth" @@ -101,10 +102,9 @@ func whoAmICmd() *cobra.Command { return err } - tok := client.GetSession() - if tok == nil { - fmt.Fprintln(cmd.OutOrStdout(), "Not logged in") - return nil + tok, ok := client.GetSession() + if !ok { + return errors.New("not logged in. Run `envsec auth login` to log in") } idClaims := tok.IDClaims() diff --git a/internal/envcli/flags.go b/internal/envcli/flags.go index d8c3bf5..70075a0 100644 --- a/internal/envcli/flags.go +++ b/internal/envcli/flags.go @@ -81,6 +81,7 @@ type cmdConfig struct { func (f *configFlags) genConfig(ctx context.Context) (*cmdConfig, error) { var tok *session.Token + var ok bool var err error if f.orgID == "" { @@ -89,8 +90,8 @@ func (f *configFlags) genConfig(ctx context.Context) (*cmdConfig, error) { return nil, err } - tok = client.GetSession() - if tok == nil { + tok, ok = client.GetSession() + if !ok { return nil, errors.Errorf( "To use envsec you must log in (`envsec auth login`) or specify --project-id and --org-id", ) diff --git a/internal/envcli/init.go b/internal/envcli/init.go index 1acc90f..68540d6 100644 --- a/internal/envcli/init.go +++ b/internal/envcli/init.go @@ -19,7 +19,10 @@ func initCmd() *cobra.Command { if err != nil { return err } - tok := client.GetSession() + tok, ok := client.GetSession() + if !ok { + return errors.New("not logged in, run `envsec auth login`") + } wd, err := os.Getwd() if err != nil {