Skip to content

Commit e9f426c

Browse files
author
Bruce Hill
committed
Code cleanup
1 parent 796a20a commit e9f426c

File tree

2 files changed

+63
-102
lines changed

2 files changed

+63
-102
lines changed

pkg/cmd/dev.go

Lines changed: 33 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -265,27 +265,13 @@ var devCommand = cli.Command{
265265
Value: false,
266266
Usage: "Run in 'watch' mode to loop and rebuild when files change.",
267267
},
268-
&cli.BoolFlag{
269-
Name: "lint",
270-
Aliases: []string{"l"},
271-
Value: false,
272-
Usage: "Only check for diagnostic errors without running a full build.",
273-
},
274268
},
275269
Action: runPreview,
276270
}
277271

278272
func runPreview(ctx context.Context, cmd *cli.Command) error {
279273
cc := getAPICommandContext(cmd)
280274

281-
openapiSpecPath := cmd.String("openapi-spec")
282-
stainlessConfigPath := cmd.String("stainless-config")
283-
284-
// If only linting is requested, run in lint-only mode
285-
if cmd.Bool("lint") {
286-
return runLintLoop(ctx, cmd)
287-
}
288-
289275
gitUser, err := getGitUsername()
290276
if err != nil {
291277
Warn("Couldn't get a git user: %s", err)
@@ -366,7 +352,7 @@ func runPreview(ctx context.Context, cmd *cli.Command) error {
366352
for {
367353
// Keep checking diagnostics until they're all fixed
368354
for {
369-
diagnostics, err := getPreviewDiagnostics(ctx, cmd)
355+
diagnostics, err := getDiagnostics(ctx, cmd, cc)
370356
if err != nil {
371357
if errors.Is(err, ErrUserCancelled) {
372358
return nil
@@ -380,7 +366,7 @@ func runPreview(ctx context.Context, cmd *cli.Command) error {
380366

381367
if hasBlockingDiagnostic(diagnostics) {
382368
fmt.Println("\nDiagnostic checks will re-run once you edit your configuration files...")
383-
if err := waitTillAnyFileChanged(ctx, []string{openapiSpecPath, stainlessConfigPath}); err != nil {
369+
if err := waitTillConfigChanges(ctx, cmd, cc); err != nil {
384370
return err
385371
}
386372
continue
@@ -404,35 +390,17 @@ func runPreview(ctx context.Context, cmd *cli.Command) error {
404390
return nil
405391
}
406392

407-
// runLintLoop handles linting in a loop for watch mode
408-
func runLintLoop(ctx context.Context, cmd *cli.Command) error {
409-
// Get the initial file modification times
410-
openapiSpecPath := cmd.String("openapi-spec")
411-
stainlessConfigPath := cmd.String("stainless-config")
412-
413-
for {
414-
diagnostics, err := getPreviewDiagnosticsJSON(ctx, cmd)
415-
if err != nil {
416-
if errors.Is(err, ErrUserCancelled) {
417-
return nil
418-
}
419-
return err
420-
}
421-
jsonObj := gjson.Parse(diagnostics.Raw)
422-
ShowJSON("Diagnostics", jsonObj, cmd.String("format"), cmd.String("transform"))
423-
424-
if !cmd.Bool("watch") {
425-
break
426-
}
427-
428-
if err := waitTillAnyFileChanged(ctx, []string{openapiSpecPath, stainlessConfigPath}); err != nil {
429-
return err
430-
}
393+
func waitTillConfigChanges(ctx context.Context, cmd *cli.Command, cc *apiCommandContext) error {
394+
openapiSpecPath := cc.workspaceConfig.OpenAPISpec
395+
if cmd.IsSet("openapi-spec") {
396+
openapiSpecPath = cmd.String("openapi-spec")
397+
}
398+
stainlessConfigPath := cc.workspaceConfig.StainlessConfig
399+
if cmd.IsSet("stainless-config") {
400+
stainlessConfigPath = cmd.String("stainless-config")
431401
}
432-
return nil
433-
}
434402

435-
func waitTillAnyFileChanged(ctx context.Context, files []string) error {
403+
files := []string{openapiSpecPath, stainlessConfigPath}
436404
lastModified := make(map[string]time.Time)
437405
for _, file := range files {
438406
if stat, err := os.Stat(file); err == nil {
@@ -528,19 +496,32 @@ func getCurrentGitBranch() (string, error) {
528496
return branch, nil
529497
}
530498

531-
func getPreviewDiagnosticsJSON(ctx context.Context, cmd *cli.Command) (*gjson.Result, error) {
532-
cc := getAPICommandContext(cmd)
499+
func getDiagnostics(ctx context.Context, cmd *cli.Command, cc *apiCommandContext) ([]stainless.BuildDiagnostic, error) {
533500
var specParams GenerateSpecParams
534-
specParams.Project = cmd.String("project")
501+
if cmd.IsSet("project") {
502+
specParams.Project = cmd.String("project")
503+
} else {
504+
specParams.Project = cc.workspaceConfig.Project
505+
}
535506
specParams.Source.Type = "upload"
536507

537-
stainlessConfig, err := os.ReadFile(cmd.String("stainless-config"))
508+
configPath := cc.workspaceConfig.StainlessConfig
509+
if cmd.IsSet("stainless-config") {
510+
configPath = cmd.String("stainless-config")
511+
}
512+
513+
stainlessConfig, err := os.ReadFile(configPath)
538514
if err != nil {
539515
return nil, err
540516
}
541517
specParams.Source.StainlessConfig = string(stainlessConfig)
542518

543-
openAPISpec, err := os.ReadFile(cmd.String("openapi-spec"))
519+
oasPath := cc.workspaceConfig.OpenAPISpec
520+
if cmd.IsSet("openapi-spec") {
521+
oasPath = cmd.String("openapi-spec")
522+
}
523+
524+
openAPISpec, err := os.ReadFile(oasPath)
544525
if err != nil {
545526
return nil, err
546527
}
@@ -551,21 +532,12 @@ func getPreviewDiagnosticsJSON(ctx context.Context, cmd *cli.Command) (*gjson.Re
551532
if err != nil {
552533
return nil, err
553534
}
554-
json := gjson.ParseBytes(result)
555-
diagnostics := json.Get("spec.diagnostics.@values.@flatten")
556-
return &diagnostics, nil
557-
}
558535

559-
func getPreviewDiagnostics(ctx context.Context, cmd *cli.Command) ([]stainless.BuildDiagnostic, error) {
560-
jsonDiagnostics, err := getPreviewDiagnosticsJSON(ctx, cmd)
561-
if err != nil {
562-
return nil, err
563-
}
536+
transform := "spec.diagnostics.@values.@flatten.#(ignored==false)#"
537+
jsonObj := gjson.Parse(string(result)).Get(transform)
564538
var diagnostics []stainless.BuildDiagnostic
565-
if err := json.Unmarshal([]byte(jsonDiagnostics.Raw), &diagnostics); err != nil {
566-
return nil, err
567-
}
568-
return diagnostics, err
539+
json.Unmarshal([]byte(jsonObj.Raw), &diagnostics)
540+
return diagnostics, nil
569541
}
570542

571543
func hasBlockingDiagnostic(diagnostics []stainless.BuildDiagnostic) bool {

pkg/cmd/lint.go

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"fmt"
77
"os"
88

9-
"github.com/stainless-api/stainless-api-go"
10-
"github.com/stainless-api/stainless-api-go/option"
119
"github.com/tidwall/gjson"
1210
"github.com/urfave/cli/v3"
1311
)
@@ -31,6 +29,11 @@ var lintCommand = cli.Command{
3129
Aliases: []string{"config"},
3230
Usage: "Path to Stainless config file",
3331
},
32+
&cli.BoolFlag{
33+
Name: "watch",
34+
Aliases: []string{"w"},
35+
Usage: "Watch for files to change and re-run linting",
36+
},
3437
},
3538
Action: runLinter,
3639
}
@@ -46,51 +49,37 @@ type GenerateSpecParams struct {
4649

4750
func runLinter(ctx context.Context, cmd *cli.Command) error {
4851
cc := getAPICommandContext(cmd)
49-
var specParams GenerateSpecParams
50-
specParams.Project = cmd.String("project")
51-
specParams.Source.Type = "upload"
52-
53-
stainlessConfig, err := os.ReadFile(cmd.String("stainless-config"))
54-
if err != nil {
55-
return err
56-
}
57-
specParams.Source.StainlessConfig = string(stainlessConfig)
58-
59-
openAPISpec, err := os.ReadFile(cmd.String("openapi-spec"))
60-
if err != nil {
61-
return err
62-
}
63-
specParams.Source.OpenAPISpec = string(openAPISpec)
64-
65-
var result []byte
66-
err = cc.client.Post(ctx, "api/generate/spec", specParams, &result, option.WithMiddleware(cc.AsMiddleware()))
67-
if err != nil {
68-
return err
69-
}
70-
71-
transform := "spec.diagnostics.@values.@flatten.#(ignored==false)#"
72-
jsonObj := gjson.Parse(string(result)).Get(transform)
73-
var diagnostics []stainless.BuildDiagnostic
74-
json.Unmarshal([]byte(jsonObj.Raw), &diagnostics)
75-
if cmd.IsSet("format") {
76-
if err := ShowJSON("Diagnostics", jsonObj, cmd.String("format"), ""); err != nil {
52+
for {
53+
diagnostics, err := getDiagnostics(ctx, cmd, cc)
54+
if err != nil {
7755
return err
7856
}
79-
} else {
80-
fmt.Println(ViewDiagnosticsPrint(diagnostics, -1))
81-
}
8257

83-
for _, d := range diagnostics {
84-
if !d.Ignored {
85-
switch d.Level {
86-
case stainless.BuildDiagnosticLevelFatal:
87-
case stainless.BuildDiagnosticLevelError:
88-
case stainless.BuildDiagnosticLevelWarning:
58+
if cmd.IsSet("format") {
59+
rawJson, err := json.Marshal(diagnostics)
60+
if err != nil {
61+
return err
62+
}
63+
jsonObj := gjson.Parse(string(rawJson))
64+
if err := ShowJSON("Diagnostics", jsonObj, cmd.String("format"), ""); err != nil {
65+
return err
66+
}
67+
} else {
68+
fmt.Println(ViewDiagnosticsPrint(diagnostics, -1))
69+
}
70+
71+
if cmd.Bool("watch") {
72+
fmt.Println("\nDiagnostic checks will re-run once you edit your configuration files...")
73+
if err := waitTillConfigChanges(ctx, cmd, cc); err != nil {
74+
return err
75+
}
76+
} else {
77+
if hasBlockingDiagnostic(diagnostics) {
8978
os.Exit(1)
90-
case stainless.BuildDiagnosticLevelNote:
91-
continue
9279
}
80+
break
9381
}
9482
}
83+
9584
return nil
9685
}

0 commit comments

Comments
 (0)