Skip to content

Commit d988b4f

Browse files
committed
refactor: extract console
1 parent 912892e commit d988b4f

File tree

9 files changed

+68
-54
lines changed

9 files changed

+68
-54
lines changed

pkg/cmd/auth.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"strings"
1313
"time"
1414

15+
"github.com/stainless-api/stainless-api-cli/pkg/console"
16+
1517
"github.com/pkg/browser"
1618
"github.com/stainless-api/stainless-api-go"
1719
"github.com/stainless-api/stainless-api-go/option"
@@ -58,7 +60,7 @@ func authenticate(ctx context.Context, cmd *cli.Command, forceAuthentication boo
5860

5961
config, err := NewAuthConfig()
6062
if err != nil {
61-
Error("Failed to create config: %v", err)
63+
console.Error("Failed to create config: %v", err)
6264
return fmt.Errorf("authentication failed")
6365
}
6466

@@ -81,10 +83,10 @@ func authenticate(ctx context.Context, cmd *cli.Command, forceAuthentication boo
8183
config.TokenType = authResult.TokenType
8284

8385
if err := config.Save(); err != nil {
84-
Error("Failed to save authentication: %v", err)
86+
console.Error("Failed to save authentication: %v", err)
8587
return fmt.Errorf("authentication failed")
8688
}
87-
Success("Authentication successful! Your credentials have been saved to %s", config.ConfigPath)
89+
console.Success("Authentication successful! Your credentials have been saved to %s", config.ConfigPath)
8890
return nil
8991
}
9092

@@ -100,22 +102,22 @@ func handleAuthLogout(ctx context.Context, cmd *cli.Command) error {
100102
}
101103

102104
if !found {
103-
Warn("No active session found.")
105+
console.Warn("No active session found.")
104106
return nil
105107
}
106108

107109
if err := config.Remove(); err != nil {
108110
return fmt.Errorf("failed to remove auth file: %v", err)
109111
}
110112

111-
Success("Successfully logged out.")
113+
console.Success("Successfully logged out.")
112114
return nil
113115
}
114116

