Skip to content

Commit 567a6e6

Browse files
chore(internal): codegen related update
1 parent 3539b82 commit 567a6e6

File tree

11 files changed

+89
-54
lines changed

11 files changed

+89
-54
lines changed

cmd/stl/main.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,23 @@ package main
44

55
import (
66
"context"
7-
"log"
7+
"errors"
8+
"fmt"
89
"os"
910

1011
"github.com/stainless-api/stainless-api-cli/pkg/cmd"
12+
"github.com/stainless-api/stainless-api-go"
1113
)
1214

1315
func main() {
1416
app := cmd.Command
1517
if err := app.Run(context.Background(), os.Args); err != nil {
16-
log.Fatal(err)
18+
var apierr *stainlessv0.Error
19+
if errors.As(err, &apierr) {
20+
fmt.Printf("%s\n", cmd.ColorizeJSON(apierr.RawJSON(), os.Stderr))
21+
} else {
22+
fmt.Printf("%s\n", err.Error())
23+
}
24+
os.Exit(1)
1725
}
1826
}

pkg/cmd/auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ func pollForToken(clientID, deviceCode string, interval, expiresIn int) (*AuthCo
305305
}
306306

307307
// GetClientOptions returns the request options for API calls
308-
func getClientOptions(ctx context.Context, cmd *cli.Command) []option.RequestOption {
308+
func getClientOptions() []option.RequestOption {
309309
options := []option.RequestOption{}
310310

311311
if apiKey := os.Getenv("STAINLESS_API_KEY"); apiKey != "" {

pkg/cmd/build.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,7 @@ func handleBuildsCreate(ctx context.Context, cmd *cli.Command) error {
398398
}
399399
}
400400

401-
// Print the actual JSON response to stdout for piping
402-
fmt.Printf("%s\n", colorizeJSON(res.RawJSON(), os.Stdout))
401+
fmt.Printf("%s\n", ColorizeJSON(res.RawJSON(), os.Stdout))
403402
return nil
404403
}
405404

@@ -414,7 +413,7 @@ func handleBuildsRetrieve(ctx context.Context, cmd *cli.Command) error {
414413
return err
415414
}
416415

417-
fmt.Printf("%s\n", colorizeJSON(res.RawJSON(), os.Stdout))
416+
fmt.Printf("%s\n", ColorizeJSON(res.RawJSON(), os.Stdout))
418417
return nil
419418
}
420419

@@ -573,7 +572,7 @@ func handleBuildsList(ctx context.Context, cmd *cli.Command) error {
573572
return err
574573
}
575574

576-
fmt.Printf("%s\n", colorizeJSON(res.RawJSON(), os.Stdout))
575+
fmt.Printf("%s\n", ColorizeJSON(res.RawJSON(), os.Stdout))
577576
return nil
578577
}
579578

@@ -589,7 +588,7 @@ func handleBuildsCompare(ctx context.Context, cmd *cli.Command) error {
589588
return err
590589
}
591590

592-
fmt.Printf("%s\n", colorizeJSON(res.RawJSON(), os.Stdout))
591+
fmt.Printf("%s\n", ColorizeJSON(res.RawJSON(), os.Stdout))
593592
return nil
594593
}
595594

pkg/cmd/buildtargetoutput.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func handleBuildsTargetOutputsRetrieve(ctx context.Context, cmd *cli.Command) er
6464
return err
6565
}
6666

67-
fmt.Printf("%s\n", colorizeJSON(res.RawJSON(), os.Stdout))
67+
fmt.Printf("%s\n", ColorizeJSON(res.RawJSON(), os.Stdout))
6868

6969
if cmd.Bool("pull") {
7070
build, err := cc.client.Builds.Get(ctx, cmd.String("build-id"))
@@ -74,5 +74,6 @@ func handleBuildsTargetOutputsRetrieve(ctx context.Context, cmd *cli.Command) er
7474
targetDir := fmt.Sprintf("%s-%s", build.Project, cmd.String("target"))
7575
return pullOutput(res.Output, res.URL, res.Ref, targetDir)
7676
}
77+
7778
return nil
7879
}

