Skip to content

Commit

Permalink
Show informative error if attempting to set target without login state
Browse files Browse the repository at this point in the history
  • Loading branch information
jhamon committed Jun 11, 2024
1 parent 65d5cd5 commit 717975b
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions internal/pkg/cli/command/target/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/charmbracelet/lipgloss"

"github.com/pinecone-io/cli/internal/pkg/dashboard"
"github.com/pinecone-io/cli/internal/pkg/utils/configuration/secrets"
"github.com/pinecone-io/cli/internal/pkg/utils/configuration/state"
"github.com/pinecone-io/cli/internal/pkg/utils/exit"
"github.com/pinecone-io/cli/internal/pkg/utils/help"
Expand Down Expand Up @@ -66,6 +67,26 @@ func NewTargetCmd() *cobra.Command {
return
}

// Print current target if no org, project, or knowledge model is specified
if options.show {
if options.json {
log.Info().Msg("Outputting target context as JSON")
text.PrettyPrintJSON(state.GetTargetContext())
return
}
log.Info().
Msg("Outputting target context as table")
presenters.PrintTargetContext(state.GetTargetContext())
return
}

access_token := secrets.OAuth2Token.Get()
if access_token.AccessToken == "" {
msg.FailMsg("You must be logged in to set a target context. Run %s to log in.", style.Code("pinecone login"))
exit.ErrorMsg("You must be logged in to set a target context")
}

// Interactive targeting if logged in
if options.Org == "" && options.Project == "" && !options.show {
// Fetch the user's organizations and projects
orgsResponse, err := dashboard.ListOrganizations()
Expand All @@ -86,19 +107,6 @@ func NewTargetCmd() *cobra.Command {
return
}

// Print current target if no org, project, or knowledge model is specified
if options.show {
if options.json {
log.Info().Msg("Outputting target context as JSON")
text.PrettyPrintJSON(state.GetTargetContext())
return
}
log.Info().
Msg("Outputting target context as table")
presenters.PrintTargetContext(state.GetTargetContext())
return
}

orgs, err := dashboard.ListOrganizations()
if err != nil {
msg.FailMsg("Failed to get organizations: %s", err)
Expand Down

0 comments on commit 717975b

Please sign in to comment.