Skip to content

Commit 00cb105

Browse files
committed
refactor: move getDiagnostics to cmd/lint.go
1 parent a057170 commit 00cb105

2 files changed

Lines changed: 61 additions & 59 deletions

File tree

pkg/cmd/dev.go

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"crypto/rand"
66
"encoding/base64"
7-
"encoding/json"
87
"errors"
98
"fmt"
109
"os"
@@ -20,7 +19,6 @@ import (
2019
"github.com/stainless-api/stainless-api-go"
2120
"github.com/stainless-api/stainless-api-go/option"
2221
"github.com/stainless-api/stainless-api-go/shared"
23-
"github.com/tidwall/gjson"
2422
"github.com/urfave/cli/v3"
2523
)
2624

@@ -266,63 +264,6 @@ type GenerateSpecParams struct {
266264
} `json:"source"`
267265
}
268266

269-
func getDiagnostics(ctx context.Context, cmd *cli.Command, client stainless.Client, wc workspace.Config) ([]stainless.BuildDiagnostic, error) {
270-
var specParams GenerateSpecParams
271-
if cmd.IsSet("project") {
272-
specParams.Project = cmd.String("project")
273-
} else {
274-
specParams.Project = wc.Project
275-
}
276-
specParams.Source.Type = "upload"
277-
278-
configPath := wc.StainlessConfig
279-
if cmd.IsSet("stainless-config") {
280-
configPath = cmd.String("stainless-config")
281-
} else if configPath == "" {
282-
return nil, fmt.Errorf("You must provide a stainless configuration file with `--config /path/to/stainless.yml` or run this command from an initialized workspace.")
283-
}
284-
285-
stainlessConfig, err := os.ReadFile(configPath)
286-
if err != nil {
287-
return nil, fmt.Errorf("Could not read your stainless configuration file:\n%w", err)
288-
}
289-
specParams.Source.StainlessConfig = string(stainlessConfig)
290-
291-
oasPath := wc.OpenAPISpec
292-
if cmd.IsSet("openapi-spec") {
293-
oasPath = cmd.String("openapi-spec")
294-
} else if oasPath == "" {
295-
return nil, fmt.Errorf("You must provide an OpenAPI specification with `--oas /path/to/openapi.json` or run this command from an initialized workspace.")
296-
}
297-
298-
openAPISpec, err := os.ReadFile(oasPath)
299-
if err != nil {
300-
return nil, fmt.Errorf("Could not read your stainless configuration file:\n%w", err)
301-
}
302-
specParams.Source.OpenAPISpec = string(openAPISpec)
303-
304-
options := []option.RequestOption{}
305-
if cmd.Bool("debug") {
306-
options = append(options, debugMiddlewareOption)
307-
}
308-
var result []byte
309-
err = client.Post(
310-
ctx,
311-
"api/generate/spec",
312-
specParams,
313-
&result,
314-
options...,
315-
)
316-
if err != nil {
317-
return nil, err
318-
}
319-
320-
transform := "spec.diagnostics.@values.@flatten.#(ignored==false)#"
321-
jsonObj := gjson.Parse(string(result)).Get(transform)
322-
var diagnostics []stainless.BuildDiagnostic
323-
json.Unmarshal([]byte(jsonObj.Raw), &diagnostics)
324-
return diagnostics, nil
325-
}
326267

327268
func hasBlockingDiagnostic(diagnostics []stainless.BuildDiagnostic) bool {
328269
for _, d := range diagnostics {

pkg/cmd/lint.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67
"log"
78
"os"
@@ -17,6 +18,8 @@ import (
1718
"github.com/stainless-api/stainless-api-cli/pkg/console"
1819
"github.com/stainless-api/stainless-api-cli/pkg/workspace"
1920
"github.com/stainless-api/stainless-api-go"
21+
"github.com/stainless-api/stainless-api-go/option"
22+
"github.com/tidwall/gjson"
2023
"github.com/urfave/cli/v3"
2124
)
2225

@@ -170,6 +173,64 @@ func getDiagnosticsCmd(ctx context.Context, cmd *cli.Command, client stainless.C
170173
}
171174
}
172175

176+
func getDiagnostics(ctx context.Context, cmd *cli.Command, client stainless.Client, wc workspace.Config) ([]stainless.BuildDiagnostic, error) {
177+
var specParams GenerateSpecParams
178+
if cmd.IsSet("project") {
179+
specParams.Project = cmd.String("project")
180+
} else {
181+
specParams.Project = wc.Project
182+
}
183+
specParams.Source.Type = "upload"
184+
185+
configPath := wc.StainlessConfig
186+
if cmd.IsSet("stainless-config") {
187+
configPath = cmd.String("stainless-config")
188+
} else if configPath == "" {
189+
return nil, fmt.Errorf("You must provide a stainless configuration file with `--config /path/to/stainless.yml` or run this command from an initialized workspace.")
190+
}
191+
192+
stainlessConfig, err := os.ReadFile(configPath)
193+
if err != nil {
194+
return nil, fmt.Errorf("Could not read your stainless configuration file:\n%w", err)
195+
}
196+
specParams.Source.StainlessConfig = string(stainlessConfig)
197+
198+
oasPath := wc.OpenAPISpec
199+
if cmd.IsSet("openapi-spec") {
200+
oasPath = cmd.String("openapi-spec")
201+
} else if oasPath == "" {
202+
return nil, fmt.Errorf("You must provide an OpenAPI specification with `--oas /path/to/openapi.json` or run this command from an initialized workspace.")
203+
}
204+
205+
openAPISpec, err := os.ReadFile(oasPath)
206+
if err != nil {
207+
return nil, fmt.Errorf("Could not read your stainless configuration file:\n%w", err)
208+
}
209+
specParams.Source.OpenAPISpec = string(openAPISpec)
210+
211+
options := []option.RequestOption{}
212+
if cmd.Bool("debug") {
213+
options = append(options, debugMiddlewareOption)
214+
}
215+
var result []byte
216+
err = client.Post(
217+
ctx,
218+
"api/generate/spec",
219+
specParams,
220+
&result,
221+
options...,
222+
)
223+
if err != nil {
224+
return nil, err
225+
}
226+
227+
transform := "spec.diagnostics.@values.@flatten.#(ignored==false)#"
228+
jsonObj := gjson.Parse(string(result)).Get(transform)
229+
var diagnostics []stainless.BuildDiagnostic
230+
json.Unmarshal([]byte(jsonObj.Raw), &diagnostics)
231+
return diagnostics, nil
232+
}
233+
173234
func (m lintModel) ShortHelp() []key.Binding {
174235
if m.canSkip {
175236
return []key.Binding{

0 commit comments

Comments
 (0)