Skip to content

Commit 44a8a3a

Browse files
committed
refactor(cmd/lint): move getDiagnostics to cmd/lint.go
1 parent a057170 commit 44a8a3a

2 files changed

Lines changed: 70 additions & 69 deletions

File tree

pkg/cmd/dev.go

Lines changed: 0 additions & 69 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

@@ -257,73 +255,6 @@ func runDevBuild(ctx context.Context, client stainless.Client, wc workspace.Conf
257255
return nil
258256
}
259257

260-
type GenerateSpecParams struct {
261-
Project string `json:"project"`
262-
Source struct {
263-
Type string `json:"type"`
264-
OpenAPISpec string `json:"openapi_spec"`
265-
StainlessConfig string `json:"stainless_config"`
266-
} `json:"source"`
267-
}
268-
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-
}
326-
327258
func hasBlockingDiagnostic(diagnostics []stainless.BuildDiagnostic) bool {
328259
for _, d := range diagnostics {
329260
if !d.Ignored {

pkg/cmd/lint.go

Lines changed: 70 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,73 @@ func getDiagnosticsCmd(ctx context.Context, cmd *cli.Command, client stainless.C
170173
}
171174
}
172175

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

0 commit comments

Comments
 (0)