Skip to content

Commit 09dcc17

Browse files
author
privapps
committed
Refactor code structure for improved readability and maintainability
1 parent b2d141f commit 09dcc17

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

internal/cli.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"os"
88
"time"
9+
"strings"
910
)
1011

1112
// Command constants to avoid goconst errors
@@ -113,6 +114,10 @@ func handleAuth() error {
113114
func handleStatusWithFormat(jsonOutput bool) error {
114115
cfg, err := LoadConfig()
115116
if err != nil {
117+
if strings.Contains(err.Error(), "either github_token or copilot_token must be provided") {
118+
fmt.Println("Not authenticated. Run 'auth' to authenticate.")
119+
return nil
120+
}
116121
return fmt.Errorf("failed to load config: %v", err)
117122
}
118123

@@ -210,6 +215,10 @@ func printStatusText(cfg *Config) error {
210215
func handleConfig() error {
211216
cfg, err := LoadConfig()
212217
if err != nil {
218+
if strings.Contains(err.Error(), "either github_token or copilot_token must be provided") {
219+
fmt.Println("Not authenticated. Run 'auth' to authenticate.")
220+
return nil
221+
}
213222
return fmt.Errorf("failed to load config: %v", err)
214223
}
215224

@@ -233,14 +242,29 @@ func handleConfig() error {
233242
return nil
234243
}
235244

245+
236246
func getCurrentTime() int64 {
237247
return time.Now().Unix()
238248
}
239249

240250
func handleRun() error {
241251
cfg, err := LoadConfig()
242252
if err != nil {
243-
return fmt.Errorf("failed to load config: %v", err)
253+
// If config validation failed due to missing tokens, trigger auth flow
254+
if strings.Contains(err.Error(), "either github_token or copilot_token must be provided") {
255+
fmt.Println("No valid token found. Starting authentication flow...")
256+
cfg = &Config{Port: defaultServerPort}
257+
SetDefaultTimeouts(cfg)
258+
SetDefaultHeaders(cfg)
259+
SetDefaultCORS(cfg)
260+
httpClient := CreateHTTPClient(cfg)
261+
authService := NewAuthService(httpClient)
262+
if authErr := authService.Authenticate(cfg); authErr != nil {
263+
return fmt.Errorf("authentication failed: %v", authErr)
264+
}
265+
} else {
266+
return fmt.Errorf("failed to load config: %v", err)
267+
}
244268
}
245269

246270
// Create HTTP client and auth service

0 commit comments

Comments
 (0)