Skip to content

Commit 799d88f

Browse files
feat: better support for positional arguments
1 parent c7df1a7 commit 799d88f

File tree

12 files changed

+222
-58
lines changed

12 files changed

+222
-58
lines changed

cmd/stl/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/stainless-api/stainless-api-cli/pkg/cmd"
1313
"github.com/stainless-api/stainless-api-go"
14+
"github.com/tidwall/gjson"
1415
)
1516

1617
func main() {
@@ -19,8 +20,9 @@ func main() {
1920
var apierr *stainless.Error
2021
if errors.As(err, &apierr) {
2122
fmt.Fprintf(os.Stderr, "%s %q: %d %s\n", apierr.Request.Method, apierr.Request.URL, apierr.Response.StatusCode, http.StatusText(apierr.Response.StatusCode))
22-
format := app.String("format")
23-
show_err := cmd.ShowJSON("Error", apierr.RawJSON(), format)
23+
format := app.String("format-error")
24+
json := gjson.Parse(apierr.RawJSON())
25+
show_err := cmd.ShowJSON("Error", json, format, app.String("transform-error"))
2426
if show_err != nil {
2527
// Just print the original error:
2628
fmt.Fprintf(os.Stderr, "%s\n", err.Error())

pkg/cmd/build.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,11 @@ var buildsCompare = cli.Command{
439439
func handleBuildsCreate(ctx context.Context, cmd *cli.Command) error {
440440
cc := getAPICommandContext(cmd)
441441

442+
unusedArgs := cmd.Args().Slice()
443+
if len(unusedArgs) > 0 {
444+
return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs)
445+
}
446+
442447
// Handle file flags by reading files and mutating JSON body
443448
if err := applyFileFlag(cmd, "openapi-spec", "revision.openapi\\.yml.content"); err != nil {
444449
return err
@@ -459,7 +464,7 @@ func handleBuildsCreate(ctx context.Context, cmd *cli.Command) error {
459464
if err != nil {
460465
return err
461466
}
462-
467+
463468
buildGroup.Property("build_id", res.ID)
464469

465470
if cmd.Bool("wait") {
@@ -483,12 +488,22 @@ func handleBuildsCreate(ctx context.Context, cmd *cli.Command) error {
483488
}
484489
}
485490

491+
data := gjson.Parse(string(res.RawJSON()))
486492
format := cmd.Root().String("format")
487-
return ShowJSON("builds create", res.RawJSON(), format)
493+
transform := cmd.Root().String("transform")
494+
return ShowJSON("builds create", data, format, transform)
488495
}
489496

490497
func handleBuildsRetrieve(ctx context.Context, cmd *cli.Command) error {
491498
cc := getAPICommandContext(cmd)
499+
unusedArgs := cmd.Args().Slice()
500+
if !cmd.IsSet("build-id") && len(unusedArgs) > 0 {
501+
cmd.Set("build-id", unusedArgs[0])
502+
unusedArgs = unusedArgs[1:]
503+
}
504+
if len(unusedArgs) > 0 {
505+
return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs)
506+
}
492507
var res []byte
493508
_, err := cc.client.Builds.Get(
494509
context.TODO(),
@@ -500,8 +515,10 @@ func handleBuildsRetrieve(ctx context.Context, cmd *cli.Command) error {
500515
return err
501516
}
502517

518+
json := gjson.Parse(string(res))
503519
format := cmd.Root().String("format")
504-
return ShowJSON("builds retrieve", string(res), format)
520+
transform := cmd.Root().String("transform")
521+
return ShowJSON("builds retrieve", json, format, transform)
505522
}
506523

507524
// pullBuildOutputs pulls the outputs for a completed build
@@ -775,6 +792,10 @@ func pullOutput(output, url, ref, targetDir string, targetGroup *Group) error {
775792

776793
func handleBuildsList(ctx context.Context, cmd *cli.Command) error {
777794
cc := getAPICommandContext(cmd)
795+
unusedArgs := cmd.Args().Slice()
796+
if len(unusedArgs) > 0 {
797+
return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs)
798+
}
778799
params := stainless.BuildListParams{}
779800
var res []byte
780801
_, err := cc.client.Builds.List(
@@ -787,12 +808,18 @@ func handleBuildsList(ctx context.Context, cmd *cli.Command) error {
787808
return err
788809
}
789810

811+
json := gjson.Parse(string(res))
790812
format := cmd.Root().String("format")
791-
return ShowJSON("builds list", string(res), format)
813+
transform := cmd.Root().String("transform")
814+
return ShowJSON("builds list", json, format, transform)
792815
}
793816

794817
func handleBuildsCompare(ctx context.Context, cmd *cli.Command) error {
795818
cc := getAPICommandContext(cmd)
819+
unusedArgs := cmd.Args().Slice()
820+
if len(unusedArgs) > 0 {
821+
return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs)
822+
}
796823
params := stainless.BuildCompareParams{}
797824
var res []byte
798825
_, err := cc.client.Builds.Compare(
@@ -805,6 +832,8 @@ func handleBuildsCompare(ctx context.Context, cmd *cli.Command) error {
805832
return err
806833
}
807834

835+
json := gjson.Parse(string(res))
808836
format := cmd.Root().String("format")
809-
return ShowJSON("builds compare", string(res), format)
837+
transform := cmd.Root().String("transform")
838+
return ShowJSON("builds compare", json, format, transform)
810839
}

pkg/cmd/builddiagnostic.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ package cmd
44

55
import (
66
"context"
7+
"fmt"
78

89
"github.com/stainless-api/stainless-api-cli/pkg/jsonflag"
910
"github.com/stainless-api/stainless-api-go"
1011
"github.com/stainless-api/stainless-api-go/option"
12+
"github.com/tidwall/gjson"
1113
"github.com/urfave/cli/v3"
1214
)
1315

@@ -60,6 +62,14 @@ var buildsDiagnosticsList = cli.Command{
6062

6163
func handleBuildsDiagnosticsList(ctx context.Context, cmd *cli.Command) error {
6264
cc := getAPICommandContext(cmd)
65+
unusedArgs := cmd.Args().Slice()
66+
if !cmd.IsSet("build-id") && len(unusedArgs) > 0 {
67+
cmd.Set("build-id", unusedArgs[0])
68+
unusedArgs = unusedArgs[1:]
69+
}
70+
if len(unusedArgs) > 0 {
71+
return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs)
72+
}
6373
params := stainless.BuildDiagnosticListParams{}
6474
var res []byte
6575
_, err := cc.client.Builds.Diagnostics.List(
@@ -73,6 +83,8 @@ func handleBuildsDiagnosticsList(ctx context.Context, cmd *cli.Command) error {
7383
return err
7484
}
7585

86+
json := gjson.Parse(string(res))
7687
format := cmd.Root().String("format")
77-
return ShowJSON("builds:diagnostics list", string(res), format)
88+
transform := cmd.Root().String("transform")
89+
return ShowJSON("builds:diagnostics list", json, format, transform)
7890
}

pkg/cmd/buildtargetoutput.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/stainless-api/stainless-api-cli/pkg/jsonflag"
1010
"github.com/stainless-api/stainless-api-go"
1111
"github.com/stainless-api/stainless-api-go/option"
12+
"github.com/tidwall/gjson"
1213
"github.com/urfave/cli/v3"
1314
)
1415

@@ -62,6 +63,10 @@ var buildsTargetOutputsRetrieve = cli.Command{
6263

6364
func handleBuildsTargetOutputsRetrieve(ctx context.Context, cmd *cli.Command) error {
6465
cc := getAPICommandContext(cmd)
66+
unusedArgs := cmd.Args().Slice()
67+
if len(unusedArgs) > 0 {
68+
return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs)
69+
}
6570

6671
buildID := cmd.String("build-id")
6772
if buildID == "" {
@@ -75,6 +80,7 @@ func handleBuildsTargetOutputsRetrieve(ctx context.Context, cmd *cli.Command) er
7580
params := stainless.BuildTargetOutputGetParams{
7681
BuildID: buildID,
7782
}
83+
var resBytes []byte
7884
res, err := cc.client.Builds.TargetOutputs.Get(
7985
context.TODO(),
8086
params,
@@ -84,8 +90,10 @@ func handleBuildsTargetOutputsRetrieve(ctx context.Context, cmd *cli.Command) er
8490
return err
8591
}
8692

93+
json := gjson.Parse(string(resBytes))
8794
format := cmd.Root().String("format")
88-
if err := ShowJSON("builds:target_outputs retrieve", res.RawJSON(), format); err != nil {
95+
transform := cmd.Root().String("transform")
96+
if err := ShowJSON("builds:target_outputs retrieve", json, format, transform); err != nil {
8997
return err
9098
}
9199

pkg/cmd/cmd.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,34 @@ stl builds create --branch <branch>`,
4343
},
4444
&cli.StringFlag{
4545
Name: "format",
46-
Usage: "The format for data output (one of: " + strings.Join(OutputFormats[:], ", ") + ")",
46+
Usage: "The format for displaying response data (one of: " + strings.Join(OutputFormats, ", ") + ")",
4747
Value: "auto",
4848
Validator: func(format string) error {
49-
if !slices.Contains(OutputFormats[:], strings.ToLower(format)) {
50-
return fmt.Errorf("format must be one of: %s", strings.Join(OutputFormats[:], ", "))
49+
if !slices.Contains(OutputFormats, strings.ToLower(format)) {
50+
return fmt.Errorf("format must be one of: %s", strings.Join(OutputFormats, ", "))
5151
}
5252
return nil
5353
},
5454
},
55+
&cli.StringFlag{
56+
Name: "format-error",
57+
Usage: "The format for displaying error data (one of: " + strings.Join(OutputFormats, ", ") + ")",
58+
Value: "auto",
59+
Validator: func(format string) error {
60+
if !slices.Contains(OutputFormats, strings.ToLower(format)) {
61+
return fmt.Errorf("format must be one of: %s", strings.Join(OutputFormats, ", "))
62+
}
63+
return nil
64+
},
65+
},
66+
&cli.StringFlag{
67+
Name: "transform",
68+
Usage: "The GJSON transformation for data output.",
69+
},
70+
&cli.StringFlag{
71+
Name: "transform-error",
72+
Usage: "The GJSON transformation for errors.",
73+
},
5574
&cli.StringFlag{
5675
Name: "environment",
5776
Usage: "Set the environment for API requests",

pkg/cmd/org.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ package cmd
44

55
import (
66
"context"
7+
"fmt"
78

89
"github.com/stainless-api/stainless-api-go/option"
10+
"github.com/tidwall/gjson"
911
"github.com/urfave/cli/v3"
1012
)
1113

@@ -31,6 +33,14 @@ var orgsList = cli.Command{
3133

3234
func handleOrgsRetrieve(ctx context.Context, cmd *cli.Command) error {
3335
cc := getAPICommandContext(cmd)
36+
unusedArgs := cmd.Args().Slice()
37+
if !cmd.IsSet("org") && len(unusedArgs) > 0 {
38+
cmd.Set("org", unusedArgs[0])
39+
unusedArgs = unusedArgs[1:]
40+
}
41+
if len(unusedArgs) > 0 {
42+
return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs)
43+
}
3444
var res []byte
3545
_, err := cc.client.Orgs.Get(
3646
context.TODO(),
@@ -42,12 +52,18 @@ func handleOrgsRetrieve(ctx context.Context, cmd *cli.Command) error {
4252
return err
4353
}
4454

55+
json := gjson.Parse(string(res))
4556
format := cmd.Root().String("format")
46-
return ShowJSON("orgs retrieve", string(res), format)
57+
transform := cmd.Root().String("transform")
58+
return ShowJSON("orgs retrieve", json, format, transform)
4759
}
4860

4961
func handleOrgsList(ctx context.Context, cmd *cli.Command) error {
5062
cc := getAPICommandContext(cmd)
63+
unusedArgs := cmd.Args().Slice()
64+
if len(unusedArgs) > 0 {
65+
return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs)
66+
}
5167
var res []byte
5268
_, err := cc.client.Orgs.List(
5369
context.TODO(),
@@ -58,6 +74,8 @@ func handleOrgsList(ctx context.Context, cmd *cli.Command) error {
5874
return err
5975
}
6076

77+
json := gjson.Parse(string(res))
6178
format := cmd.Root().String("format")
62-
return ShowJSON("orgs list", string(res), format)
79+
transform := cmd.Root().String("transform")
80+
return ShowJSON("orgs list", json, format, transform)
6381
}

pkg/cmd/project.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ package cmd
44

55
import (
66
"context"
7+
"fmt"
78

89
"github.com/stainless-api/stainless-api-cli/pkg/jsonflag"
910
"github.com/stainless-api/stainless-api-go"
1011
"github.com/stainless-api/stainless-api-go/option"
12+
"github.com/tidwall/gjson"
1113
"github.com/urfave/cli/v3"
1214
)
1315

@@ -118,6 +120,10 @@ var projectsList = cli.Command{
118120

119121
func handleProjectsCreate(ctx context.Context, cmd *cli.Command) error {
120122
cc := getAPICommandContext(cmd)
123+
unusedArgs := cmd.Args().Slice()
124+
if len(unusedArgs) > 0 {
125+
return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs)
126+
}
121127
params := stainless.ProjectNewParams{}
122128
var res []byte
123129
_, err := cc.client.Projects.New(
@@ -130,12 +136,18 @@ func handleProjectsCreate(ctx context.Context, cmd *cli.Command) error {
130136
return err
131137
}
132138

139+
json := gjson.Parse(string(res))
133140
format := cmd.Root().String("format")
134-
return ShowJSON("projects create", string(res), format)
141+
transform := cmd.Root().String("transform")
142+
return ShowJSON("projects create", json, format, transform)
135143
}
136144

137145
func handleProjectsRetrieve(ctx context.Context, cmd *cli.Command) error {
138146
cc := getAPICommandContext(cmd)
147+
unusedArgs := cmd.Args().Slice()
148+
if len(unusedArgs) > 0 {
149+
return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs)
150+
}
139151
params := stainless.ProjectGetParams{}
140152
if cmd.IsSet("project") {
141153
params.Project = stainless.String(cmd.Value("project").(string))
@@ -151,12 +163,18 @@ func handleProjectsRetrieve(ctx context.Context, cmd *cli.Command) error {
151163
return err
152164
}
153165

166+
json := gjson.Parse(string(res))
154167
format := cmd.Root().String("format")
155-
return ShowJSON("projects retrieve", string(res), format)
168+
transform := cmd.Root().String("transform")
169+
return ShowJSON("projects retrieve", json, format, transform)
156170
}
157171

158172
func handleProjectsUpdate(ctx context.Context, cmd *cli.Command) error {
159173
cc := getAPICommandContext(cmd)
174+
unusedArgs := cmd.Args().Slice()
175+
if len(unusedArgs) > 0 {
176+
return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs)
177+
}
160178
params := stainless.ProjectUpdateParams{}
161179
if cmd.IsSet("project") {
162180
params.Project = stainless.String(cmd.Value("project").(string))
@@ -172,12 +190,18 @@ func handleProjectsUpdate(ctx context.Context, cmd *cli.Command) error {
172190
return err
173191
}
174192

193+
json := gjson.Parse(string(res))
175194
format := cmd.Root().String("format")
176-
return ShowJSON("projects update", string(res), format)
195+
transform := cmd.Root().String("transform")
196+
return ShowJSON("projects update", json, format, transform)
177197
}
178198

179199
func handleProjectsList(ctx context.Context, cmd *cli.Command) error {
180200
cc := getAPICommandContext(cmd)
201+
unusedArgs := cmd.Args().Slice()
202+
if len(unusedArgs) > 0 {
203+
return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs)
204+
}
181205
params := stainless.ProjectListParams{}
182206
var res []byte
183207
_, err := cc.client.Projects.List(
@@ -190,6 +214,8 @@ func handleProjectsList(ctx context.Context, cmd *cli.Command) error {
190214
return err
191215
}
192216

217+
json := gjson.Parse(string(res))
193218
format := cmd.Root().String("format")
194-
return ShowJSON("projects list", string(res), format)
219+
transform := cmd.Root().String("transform")
220+
return ShowJSON("projects list", json, format, transform)
195221
}

0 commit comments

Comments
 (0)