115117
func handleAuthStatus(ctx context.Context, cmd *cli.Command) error {
116118
// Check for API key in environment variables first
117119
if apiKey := os.Getenv("STAINLESS_API_KEY"); apiKey != "" {
118-
Success("Authenticated via STAINLESS_API_KEY environment variable")
120+
console.Success("Authenticated via STAINLESS_API_KEY environment variable")
119121
return nil
120122
}
121123

@@ -127,12 +129,12 @@ func handleAuthStatus(ctx context.Context, cmd *cli.Command) error {
127129
}
128130

129131
if !found || config.AccessToken == "" {
130-
Warn("Not logged in.")
132+
console.Warn("Not logged in.")
131133
return nil
132134
}
133135

134136
// If we have a config file with a token
135-
group := Success("Authenticated via saved credentials")
137+
group := console.Success("Authenticated via saved credentials")
136138

137139
// Show a truncated version of the token for verification
138140
if len(config.AccessToken) > 10 {
@@ -164,7 +166,7 @@ func startDeviceFlow(ctx context.Context, cmd *cli.Command, client stainless.Cli
164166
return nil, err
165167
}
166168

167-
group := Info("To authenticate, visit the verification URL")
169+
group := console.Info("To authenticate, visit the verification URL")
168170
group.Property("url", deviceResponse.VerificationURIComplete)
169171
group.Property("code", deviceResponse.UserCode)
170172

@@ -195,7 +197,7 @@ func pollForToken(ctx context.Context, client stainless.Client, clientID, device
195197
deadline := time.Now().Add(time.Duration(expiresIn) * time.Second)
196198
pollInterval := time.Duration(interval) * time.Second
197199

198-
Progress("Waiting for authentication to complete...")
200+
console.Progress("Waiting for authentication to complete...")
199201

200202
for time.Now().Before(deadline) {
201203
time.Sleep(pollInterval)

pkg/cmd/build.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"strings"
1616
"time"
1717

18+
"github.com/stainless-api/stainless-api-cli/pkg/console"
19+
1820
"github.com/stainless-api/stainless-api-cli/pkg/jsonflag"
1921
"github.com/stainless-api/stainless-api-cli/pkg/stainlessutils"
2022
"github.com/stainless-api/stainless-api-go"
@@ -174,7 +176,7 @@ func isTargetCompleted(status stainless.BuildTargetStatus) bool {
174176
}
175177

176178
// waitForBuildCompletion polls a build until completion and shows progress updates
177-
func waitForBuildCompletion(ctx context.Context, client stainless.Client, build *stainless.Build, waitGroup *Group) (*stainless.Build, error) {
179+
func waitForBuildCompletion(ctx context.Context, client stainless.Client, build *stainless.Build, waitGroup *console.Group) (*stainless.Build, error) {
178180
ticker := time.NewTicker(3 * time.Second)
179181
defer ticker.Stop()
180182

@@ -478,7 +480,7 @@ func handleBuildsCreate(ctx context.Context, cmd *cli.Command) error {
478480

479481
// Parse target paths using cached workspace config
480482
targetPaths := parseTargetPaths(cc.workspaceConfig)
481-
buildGroup := Info("Creating build...")
483+
buildGroup := console.Info("Creating build...")
482484
params := stainless.BuildNewParams{}
483485
build, err := cc.client.Builds.New(
484486
ctx,
@@ -492,7 +494,7 @@ func handleBuildsCreate(ctx context.Context, cmd *cli.Command) error {
492494
buildGroup.Property("build_id", build.ID)
493495

494496
if cmd.Bool("wait") {
495-
waitGroup := Info("Waiting for latest build to complete...")
497+
waitGroup := console.Info("Waiting for latest build to complete...")
496498

497499
build, err = waitForBuildCompletion(context.TODO(), cc.client, build, &waitGroup)
498500
if err != nil {
@@ -503,7 +505,7 @@ func handleBuildsCreate(ctx context.Context, cmd *cli.Command) error {
503505
shouldPull := cmd.Bool("pull") || (cc.HasWorkspaceTargets() && !cmd.IsSet("pull"))
504506

505507
if shouldPull {
506-
pullGroup := Info("Downloading build outputs...")
508+
pullGroup := console.Info("Downloading build outputs...")
507509
if err := pullBuildOutputs(context.TODO(), cc.client, *build, targetPaths, &pullGroup); err != nil {
508510
pullGroup.Error("Failed to download outputs: %v", err)
509511
} else {
@@ -560,7 +562,7 @@ func handleBuildsRetrieve(ctx context.Context, cmd *cli.Command) error {
560562
}
561563

562564
// pullBuildOutputs pulls the outputs for a completed build
563-
func pullBuildOutputs(ctx context.Context, client stainless.Client, res stainless.Build, targetPaths map[string]string, pullGroup *Group) error {
565+
func pullBuildOutputs(ctx context.Context, client stainless.Client, res stainless.Build, targetPaths map[string]string, pullGroup *console.Group) error {
564566
// Get all targets
565567
allTargets := getBuildTargetInfo(res)
566568

@@ -707,7 +709,7 @@ func extractFilename(urlStr string, resp *http.Response) string {
707709
}
708710

709711
// pullOutput handles downloading or cloning a build target output
710-
func pullOutput(output, url, ref, targetDir string, targetGroup *Group) error {
712+
func pullOutput(output, url, ref, targetDir string, targetGroup *console.Group) error {
711713
switch output {
712714
case "git":
713715
// Extract repository name from git URL for directory name

pkg/cmd/buildtargetoutput.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"context"
77
"fmt"
88

9+
"github.com/stainless-api/stainless-api-cli/pkg/console"
10+
911
"github.com/stainless-api/stainless-api-cli/pkg/jsonflag"
1012
"github.com/stainless-api/stainless-api-go"
1113
"github.com/stainless-api/stainless-api-go/option"
@@ -102,7 +104,7 @@ func handleBuildsTargetOutputsRetrieve(ctx context.Context, cmd *cli.Command) er
102104
return err
103105
}
104106

105-
group := Info("Downloading output")
107+
group := console.Info("Downloading output")
106108
if cmd.Bool("pull") {
107109
return pullOutput(res.Output, res.URL, res.Ref, "", &group)
108110
}

pkg/cmd/dev.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/charmbracelet/bubbles/key"
1717
tea "github.com/charmbracelet/bubbletea"
1818
"github.com/charmbracelet/huh"
19+
"github.com/stainless-api/stainless-api-cli/pkg/console"
1920
"github.com/stainless-api/stainless-api-cli/pkg/stainlessutils"
2021
"github.com/stainless-api/stainless-api-cli/pkg/stainlessviews"
2122
"github.com/stainless-api/stainless-api-go"
@@ -210,7 +211,7 @@ func (m BuildModel) downloadTarget(target stainless.Target) tea.Cmd {
210211
if err != nil {
211212
return errorMsg(err)
212213
}
213-
err = pullOutput(outputRes.Output, outputRes.URL, outputRes.Ref, m.downloads[target].Path, &Group{silent: true})
214+
err = pullOutput(outputRes.Output, outputRes.URL, outputRes.Ref, m.downloads[target].Path, &console.Group{})
214215
if err != nil {
215216
return errorMsg(err)
216217
}
@@ -318,7 +319,7 @@ func runPreview(ctx context.Context, cmd *cli.Command) error {
318319

319320
gitUser, err := getGitUsername()
320321
if err != nil {
321-
Warn("Couldn't get a git user: %s", err)
322+
console.Warn("Couldn't get a git user: %s", err)
322323
gitUser = "user"
323324
}
324325

@@ -331,7 +332,7 @@ func runPreview(ctx context.Context, cmd *cli.Command) error {
331332
return err
332333
}
333334
}
334-
Property("branch", selectedBranch)
335+
console.Property("branch", selectedBranch)
335336

336337
// Phase 2: Language selection
337338
var selectedTargets []string
@@ -351,7 +352,7 @@ func runPreview(ctx context.Context, cmd *cli.Command) error {
351352
return fmt.Errorf("no languages selected")
352353
}
353354

354-
Property("targets", strings.Join(selectedTargets, ", "))
355+
console.Property("targets", strings.Join(selectedTargets, ", "))
355356

356357
// Convert string targets to stainless.Target
357358
targets := make([]stainless.Target, len(selectedTargets))
@@ -384,8 +385,8 @@ func runPreview(ctx context.Context, cmd *cli.Command) error {
384385
// Clear the screen and move the cursor to the top
385386
fmt.Print("\nRebuilding...\n\n\033[2J\033[H")
386387
os.Stdout.Sync()
387-
Property("branch", selectedBranch)
388-
Property("targets", strings.Join(selectedTargets, ", "))
388+
console.Property("branch", selectedBranch)
389+
console.Property("targets", strings.Join(selectedTargets, ", "))
389390
}
390391
return nil
391392
}
@@ -417,7 +418,7 @@ func chooseBranch(gitUser string) (string, error) {
417418
Options(branchOptions...).
418419
Value(&selectedBranch),
419420
),
420-
).WithTheme(GetFormTheme(0))
421+
).WithTheme(console.GetFormTheme(0))
421422

422423
if err := branchForm.Run(); err != nil {
423424
return selectedBranch, fmt.Errorf("branch selection failed: %v", err)
@@ -438,7 +439,7 @@ func chooseSelectedTargets(targetInfos []TargetInfo) ([]string, error) {
438439
Options(targetOptions...).
439440
Value(&selectedTargets),
440441
),
441-
).WithTheme(GetFormTheme(0))
442+
).WithTheme(console.GetFormTheme(0))
442443

443444
if err := targetForm.Run(); err != nil {
444445
return nil, fmt.Errorf("target selection failed: %v", err)

0 commit comments

Comments
 (0)