Skip to content

Commit ce8ce82

Browse files
author
Bruce Hill
committed
Working version of fast diagnostics
1 parent e9f426c commit ce8ce82

File tree

5 files changed

+259
-54
lines changed

5 files changed

+259
-54
lines changed

go.mod

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
module github.com/stainless-api/stainless-api-cli
22

3-
go 1.24.4
4-
5-
toolchain go1.24.7
3+
go 1.25
64

75
require (
86
github.com/charmbracelet/bubbles v0.21.0
@@ -12,6 +10,7 @@ require (
1210
github.com/charmbracelet/lipgloss v1.1.1-0.20250404203927-76690c660834
1311
github.com/charmbracelet/x/ansi v0.8.0
1412
github.com/charmbracelet/x/term v0.2.1
13+
github.com/fsnotify/fsnotify v1.9.0
1514
github.com/itchyny/json2yaml v0.1.4
1615
github.com/logrusorgru/aurora/v4 v4.0.0
1716
github.com/muesli/reflow v0.3.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkp
6060
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
6161
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
6262
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=
63+
github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
64+
github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
6365
github.com/gorilla/css v1.0.1 h1:ntNaBIghp6JmvWnxbZKANoLyuXTPZ4cAMlo6RyhlbO8=
6466
github.com/gorilla/css v1.0.1/go.mod h1:BvnYkspnSzMmwRK+b8/xgNPLiIuNZr6vbZBTPQ2A3b0=
6567
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=

pkg/cmd/dev.go

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type fetchBuildMsg *stainless.Build
4747
type fetchDiagnosticsMsg []stainless.BuildDiagnostic
4848
type errorMsg error
4949
type downloadMsg stainless.Target
50+
type fileChangeMsg struct{}
5051

5152
func NewBuildModel(cc *apiCommandContext, ctx context.Context, branch string, fn func() (*stainless.Build, error)) BuildModel {
5253
return BuildModel{
@@ -160,6 +161,10 @@ func (m BuildModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
160161
case errorMsg:
161162
m.err = msg
162163
cmds = append(cmds, tea.Quit)
164+
165+
case fileChangeMsg:
166+
// File change detected, exit with success
167+
cmds = append(cmds, tea.Quit)
163168
}
164169
return m, tea.Sequence(cmds...)
165170
}
@@ -367,6 +372,9 @@ func runPreview(ctx context.Context, cmd *cli.Command) error {
367372
if hasBlockingDiagnostic(diagnostics) {
368373
fmt.Println("\nDiagnostic checks will re-run once you edit your configuration files...")
369374
if err := waitTillConfigChanges(ctx, cmd, cc); err != nil {
375+
if errors.Is(err, ErrUserCancelled) {
376+
return nil
377+
}
370378
return err
371379
}
372380
continue
@@ -400,32 +408,46 @@ func waitTillConfigChanges(ctx context.Context, cmd *cli.Command, cc *apiCommand
400408
stainlessConfigPath = cmd.String("stainless-config")
401409
}
402410

403-
files := []string{openapiSpecPath, stainlessConfigPath}
404-
lastModified := make(map[string]time.Time)
405-
for _, file := range files {
406-
if stat, err := os.Stat(file); err == nil {
407-
lastModified[file] = stat.ModTime()
408-
}
411+
// Get initial file modification times
412+
openapiSpecInfo, err := os.Stat(openapiSpecPath)
413+
if err != nil {
414+
return fmt.Errorf("failed to get file info for %s: %v", openapiSpecPath, err)
415+
}
416+
openapiSpecModTime := openapiSpecInfo.ModTime()
417+
418+
stainlessConfigInfo, err := os.Stat(stainlessConfigPath)
419+
if err != nil {
420+
return fmt.Errorf("failed to get file info for %s: %v", stainlessConfigPath, err)
409421
}
422+
stainlessConfigModTime := stainlessConfigInfo.ModTime()
423+
424+
fmt.Println("Waiting for file changes...")
425+
426+
// Poll for file changes every 250ms
427+
ticker := time.NewTicker(250 * time.Millisecond)
428+
defer ticker.Stop()
410429

411430
for {
412-
time.Sleep(500 * time.Millisecond)
431+
select {
432+
case <-ticker.C:
433+
// Check OpenAPI spec file
434+
if info, err := os.Stat(openapiSpecPath); err == nil {
435+
if info.ModTime().After(openapiSpecModTime) {
436+
return nil
437+
}
438+
}
413439

414-
for _, file := range files {
415-
if stat, err := os.Stat(file); err == nil {
416-
if stat.ModTime().After(lastModified[file]) {
440+
// Check Stainless config file
441+
if info, err := os.Stat(stainlessConfigPath); err == nil {
442+
if info.ModTime().After(stainlessConfigModTime) {
417443
return nil
418444
}
419445
}
420-
}
421-
// Check for cancellation
422-
select {
446+
423447
case <-ctx.Done():
424448
return ctx.Err()
425-
default:
426449
}
427450
}
428-
return nil
429451
}
430452

431453
func runDevBuild(ctx context.Context, cc *apiCommandContext, cmd *cli.Command, branch string, languages []stainless.Target) error {
@@ -496,6 +518,15 @@ func getCurrentGitBranch() (string, error) {
496518
return branch, nil
497519
}
498520

521+
type GenerateSpecParams struct {
522+
Project string `json:"project"`
523+
Source struct {
524+
Type string `json:"type"`
525+
OpenAPISpec string `json:"openapi_spec"`
526+
StainlessConfig string `json:"stainless_config"`
527+
} `json:"source"`
528+
}
529+
499530
func getDiagnostics(ctx context.Context, cmd *cli.Command, cc *apiCommandContext) ([]stainless.BuildDiagnostic, error) {
500531
var specParams GenerateSpecParams
501532
if cmd.IsSet("project") {

pkg/cmd/dev_view.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ func ViewDiagnosticsPrint(diagnostics []stainless.BuildDiagnostic, maxDiagnostic
394394

395395
s.WriteString(SProperty(0, "diagnostics", summary))
396396
s.WriteString(lipgloss.NewStyle().
397-
Padding(1).
397+
Padding(0).
398398
Border(lipgloss.RoundedBorder()).
399399
BorderForeground(lipgloss.Color("208")).
400400
Render(strings.TrimRight(sub.String(), "\n")),

0 commit comments

Comments
 (0)