From 9a40a122be3a668036361ac21a29ae78379b8ea4 Mon Sep 17 00:00:00 2001 From: kevin olson Date: Tue, 19 Mar 2024 17:17:36 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20centralizing=20progress=20-=20st?= =?UTF-8?q?ill=20deciding=20on=20error=20flow?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/cmd/auth/logout/logout.go | 8 +++----- pkg/cmd/auth/status/status.go | 6 +----- pkg/cmd/root/root.go | 21 +++++++++++++++++---- pkg/config/config.go | 21 +++++++++++---------- pkg/session/session.go | 14 ++++++++++++-- pkg/ui/ui.go | 4 ++-- 6 files changed, 46 insertions(+), 28 deletions(-) diff --git a/pkg/cmd/auth/logout/logout.go b/pkg/cmd/auth/logout/logout.go index 994921a..3bd26a7 100644 --- a/pkg/cmd/auth/logout/logout.go +++ b/pkg/cmd/auth/logout/logout.go @@ -1,10 +1,12 @@ package logout import ( + "errors" "github.com/spf13/cobra" "github.com/vulncheck-oss/cli/pkg/config" "github.com/vulncheck-oss/cli/pkg/session" "github.com/vulncheck-oss/cli/pkg/ui" + "github.com/vulncheck-oss/sdk" ) func Command() *cobra.Command { @@ -13,10 +15,6 @@ func Command() *cobra.Command { Short: "Invalidate and remove your current authentication token", RunE: func(cmd *cobra.Command, args []string) error { - if !config.HasConfig() { - return ui.Danger("No configuration file found") - } - if !config.HasToken() { return ui.Danger("No valid token was found") } @@ -28,7 +26,7 @@ func Command() *cobra.Command { } return ui.Success("Token successfully invalidated") } - if err.Error() == session.ErrorUnauthorized { + if errors.Is(err, sdk.ErrorUnauthorized) { if err := config.RemoveToken(); err != nil { return ui.Danger("Token was invalid, removing from config") } diff --git a/pkg/cmd/auth/status/status.go b/pkg/cmd/auth/status/status.go index 9fcc44f..4a0ab2a 100644 --- a/pkg/cmd/auth/status/status.go +++ b/pkg/cmd/auth/status/status.go @@ -16,12 +16,8 @@ func Command() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { config.Init() - if !config.HasConfig() { - return ui.Danger("No configuration found. Please run `vc auth login` to authenticate.") - } - if !config.HasToken() { - return ui.Danger("No token found. Please run `vc auth login` to authenticate.") + return ui.Danger("No token found. Please run `vc auth login` to authenticate or populate the environment variable `VC_TOKEN`.") } token := config.Token() diff --git a/pkg/cmd/root/root.go b/pkg/cmd/root/root.go index 82ff54f..856ae85 100644 --- a/pkg/cmd/root/root.go +++ b/pkg/cmd/root/root.go @@ -2,6 +2,7 @@ package root import ( _ "embed" + "errors" "fmt" "github.com/MakeNowJust/heredoc/v2" "github.com/spf13/cobra" @@ -13,13 +14,23 @@ import ( "github.com/vulncheck-oss/cli/pkg/config" "github.com/vulncheck-oss/cli/pkg/environment" "github.com/vulncheck-oss/cli/pkg/session" - "os" + "github.com/vulncheck-oss/cli/pkg/ui" + "github.com/vulncheck-oss/sdk" ) type AuthError struct { err error } +type exitCode int + +const ( + exitOK exitCode = 0 + exitError exitCode = 1 + exitCancel exitCode = 2 + exitAuthError exitCode = 3 +) + func (ae *AuthError) Error() string { return ae.err.Error() } @@ -68,9 +79,11 @@ func NewCmdRoot() *cobra.Command { } func Execute() { - if err := NewCmdRoot().Execute(); err != nil { - fmt.Println(err) - os.Exit(1) + if errors.Is(err, sdk.ErrorUnauthorized) { + fmt.Println(ui.Danger("Error: %s, Try authenticating with: vc auth login", err.Error())) + } + // fmt.Println(err) + // os.Exit(1) } } diff --git a/pkg/config/config.go b/pkg/config/config.go index f06df08..7af38a3 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -72,6 +72,7 @@ func configDir() (string, error) { } func HasConfig() bool { + _, err := loadConfig() if err != nil { return false @@ -79,18 +80,14 @@ func HasConfig() bool { return true } -func HasToken() bool { - config, err := loadConfig() - if err != nil { - return false - } - if !ValidToken(config.Token) { - return false +func Token() string { + + token := os.Getenv("VC_TOKEN") + + if token != "" && ValidToken(token) { + return token } - return true -} -func Token() string { config, err := loadConfig() if err != nil { return "" @@ -98,6 +95,10 @@ func Token() string { return config.Token } +func HasToken() bool { + return Token() != "" +} + func SaveToken(token string) error { config := &Config{ Token: token, diff --git a/pkg/session/session.go b/pkg/session/session.go index 637172b..610b84c 100644 --- a/pkg/session/session.go +++ b/pkg/session/session.go @@ -2,8 +2,10 @@ package session import ( "github.com/spf13/cobra" + "github.com/vulncheck-oss/cli/pkg/config" "github.com/vulncheck-oss/cli/pkg/environment" "github.com/vulncheck-oss/sdk" + "os" ) type MeResponse struct { @@ -18,9 +20,17 @@ type Me struct { Avatar string } -var ErrorUnauthorized = "errors: [Unauthorized]" - func CheckAuth() bool { + token := os.Getenv("VC_TOKEN") + if token != "" && config.ValidToken(token) { + return true + } + if token != "" && !config.ValidToken(token) { + return false + } + + token = config.Token() + return true } diff --git a/pkg/ui/ui.go b/pkg/ui/ui.go index 45b4bdc..9c6406c 100644 --- a/pkg/ui/ui.go +++ b/pkg/ui/ui.go @@ -30,10 +30,10 @@ func Info(str string) error { return nil } -func Danger(sr string) error { +func Danger(str string, a ...any) error { return fmt.Errorf( format, Red.Render("✗"), - White.Render(sr), + White.Render(fmt.Sprintf(str, a)), ) }