Skip to content

Commit a108d1a

Browse files
chore(internal): codegen related update
1 parent 85c477b commit a108d1a

File tree

11 files changed

+850
-353
lines changed

11 files changed

+850
-353
lines changed

pkg/cmd/build.go

Lines changed: 151 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"path/filepath"
1414
"time"
1515

16+
"github.com/stainless-api/stainless-api-cli/pkg/jsonflag"
1617
"github.com/stainless-api/stainless-api-go"
1718
"github.com/stainless-api/stainless-api-go/option"
1819
"github.com/urfave/cli/v3"
@@ -96,27 +97,35 @@ var buildsCreate = cli.Command{
9697
Name: "create",
9798
Usage: "Create a new build",
9899
Flags: []cli.Flag{
99-
&cli.StringFlag{
100-
Name: "project",
101-
Action: getAPIFlagAction[string]("body", "project"),
100+
&jsonflag.JSONStringFlag{
101+
Name: "project",
102+
Config: jsonflag.JSONConfig{
103+
Kind: jsonflag.Body,
104+
Path: "project",
105+
},
102106
},
103-
&cli.StringFlag{
104-
Name: "revision",
105-
Action: getAPIFlagAction[string]("body", "revision"),
107+
&jsonflag.JSONStringFlag{
108+
Name: "revision",
109+
Config: jsonflag.JSONConfig{
110+
Kind: jsonflag.Body,
111+
Path: "revision",
112+
},
106113
},
107-
&cli.StringFlag{
114+
&jsonflag.JSONFileFlag{
108115
Name: "openapi-spec",
109116
Aliases: []string{"oas"},
110-
Action: getAPIFlagFileAction("body", "revision.openapi\\.yml.content"),
117+
Config: jsonflag.JSONConfig{
118+
Kind: jsonflag.Body,
119+
Path: "revision.openapi\\.yml.content",
120+
},
111121
},
112-
&cli.StringFlag{
122+
&jsonflag.JSONFileFlag{
113123
Name: "stainless-config",
114124
Aliases: []string{"config"},
115-
Action: getAPIFlagFileAction("body", "revision.openapi\\.stainless\\.yml.content"),
116-
},
117-
&cli.BoolFlag{
118-
Name: "allow-empty",
119-
Action: getAPIFlagAction[bool]("body", "allow_empty"),
125+
Config: jsonflag.JSONConfig{
126+
Kind: jsonflag.Body,
127+
Path: "revision.openapi\\.stainless\\.yml.content",
128+
},
120129
},
121130
&cli.BoolFlag{
122131
Name: "wait",
@@ -126,24 +135,42 @@ var buildsCreate = cli.Command{
126135
Name: "pull",
127136
Usage: "Pull the build outputs after completion (only works with --wait)",
128137
},
129-
&cli.StringFlag{
130-
Name: "branch",
131-
Action: getAPIFlagAction[string]("body", "branch"),
138+
&jsonflag.JSONBoolFlag{
139+
Name: "allow-empty",
140+
Config: jsonflag.JSONConfig{
141+
Kind: jsonflag.Body,
142+
Path: "allow_empty",
143+
},
132144
},
133-
&cli.StringFlag{
134-
Name: "commit-message",
135-
Action: getAPIFlagAction[string]("body", "commit_message"),
145+
&jsonflag.JSONStringFlag{
146+
Name: "branch",
147+
Config: jsonflag.JSONConfig{
148+
Kind: jsonflag.Body,
149+
Path: "branch",
150+
},
136151
},
137-
&cli.StringFlag{
138-
Name: "targets",
139-
Action: getAPIFlagAction[string]("body", "targets.#"),
152+
&jsonflag.JSONStringFlag{
153+
Name: "commit-message",
154+
Config: jsonflag.JSONConfig{
155+
Kind: jsonflag.Body,
156+
Path: "commit_message",
157+
},
140158
},
141-
&cli.StringFlag{
142-
Name: "+target",
143-
Action: getAPIFlagAction[string]("body", "targets.-1"),
159+
&jsonflag.JSONStringFlag{
160+
Name: "targets",
161+
Config: jsonflag.JSONConfig{
162+
Kind: jsonflag.Body,
163+
Path: "targets.#",
164+
},
165+
},
166+
&jsonflag.JSONStringFlag{
167+
Name: "+target",
168+
Config: jsonflag.JSONConfig{
169+
Kind: jsonflag.Body,
170+
Path: "targets.-1",
171+
},
144172
},
145173
},
146-
Before: initAPICommandWithWorkspaceDefaults,
147174
Action: handleBuildsCreate,
148175
HideHelpCommand: true,
149176
}
@@ -156,7 +183,6 @@ var buildsRetrieve = cli.Command{
156183
Name: "build-id",
157184
},
158185
},
159-
Before: initAPICommand,
160186
Action: handleBuildsRetrieve,
161187
HideHelpCommand: true,
162188
}
@@ -165,28 +191,42 @@ var buildsList = cli.Command{
165191
Name: "list",
166192
Usage: "List builds for a project",
167193
Flags: []cli.Flag{
168-
&cli.StringFlag{
169-
Name: "project",
170-
Action: getAPIFlagAction[string]("query", "project"),
194+
&jsonflag.JSONStringFlag{
195+
Name: "project",
196+
Config: jsonflag.JSONConfig{
197+
Kind: jsonflag.Query,
198+
Path: "project",
199+
},
171200
},
172-
&cli.StringFlag{
173-
Name: "branch",
174-
Action: getAPIFlagAction[string]("query", "branch"),
201+
&jsonflag.JSONStringFlag{
202+
Name: "branch",
203+
Config: jsonflag.JSONConfig{
204+
Kind: jsonflag.Query,
205+
Path: "branch",
206+
},
175207
},
176-
&cli.StringFlag{
177-
Name: "cursor",
178-
Action: getAPIFlagAction[string]("query", "cursor"),
208+
&jsonflag.JSONStringFlag{
209+
Name: "cursor",
210+
Config: jsonflag.JSONConfig{
211+
Kind: jsonflag.Query,
212+
Path: "cursor",
213+
},
179214
},
180-
&cli.FloatFlag{
181-
Name: "limit",
182-
Action: getAPIFlagAction[float64]("query", "limit"),
215+
&jsonflag.JSONFloatFlag{
216+
Name: "limit",
217+
Config: jsonflag.JSONConfig{
218+
Kind: jsonflag.Query,
219+
Path: "limit",
220+
},
183221
},
184-
&cli.StringFlag{
185-
Name: "revision",
186-
Action: getAPIFlagAction[string]("query", "revision"),
222+
&jsonflag.JSONStringFlag{
223+
Name: "revision",
224+
Config: jsonflag.JSONConfig{
225+
Kind: jsonflag.Query,
226+
Path: "revision",
227+
},
187228
},
188229
},
189-
Before: initAPICommand,
190230
Action: handleBuildsList,
191231
HideHelpCommand: true,
192232
}
@@ -195,51 +235,76 @@ var buildsCompare = cli.Command{
195235
Name: "compare",
196236
Usage: "Creates two builds whose outputs can be compared directly",
197237
Flags: []cli.Flag{
198-
&cli.StringFlag{
199-
Name: "base.revision",
200-
Action: getAPIFlagAction[string]("body", "base.revision"),
238+
&jsonflag.JSONStringFlag{
239+
Name: "base.revision",
240+
Config: jsonflag.JSONConfig{
241+
Kind: jsonflag.Body,
242+
Path: "base.revision",
243+
},
201244
},
202-
&cli.StringFlag{
203-
Name: "base.branch",
204-
Action: getAPIFlagAction[string]("body", "base.branch"),
245+
&jsonflag.JSONStringFlag{
246+
Name: "base.branch",
247+
Config: jsonflag.JSONConfig{
248+
Kind: jsonflag.Body,
249+
Path: "base.branch",
250+
},
205251
},
206-
&cli.StringFlag{
207-
Name: "base.commit_message",
208-
Action: getAPIFlagAction[string]("body", "base.commit_message"),
252+
&jsonflag.JSONStringFlag{
253+
Name: "base.commit_message",
254+
Config: jsonflag.JSONConfig{
255+
Kind: jsonflag.Body,
256+
Path: "base.commit_message",
257+
},
209258
},
210-
&cli.StringFlag{
211-
Name: "head.revision",
212-
Action: getAPIFlagAction[string]("body", "head.revision"),
259+
&jsonflag.JSONStringFlag{
260+
Name: "head.revision",
261+
Config: jsonflag.JSONConfig{
262+
Kind: jsonflag.Body,
263+
Path: "head.revision",
264+
},
213265
},
214-
&cli.StringFlag{
215-
Name: "head.branch",
216-
Action: getAPIFlagAction[string]("body", "head.branch"),
266+
&jsonflag.JSONStringFlag{
267+
Name: "head.branch",
268+
Config: jsonflag.JSONConfig{
269+
Kind: jsonflag.Body,
270+
Path: "head.branch",
271+
},
217272
},
218-
&cli.StringFlag{
219-
Name: "head.commit_message",
220-
Action: getAPIFlagAction[string]("body", "head.commit_message"),
273+
&jsonflag.JSONStringFlag{
274+
Name: "head.commit_message",
275+
Config: jsonflag.JSONConfig{
276+
Kind: jsonflag.Body,
277+
Path: "head.commit_message",
278+
},
221279
},
222-
&cli.StringFlag{
223-
Name: "project",
224-
Action: getAPIFlagAction[string]("body", "project"),
280+
&jsonflag.JSONStringFlag{
281+
Name: "project",
282+
Config: jsonflag.JSONConfig{
283+
Kind: jsonflag.Body,
284+
Path: "project",
285+
},
225286
},
226-
&cli.StringFlag{
227-
Name: "targets",
228-
Action: getAPIFlagAction[string]("body", "targets.#"),
287+
&jsonflag.JSONStringFlag{
288+
Name: "targets",
289+
Config: jsonflag.JSONConfig{
290+
Kind: jsonflag.Body,
291+
Path: "targets.#",
292+
},
229293
},
230-
&cli.StringFlag{
231-
Name: "+target",
232-
Action: getAPIFlagAction[string]("body", "targets.-1"),
294+
&jsonflag.JSONStringFlag{
295+
Name: "+target",
296+
Config: jsonflag.JSONConfig{
297+
Kind: jsonflag.Body,
298+
Path: "targets.-1",
299+
},
233300
},
234301
},
235-
Before: initAPICommand,
236302
Action: handleBuildsCompare,
237303
HideHelpCommand: true,
238304
}
239305

240306
func handleBuildsCreate(ctx context.Context, cmd *cli.Command) error {
241-
cc := getAPICommandContext(ctx, cmd)
242-
// Log to stderr that we're creating a build
307+
cc := getAPICommandContext(cmd)
243308
fmt.Fprintf(os.Stderr, "%s Creating build...\n", au.BrightCyan("✱"))
244309
params := stainlessv0.BuildNewParams{}
245310
res, err := cc.client.Builds.New(
@@ -339,7 +404,7 @@ func handleBuildsCreate(ctx context.Context, cmd *cli.Command) error {
339404
}
340405

341406
func handleBuildsRetrieve(ctx context.Context, cmd *cli.Command) error {
342-
cc := getAPICommandContext(ctx, cmd)
407+
cc := getAPICommandContext(cmd)
343408
res, err := cc.client.Builds.Get(
344409
context.TODO(),
345410
cmd.Value("build-id").(string),
@@ -497,7 +562,7 @@ func pullOutput(output, url, ref, targetDir, target string) error {
497562
}
498563

499564
func handleBuildsList(ctx context.Context, cmd *cli.Command) error {
500-
cc := getAPICommandContext(ctx, cmd)
565+
cc := getAPICommandContext(cmd)
501566
params := stainlessv0.BuildListParams{}
502567
res, err := cc.client.Builds.List(
503568
context.TODO(),
@@ -513,7 +578,7 @@ func handleBuildsList(ctx context.Context, cmd *cli.Command) error {
513578
}
514579

515580
func handleBuildsCompare(ctx context.Context, cmd *cli.Command) error {
516-
cc := getAPICommandContext(ctx, cmd)
581+
cc := getAPICommandContext(cmd)
517582
params := stainlessv0.BuildCompareParams{}
518583
res, err := cc.client.Builds.Compare(
519584
context.TODO(),
@@ -528,12 +593,9 @@ func handleBuildsCompare(ctx context.Context, cmd *cli.Command) error {
528593
return nil
529594
}
530595

531-
// initAPICommandWithWorkspaceDefaults applies workspace defaults before initializing API command
532-
func initAPICommandWithWorkspaceDefaults(ctx context.Context, cmd *cli.Command) (context.Context, error) {
533-
cc, err := initAPICommand(ctx, cmd)
534-
if err != nil {
535-
return nil, err
536-
}
596+
// getAPICommandWithWorkspaceDefaults applies workspace defaults before initializing API command
597+
func getAPICommandContextWithWorkspaceDefaults(cmd *cli.Command) (*apiCommandContext, error) {
598+
cc := getAPICommandContext(cmd)
537599
config, configPath, err := FindWorkspaceConfig()
538600
if err == nil && config != nil {
539601
// Get the directory containing the workspace config file
@@ -542,19 +604,21 @@ func initAPICommandWithWorkspaceDefaults(ctx context.Context, cmd *cli.Command)
542604
if !cmd.IsSet("openapi-spec") && !cmd.IsSet("oas") && config.OpenAPISpec != "" {
543605
// Resolve OpenAPI spec path relative to workspace config directory
544606
openAPIPath := filepath.Join(configDir, config.OpenAPISpec)
545-
fileAction := getAPIFlagFileAction("body", "revision.openapi\\.yml.content")
546-
if err := fileAction(cc, cmd, openAPIPath); err != nil {
607+
content, err := os.ReadFile(openAPIPath)
608+
if err != nil {
547609
return nil, fmt.Errorf("failed to load OpenAPI spec from workspace config: %v", err)
548610
}
611+
jsonflag.Register(jsonflag.Body, "revision.openapi\\.yml.content", string(content))
549612
}
550613

551614
if !cmd.IsSet("stainless-config") && !cmd.IsSet("config") && config.StainlessConfig != "" {
552615
// Resolve Stainless config path relative to workspace config directory
553616
stainlessConfigPath := filepath.Join(configDir, config.StainlessConfig)
554-
fileAction := getAPIFlagFileAction("body", "revision.openapi\\.stainless\\.yml.content")
555-
if err := fileAction(cc, cmd, stainlessConfigPath); err != nil {
617+
content, err := os.ReadFile(stainlessConfigPath)
618+
if err != nil {
556619
return nil, fmt.Errorf("failed to load Stainless config from workspace config: %v", err)
557620
}
621+
jsonflag.Register(jsonflag.Body, "revision.openapi\\.stainless\\.yml.content", string(content))
558622
}
559623
}
560624
return cc, err

0 commit comments

Comments
 (0)