pkg/cmd/cmd.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import (
99
var Command = cli.Command{
1010
Name: "stl",
1111
Usage: "CLI for the stainless API",
12+
Flags: []cli.Flag{
13+
&cli.BoolFlag{
14+
Name: "debug",
15+
Usage: "Enable debug logging",
16+
},
17+
},
1218
Commands: []*cli.Command{
1319
{
1420
Name: "auth",

pkg/cmd/org.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func handleOrgsRetrieve(ctx context.Context, cmd *cli.Command) error {
4242
return err
4343
}
4444

45-
fmt.Printf("%s\n", colorizeJSON(res.RawJSON(), os.Stdout))
45+
fmt.Printf("%s\n", ColorizeJSON(res.RawJSON(), os.Stdout))
4646
return nil
4747
}
4848

@@ -53,6 +53,6 @@ func handleOrgsList(ctx context.Context, cmd *cli.Command) error {
5353
return err
5454
}
5555

56-
fmt.Printf("%s\n", colorizeJSON(res.RawJSON(), os.Stdout))
56+
fmt.Printf("%s\n", ColorizeJSON(res.RawJSON(), os.Stdout))
5757
return nil
5858
}

pkg/cmd/project.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,12 @@ func handleProjectsCreate(ctx context.Context, cmd *cli.Command) error {
125125
context.TODO(),
126126
params,
127127
option.WithMiddleware(cc.AsMiddleware()),
128-
option.WithRequestBody("application/json", cc.body),
129128
)
130129
if err != nil {
131130
return err
132131
}
133132

134-
fmt.Printf("%s\n", colorizeJSON(res.RawJSON(), os.Stdout))
133+
fmt.Printf("%s\n", ColorizeJSON(res.RawJSON(), os.Stdout))
135134
return nil
136135
}
137136

@@ -150,7 +149,7 @@ func handleProjectsRetrieve(ctx context.Context, cmd *cli.Command) error {
150149
return err
151150
}
152151

153-
fmt.Printf("%s\n", colorizeJSON(res.RawJSON(), os.Stdout))
152+
fmt.Printf("%s\n", ColorizeJSON(res.RawJSON(), os.Stdout))
154153
return nil
155154
}
156155

@@ -164,13 +163,12 @@ func handleProjectsUpdate(ctx context.Context, cmd *cli.Command) error {
164163
context.TODO(),
165164
params,
166165
option.WithMiddleware(cc.AsMiddleware()),
167-
option.WithRequestBody("application/json", cc.body),
168166
)
169167
if err != nil {
170168
return err
171169
}
172170

173-
fmt.Printf("%s\n", colorizeJSON(res.RawJSON(), os.Stdout))
171+
fmt.Printf("%s\n", ColorizeJSON(res.RawJSON(), os.Stdout))
174172
return nil
175173
}
176174

@@ -186,6 +184,6 @@ func handleProjectsList(ctx context.Context, cmd *cli.Command) error {
186184
return err
187185
}
188186

189-
fmt.Printf("%s\n", colorizeJSON(res.RawJSON(), os.Stdout))
187+
fmt.Printf("%s\n", ColorizeJSON(res.RawJSON(), os.Stdout))
190188
return nil
191189
}

pkg/cmd/projectbranch.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,12 @@ func handleProjectsBranchesCreate(ctx context.Context, cmd *cli.Command) error {
7171
context.TODO(),
7272
params,
7373
option.WithMiddleware(cc.AsMiddleware()),
74-
option.WithRequestBody("application/json", cc.body),
7574
)
7675
if err != nil {
7776
return err
7877
}
7978

80-
fmt.Printf("%s\n", colorizeJSON(res.RawJSON(), os.Stdout))
79+
fmt.Printf("%s\n", ColorizeJSON(res.RawJSON(), os.Stdout))
8180
return nil
8281
}
8382

@@ -97,6 +96,6 @@ func handleProjectsBranchesRetrieve(ctx context.Context, cmd *cli.Command) error
9796
return err
9897
}
9998

100-
fmt.Printf("%s\n", colorizeJSON(res.RawJSON(), os.Stdout))
99+
fmt.Printf("%s\n", ColorizeJSON(res.RawJSON(), os.Stdout))
101100
return nil
102101
}

pkg/cmd/projectconfig.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func handleProjectsConfigsRetrieve(ctx context.Context, cmd *cli.Command) error
7575
return err
7676
}
7777

78-
fmt.Printf("%s\n", colorizeJSON(string(res), os.Stdout))
78+
fmt.Printf("%s\n", ColorizeJSON(string(res), os.Stdout))
7979
return nil
8080
}
8181

@@ -90,13 +90,12 @@ func handleProjectsConfigsGuess(ctx context.Context, cmd *cli.Command) error {
9090
context.TODO(),
9191
params,
9292
option.WithMiddleware(cc.AsMiddleware()),
93-
option.WithRequestBody("application/json", cc.body),
9493
option.WithResponseBodyInto(&res),
9594
)
9695
if err != nil {
9796
return err
9897
}
9998

100-
fmt.Printf("%s\n", colorizeJSON(string(res), os.Stdout))
99+
fmt.Printf("%s\n", ColorizeJSON(string(res), os.Stdout))
101100
return nil
102101
}

pkg/cmd/util.go

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package cmd
22

