Skip to content

Commit 2b9f5ae

Browse files
committed
refactor(cli): use stdlib, parallelize API calls, and remove dead exports
- Replace custom containsSubstring/findSubstring with strings.Contains - Run verifyToken and fetchUserInfo concurrently via sync.WaitGroup - Remove unused exported ParseError and GetErrorRecommendations wrappers
1 parent 7c81f93 commit 2b9f5ae

4 files changed

Lines changed: 31 additions & 38 deletions

File tree

config_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"strings"
45
"testing"
56

67
"github.com/go-authgate/sdk-go/credstore"
@@ -52,7 +53,7 @@ func TestNewTokenStore(t *testing.T) {
5253
if err == nil {
5354
t.Fatal("expected error, got nil")
5455
}
55-
if !containsSubstring(err.Error(), tc.errSubstr) {
56+
if !strings.Contains(err.Error(), tc.errSubstr) {
5657
t.Errorf("error %q should contain %q", err.Error(), tc.errSubstr)
5758
}
5859
return

main.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"os"
88
"os/signal"
9+
"sync"
910
"syscall"
1011
"time"
1112

@@ -116,18 +117,33 @@ func run(ctx context.Context, ui tui.Manager, cfg *AppConfig) int {
116117
// Display token info.
117118
ui.ShowTokenInfo(toTUITokenStorage(storage, flow, cfg.Store.String()))
118119

119-
// Verify token against server.
120-
info, err := verifyToken(ctx, cfg, storage.AccessToken)
121-
if err != nil {
122-
ui.ShowVerification(false, err.Error())
120+
// Verify token and fetch UserInfo in parallel (independent API calls).
121+
var (
122+
verifyInfo string
123+
verifyErr error
124+
userInfo *UserInfo
125+
userErr error
126+
wg sync.WaitGroup
127+
)
128+
wg.Add(2)
129+
go func() {
130+
defer wg.Done()
131+
verifyInfo, verifyErr = verifyToken(ctx, cfg, storage.AccessToken)
132+
}()
133+
go func() {
134+
defer wg.Done()
135+
userInfo, userErr = fetchUserInfo(ctx, cfg, storage.AccessToken)
136+
}()
137+
wg.Wait()
138+
139+
if verifyErr != nil {
140+
ui.ShowVerification(false, verifyErr.Error())
123141
} else {
124-
ui.ShowVerification(true, info)
142+
ui.ShowVerification(true, verifyInfo)
125143
}
126144

127-
// Fetch OIDC UserInfo profile claims (OIDC Core §5.3).
128-
userInfo, err := fetchUserInfo(ctx, cfg, storage.AccessToken)
129-
if err != nil {
130-
ui.ShowUserInfo(false, err.Error())
145+
if userErr != nil {
146+
ui.ShowUserInfo(false, userErr.Error())
131147
} else {
132148
ui.ShowUserInfo(true, formatUserInfo(userInfo))
133149
}

main_test.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http"
88
"net/http/httptest"
99
"path/filepath"
10+
"strings"
1011
"sync"
1112
"sync/atomic"
1213
"testing"
@@ -242,7 +243,7 @@ func TestBuildAuthURL_ContainsRequiredParams(t *testing.T) {
242243
"code_challenge=test-challenge",
243244
"code_challenge_method=S256",
244245
} {
245-
if !containsSubstring(u, want) {
246+
if !strings.Contains(u, want) {
246247
t.Errorf("auth URL missing %q\nURL: %s", want, u)
247248
}
248249
}
@@ -362,16 +363,3 @@ func TestRequestDeviceCode_WithRetry(t *testing.T) {
362363
// -----------------------------------------------------------------------
363364
// Helpers
364365
// -----------------------------------------------------------------------
365-
366-
func containsSubstring(s, sub string) bool {
367-
return len(s) >= len(sub) && findSubstring(s, sub)
368-
}
369-
370-
func findSubstring(s, sub string) bool {
371-
for i := 0; i <= len(s)-len(sub); i++ {
372-
if s[i:i+len(sub)] == sub {
373-
return true
374-
}
375-
}
376-
return false
377-
}

tui/error_parser.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,7 @@ var oauthErrorCodes = []string{
3333
"unsupported_grant_type",
3434
}
3535

36-
// ParseError parses an error message and extracts OAuth error information.
37-
// This is exported for testing purposes.
38-
func ParseError(err error) *ParsedError {
39-
return parseError(err)
40-
}
41-
42-
// parseError is the internal implementation
36+
// parseError parses an error message and extracts OAuth error information.
4337
func parseError(err error) *ParsedError {
4438
if err == nil {
4539
return nil
@@ -167,13 +161,7 @@ func extractCleanMessage(errMsg string) string {
167161
return cleaned
168162
}
169163

170-
// GetErrorRecommendations returns context-specific recommendations based on the parsed error.
171-
// This is exported for testing purposes.
172-
func GetErrorRecommendations(parsed *ParsedError) []string {
173-
return getErrorRecommendations(parsed)
174-
}
175-
176-
// getErrorRecommendations is the internal implementation
164+
// getErrorRecommendations returns context-specific recommendations based on the parsed error.
177165
func getErrorRecommendations(parsed *ParsedError) []string {
178166
if parsed == nil {
179167
return nil

0 commit comments

Comments
 (0)