Skip to content

Commit

Permalink
🚧 centralizing progress - still deciding on error flow
Browse files Browse the repository at this point in the history
  • Loading branch information
acidjazz committed Mar 19, 2024
1 parent d871446 commit 9a40a12
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 28 deletions.
8 changes: 3 additions & 5 deletions pkg/cmd/auth/logout/logout.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand Down
6 changes: 1 addition & 5 deletions pkg/cmd/auth/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
21 changes: 17 additions & 4 deletions pkg/cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package root

import (
_ "embed"
"errors"
"fmt"
"github.com/MakeNowJust/heredoc/v2"
"github.com/spf13/cobra"
Expand All @@ -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()
}
Expand Down Expand Up @@ -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)
}
}
21 changes: 11 additions & 10 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,32 +72,33 @@ func configDir() (string, error) {
}

func HasConfig() bool {

_, err := loadConfig()
if err != nil {
return false
}
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 ""
}
return config.Token
}

func HasToken() bool {
return Token() != ""
}

func SaveToken(token string) error {
config := &Config{
Token: token,
Expand Down
14 changes: 12 additions & 2 deletions pkg/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
)
}

0 comments on commit 9a40a12

Please sign in to comment.