33
import (
4+
"bytes"
45
"fmt"
56
"io"
67
"log"
78
"net/http"
9+
"net/http/httputil"
810
"net/url"
911
"os"
1012
"strings"
@@ -14,45 +16,61 @@ import (
1416
"github.com/stainless-api/stainless-api-go"
1517
"github.com/stainless-api/stainless-api-go/option"
1618

17-
"github.com/tidwall/sjson"
1819
"github.com/tidwall/gjson"
1920
"github.com/tidwall/pretty"
21+
"github.com/tidwall/sjson"
2022
"github.com/urfave/cli/v3"
2123
"golang.org/x/term"
2224
)
2325

2426
func getDefaultRequestOptions() []option.RequestOption {
25-
return []option.RequestOption{
26-
option.WithHeader("X-Stainless-Lang", "cli"),
27-
option.WithHeader("X-Stainless-Runtime", "cli"),
28-
}
27+
return append(
28+
[]option.RequestOption{
29+
option.WithHeader("X-Stainless-Lang", "cli"),
30+
option.WithHeader("X-Stainless-Runtime", "cli"),
31+
},
32+
getClientOptions()...,
33+
)
2934
}
3035

3136
type apiCommandContext struct {
3237
client stainlessv0.Client
33-
body []byte
34-
query []byte
35-
header []byte
38+
cmd *cli.Command
3639
}
3740

3841
func (c apiCommandContext) AsMiddleware() option.Middleware {
42+
body := getStdInput()
43+
if body == nil {
44+
body = []byte("{}")
45+
}
46+
var query = []byte("{}")
47+
var header = []byte("{}")
48+
49+
// Apply JSON flag mutations
50+
body, query, header, err := jsonflag.Apply(body, query, header)
51+
if err != nil {
52+
log.Fatal(err)
53+
}
54+
55+
debug := c.cmd.Bool("debug")
56+
3957
return func(r *http.Request, mn option.MiddlewareNext) (*http.Response, error) {
4058
q := r.URL.Query()
41-
for key, values := range serializeQuery(c.query) {
59+
for key, values := range serializeQuery(query) {
4260
for _, value := range values {
4361
q.Set(key, value)
4462
}
4563
}
4664
r.URL.RawQuery = q.Encode()
4765

48-
for key, values := range serializeHeader(c.header) {
66+
for key, values := range serializeHeader(header) {
4967
for _, value := range values {
50-
r.Header.Add(key, value)
68+
r.Header.Set(key, value)
5169
}
5270
}
5371

5472
// Handle request body merging if there's a body to process
55-
if r.Body != nil && len(c.body) > 2 { // More than just "{}"
73+
if r.Body != nil || len(body) > 2 { // More than just "{}"
5674
// Read the existing request body
5775
existingBody, err := io.ReadAll(r.Body)
5876
r.Body.Close()
@@ -67,7 +85,7 @@ func (c apiCommandContext) AsMiddleware() option.Middleware {
6785
}
6886

6987
// Parse command body and merge top-level keys
70-
commandResult := gjson.ParseBytes(c.body)
88+
commandResult := gjson.ParseBytes(body)
7189
if commandResult.IsObject() {
7290
commandResult.ForEach(func(key, value gjson.Result) bool {
7391
// Set each top-level key from command body, overwriting existing values
@@ -82,31 +100,38 @@ func (c apiCommandContext) AsMiddleware() option.Middleware {
82100
}
83101

84102
// Set the new body
85-
r.Body = io.NopCloser(strings.NewReader(string(mergedBody)))
103+
r.Body = io.NopCloser(bytes.NewBuffer(mergedBody))
86104
r.ContentLength = int64(len(mergedBody))
87105
r.Header.Set("Content-Type", "application/json")
88106
}
89107

108+
// Add debug logging if the --debug flag is set
109+
if debug {
110+
logger := log.Default()
111+
112+
if reqBytes, err := httputil.DumpRequest(r, true); err == nil {
113+
logger.Printf("Request Content:\n%s\n", reqBytes)
114+
}
115+
116+
resp, err := mn(r)
117+
if err != nil {
118+
return resp, err
119+
}
120+
121+
if respBytes, err := httputil.DumpResponse(resp, true); err == nil {
122+
logger.Printf("Response Content:\n%s\n", respBytes)
123+
}
124+
125+
return resp, err
126+
}
127+
90128
return mn(r)
91129
}
92130
}
93131

94132
func getAPICommandContext(cmd *cli.Command) *apiCommandContext {
95133
client := stainlessv0.NewClient(getDefaultRequestOptions()...)
96-
body := getStdInput()
97-
if body == nil {
98-
body = []byte("{}")
99-
}
100-
var query = []byte("{}")
101-
var header = []byte("{}")
102-
103-
// Apply JSON flag mutations
104-
body, query, header, err := jsonflag.Apply(body, query, header)
105-
if err != nil {
106-
log.Fatal(err)
107-
}
108-
109-
return &apiCommandContext{client, body, query, header}
134+
return &apiCommandContext{client, cmd}
110135
}
111136

112137
func serializeQuery(params []byte) url.Values {
@@ -224,7 +249,7 @@ func shouldUseColors(w io.Writer) bool {
224249
return false
225250
}
226251

227-
func colorizeJSON(input string, w io.Writer) string {
252+
func ColorizeJSON(input string, w io.Writer) string {
228253
if !shouldUseColors(w) {
229254
return input
230255
}

0 commit comments

Comments
 (0)