Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions cmd/auth_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package cmd

import (
"fmt"
"strings"

"github.com/loops-so/cli/internal/api"
"github.com/loops-so/cli/internal/config"
"github.com/spf13/cobra"
)
Expand All @@ -11,23 +13,45 @@ var statusCmd = &cobra.Command{
Use: "status",
Short: "Print the resolved configuration",
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := runAuthStatus()
cfg, keyResp, err := runAuthStatus()
if err != nil {
return err
}

masked := maskKey(cfg.APIKey)

if isJSONOutput() {
return printJSON(cmd.OutOrStdout(), cfg)
return printJSON(cmd.OutOrStdout(), struct {
APIKey string `json:"apiKey"`
EndpointURL string `json:"endpointUrl"`
TeamName string `json:"teamName"`
}{masked, cfg.EndpointURL, keyResp.TeamName})
}

fmt.Fprintf(cmd.OutOrStdout(), "API Key: %s\n", cfg.APIKey)
fmt.Fprintf(cmd.OutOrStdout(), "API Key: %s\n", masked)
fmt.Fprintf(cmd.OutOrStdout(), "Endpoint: %s\n", cfg.EndpointURL)
fmt.Fprintf(cmd.OutOrStdout(), "Team: %s\n", keyResp.TeamName)
return nil
},
}

func runAuthStatus() (*config.Config, error) {
return config.Load()
func runAuthStatus() (*config.Config, *api.APIKeyResponse, error) {
cfg, err := config.Load()
if err != nil {
return nil, nil, err
}
keyResp, err := api.NewClient(cfg.EndpointURL, cfg.APIKey).GetAPIKey()
if err != nil {
return nil, nil, fmt.Errorf("API key verification failed: %w", err)
}
return cfg, keyResp, nil
}

func maskKey(key string) string {
if len(key) <= 4 {
return "****"
}
return fmt.Sprintf("%s%s", strings.Repeat("*", len(key)-4), key[len(key)-4:])
}

func init() {
Expand Down
19 changes: 15 additions & 4 deletions cmd/auth_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
)

func TestRunAuthStatus(t *testing.T) {
t.Run("returns config", func(t *testing.T) {
serveJSON(t, http.StatusOK, `{}`)
cfg, err := runAuthStatus()
t.Run("returns config and team name", func(t *testing.T) {
serveJSON(t, http.StatusOK, `{"teamName":"Acme"}`)
cfg, keyResp, err := runAuthStatus()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand All @@ -20,11 +20,22 @@ func TestRunAuthStatus(t *testing.T) {
if cfg.EndpointURL == "" {
t.Error("expected EndpointURL to be set")
}
if keyResp.TeamName != "Acme" {
t.Errorf("got team %q, want %q", keyResp.TeamName, "Acme")
}
})

t.Run("returns error when no key set", func(t *testing.T) {
keyring.MockInit()
_, err := runAuthStatus()
_, _, err := runAuthStatus()
if err == nil {
t.Fatal("expected error, got nil")
}
})

t.Run("returns error on api failure", func(t *testing.T) {
serveJSON(t, http.StatusUnauthorized, `{"error":"Invalid API key"}`)
_, _, err := runAuthStatus()
if err == nil {
t.Fatal("expected error, got nil")
}
Expand Down
Loading