Skip to content

Commit e84af71

Browse files
chore(internal): codegen related update
1 parent abc91cb commit e84af71

File tree

7 files changed

+67
-56
lines changed

7 files changed

+67
-56
lines changed

pkg/cmd/build.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ func getAPICommandContextWithWorkspaceDefaults(cmd *cli.Command) (*apiCommandCon
605605
if err != nil {
606606
return nil, fmt.Errorf("failed to load OpenAPI spec from workspace config: %v", err)
607607
}
608-
jsonflag.Register(jsonflag.Body, "revision.openapi\\.yml.content", string(content))
608+
jsonflag.Mutate(jsonflag.Body, "revision.openapi\\.yml.content", string(content))
609609
}
610610

611611
if !cmd.IsSet("stainless-config") && !cmd.IsSet("config") && config.StainlessConfig != "" {
@@ -615,7 +615,7 @@ func getAPICommandContextWithWorkspaceDefaults(cmd *cli.Command) (*apiCommandCon
615615
if err != nil {
616616
return nil, fmt.Errorf("failed to load Stainless config from workspace config: %v", err)
617617
}
618-
jsonflag.Register(jsonflag.Body, "revision.openapi\\.stainless\\.yml.content", string(content))
618+
jsonflag.Mutate(jsonflag.Body, "revision.openapi\\.stainless\\.yml.content", string(content))
619619
}
620620
}
621621
return cc, err

pkg/cmd/cmd.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ var Command = cli.Command{
1515
Name: "debug",
1616
Usage: "Enable debug logging",
1717
},
18+
&cli.StringFlag{
19+
Name: "base-url",
20+
Usage: "Override the base URL for API requests",
21+
},
1822
},
1923
Commands: []*cli.Command{
2024
{

pkg/cmd/project.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ var projectsList = cli.Command{
136136
}
137137

138138
func handleProjectsCreate(ctx context.Context, cmd *cli.Command) error {
139+
cc := getAPICommandContext(cmd)
140+
139141
// Define available target languages
140142
availableTargets := []huh.Option[string]{
141143
huh.NewOption("TypeScript", "typescript").Selected(true),
@@ -184,7 +186,7 @@ func handleProjectsCreate(ctx context.Context, cmd *cli.Command) error {
184186
Info("Creating a new project...")
185187

186188
// Fetch available organizations for suggestions
187-
orgs := fetchUserOrgs(ctx)
189+
orgs := fetchUserOrgs(cc.client, ctx)
188190

189191
// Auto-fill with first organization if org is empty and orgs are available
190192
if org == "" && len(orgs) > 0 {
@@ -294,20 +296,18 @@ func handleProjectsCreate(ctx context.Context, cmd *cli.Command) error {
294296
content, err := os.ReadFile(openAPISpec)
295297
if err == nil {
296298
// Inject the actual file content into the project creation payload
297-
jsonflag.Register(jsonflag.Body, "revision.openapi\\.yml.content", string(content))
299+
jsonflag.Mutate(jsonflag.Body, "revision.openapi\\.yml.content", string(content))
298300
}
299301
}
300302

301303
if stainlessConfig != "" {
302304
content, err := os.ReadFile(stainlessConfig)
303305
if err == nil {
304306
// Inject the actual file content into the project creation payload
305-
jsonflag.Register(jsonflag.Body, "revision.openapi\\.stainless\\.yml.content", string(content))
307+
jsonflag.Mutate(jsonflag.Body, "revision.openapi\\.stainless\\.yml.content", string(content))
306308
}
307309
}
308310

309-
// Use the original logic - let the JSONFlag middleware handle parameter construction
310-
cc := getAPICommandContext(cmd)
311311
params := stainlessv0.ProjectNewParams{}
312312
res, err := cc.client.Projects.New(
313313
context.TODO(),
@@ -402,9 +402,7 @@ func handleProjectsList(ctx context.Context, cmd *cli.Command) error {
402402
}
403403

404404
// fetchUserOrgs retrieves the list of organizations the user has access to
405-
func fetchUserOrgs(ctx context.Context) []string {
406-
client := stainlessv0.NewClient(getClientOptions()...)
407-
405+
func fetchUserOrgs(client stainlessv0.Client, ctx context.Context) []string {
408406
res, err := client.Orgs.List(ctx)
409407
if err != nil {
410408
// Return empty slice if we can't fetch orgs

pkg/cmd/util.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,20 @@ import (
2323
"golang.org/x/term"
2424
)
2525

26-
func getDefaultRequestOptions() []option.RequestOption {
27-
return append(
28-
[]option.RequestOption{
29-
option.WithHeader("X-Stainless-Lang", "cli"),
30-
option.WithHeader("X-Stainless-Runtime", "cli"),
31-
},
32-
getClientOptions()...,
33-
)
26+
func getDefaultRequestOptions(cmd *cli.Command) []option.RequestOption {
27+
opts := []option.RequestOption{
28+
option.WithHeader("X-Stainless-Lang", "cli"),
29+
option.WithHeader("X-Stainless-Runtime", "cli"),
30+
}
31+
32+
// Override base URL if the --base-url flag is provided
33+
if baseURL := cmd.String("base-url"); baseURL != "" {
34+
opts = append(opts, option.WithBaseURL(baseURL))
35+
}
36+
37+
opts = append(opts, getClientOptions()...)
38+
39+
return opts
3440
}
3541

3642
type apiCommandContext struct {
@@ -47,7 +53,7 @@ func (c apiCommandContext) AsMiddleware() option.Middleware {
4753
var header = []byte("{}")
4854

4955
// Apply JSON flag mutations
50-
body, query, header, err := jsonflag.Apply(body, query, header)
56+
body, query, header, err := jsonflag.ApplyMutations(body, query, header)
5157
if err != nil {
5258
log.Fatal(err)
5359
}
@@ -130,7 +136,7 @@ func (c apiCommandContext) AsMiddleware() option.Middleware {
130136
}
131137

132138
func getAPICommandContext(cmd *cli.Command) *apiCommandContext {
133-
client := stainlessv0.NewClient(getDefaultRequestOptions()...)
139+
client := stainlessv0.NewClient(getDefaultRequestOptions(cmd)...)
134140
return &apiCommandContext{client, cmd}
135141
}
136142

pkg/jsonflag/json_flag.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ type JSONConfig struct {
1616
SetValue interface{}
1717
}
1818

19-
type JsonValueCreator[T any] struct{}
19+
type JSONValueCreator[T any] struct{}
2020

21-
func (c JsonValueCreator[T]) Create(val T, dest *T, config JSONConfig) cli.Value {
21+
func (c JSONValueCreator[T]) Create(val T, dest *T, config JSONConfig) cli.Value {
2222
*dest = val
2323
return &jsonValue[T]{
2424
destination: dest,
2525
config: config,
2626
}
2727
}
2828

29-
func (c JsonValueCreator[T]) ToString(val T) string {
29+
func (c JSONValueCreator[T]) ToString(val T) string {
3030
switch v := any(val).(type) {
3131
case string:
3232
if v == "" {
@@ -59,13 +59,13 @@ func (v *jsonValue[T]) Set(val string) error {
5959
if v.config.SetValue != nil {
6060
// For boolean flags with SetValue, register the configured value
6161
if _, isBool := any(parsed).(bool); isBool {
62-
globalRegistry.Register(v.config.Kind, v.config.Path, v.config.SetValue)
62+
globalRegistry.Mutate(v.config.Kind, v.config.Path, v.config.SetValue)
6363
*v.destination = any(true).(T) // Set the flag itself to true
6464
return nil
6565
}
6666
// For any flags with SetValue, register the configured value
6767
if _, isAny := any(parsed).(interface{}); isAny {
68-
globalRegistry.Register(v.config.Kind, v.config.Path, v.config.SetValue)
68+
globalRegistry.Mutate(v.config.Kind, v.config.Path, v.config.SetValue)
6969
*v.destination = any(v.config.SetValue).(T)
7070
return nil
7171
}
@@ -123,7 +123,7 @@ func (v *jsonValue[T]) Set(val string) error {
123123
}
124124

125125
*v.destination = parsed
126-
globalRegistry.Register(v.config.Kind, v.config.Path, parsed)
126+
globalRegistry.Mutate(v.config.Kind, v.config.Path, parsed)
127127
return err
128128
}
129129

@@ -173,18 +173,18 @@ func (v *jsonValue[T]) IsBoolFlag() bool {
173173
return v.config.SetValue != nil
174174
}
175175

176-
// JsonDateValueCreator is a specialized creator for date-only values
177-
type JsonDateValueCreator struct{}
176+
// JSONDateValueCreator is a specialized creator for date-only values
177+
type JSONDateValueCreator struct{}
178178

179-
func (c JsonDateValueCreator) Create(val time.Time, dest *time.Time, config JSONConfig) cli.Value {
179+
func (c JSONDateValueCreator) Create(val time.Time, dest *time.Time, config JSONConfig) cli.Value {
180180
*dest = val
181181
return &jsonDateValue{
182182
destination: dest,
183183
config: config,
184184
}
185185
}
186186

187-
func (c JsonDateValueCreator) ToString(val time.Time) string {
187+
func (c JSONDateValueCreator) ToString(val time.Time) string {
188188
return val.Format("2006-01-02")
189189
}
190190

@@ -220,7 +220,7 @@ func (v *jsonDateValue) Set(val string) error {
220220
}
221221

222222
*v.destination = timeVal
223-
globalRegistry.Register(v.config.Kind, v.config.Path, timeVal.Format("2006-01-02"))
223+
globalRegistry.Mutate(v.config.Kind, v.config.Path, timeVal.Format("2006-01-02"))
224224
return nil
225225
}
226226

@@ -242,14 +242,6 @@ func (v *jsonDateValue) IsBoolFlag() bool {
242242
return false
243243
}
244244

245-
type JSONStringFlag = cli.FlagBase[string, JSONConfig, JsonValueCreator[string]]
246-
type JSONBoolFlag = cli.FlagBase[bool, JSONConfig, JsonValueCreator[bool]]
247-
type JSONIntFlag = cli.FlagBase[int, JSONConfig, JsonValueCreator[int]]
248-
type JSONFloatFlag = cli.FlagBase[float64, JSONConfig, JsonValueCreator[float64]]
249-
type JSONDatetimeFlag = cli.FlagBase[time.Time, JSONConfig, JsonValueCreator[time.Time]]
250-
type JSONDateFlag = cli.FlagBase[time.Time, JSONConfig, JsonDateValueCreator]
251-
type JSONAnyFlag = cli.FlagBase[interface{}, JSONConfig, JsonValueCreator[interface{}]]
252-
253245
// JsonFileValueCreator handles file-based flags that read content and register with mutations
254246
type JsonFileValueCreator struct{}
255247

@@ -281,7 +273,7 @@ func (v *jsonFileValue) Set(filePath string) error {
281273
*v.destination = filePath
282274

283275
// Register the file content with the global registry
284-
globalRegistry.Register(v.config.Kind, v.config.Path, string(content))
276+
globalRegistry.Mutate(v.config.Kind, v.config.Path, string(content))
285277
return nil
286278
}
287279

@@ -304,3 +296,10 @@ func (v *jsonFileValue) IsBoolFlag() bool {
304296
}
305297

306298
type JSONFileFlag = cli.FlagBase[string, JSONConfig, JsonFileValueCreator]
299+
type JSONStringFlag = cli.FlagBase[string, JSONConfig, JSONValueCreator[string]]
300+
type JSONBoolFlag = cli.FlagBase[bool, JSONConfig, JSONValueCreator[bool]]
301+
type JSONIntFlag = cli.FlagBase[int, JSONConfig, JSONValueCreator[int]]
302+
type JSONFloatFlag = cli.FlagBase[float64, JSONConfig, JSONValueCreator[float64]]
303+
type JSONDatetimeFlag = cli.FlagBase[time.Time, JSONConfig, JSONValueCreator[time.Time]]
304+
type JSONDateFlag = cli.FlagBase[time.Time, JSONConfig, JSONDateValueCreator]
305+
type JSONAnyFlag = cli.FlagBase[interface{}, JSONConfig, JSONValueCreator[interface{}]]

pkg/jsonflag/mutation.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ type registry struct {
2929

3030
var globalRegistry = &registry{}
3131

32-
func (r *registry) Register(kind MutationKind, path string, value interface{}) {
32+
func (r *registry) Mutate(kind MutationKind, path string, value interface{}) {
3333
r.mutations = append(r.mutations, Mutation{
3434
Kind: kind,
3535
Path: path,
3636
Value: value,
3737
})
3838
}
3939

40-
func (r *registry) ApplyMutations(body, query, header []byte) ([]byte, []byte, []byte, error) {
40+
func (r *registry) Apply(body, query, header []byte) ([]byte, []byte, []byte, error) {
4141
var err error
4242

4343
for _, mutation := range r.mutations {
@@ -67,18 +67,24 @@ func (r *registry) List() []Mutation {
6767
return result
6868
}
6969

70-
func Apply(body, query, header []byte) ([]byte, []byte, []byte, error) {
71-
body, query, header, err := globalRegistry.ApplyMutations(body, query, header)
72-
globalRegistry.Clear()
73-
return body, query, header, err
70+
// Mutate adds a mutation that will be applied to the specified kind of data
71+
func Mutate(kind MutationKind, path string, value interface{}) {
72+
globalRegistry.Mutate(kind, path, value)
73+
}
74+
75+
// ApplyMutations applies all registered mutations to the provided JSON data
76+
func ApplyMutations(body, query, header []byte) ([]byte, []byte, []byte, error) {
77+
return globalRegistry.Apply(body, query, header)
7478
}
7579

76-
func Clear() {
80+
// ClearMutations removes all registered mutations from the global registry
81+
func ClearMutations() {
7782
globalRegistry.Clear()
7883
}
7984

80-
func Register(kind MutationKind, path string, value interface{}) {
81-
globalRegistry.Register(kind, path, value)
85+
// ListMutations returns a copy of all currently registered mutations
86+
func ListMutations() []Mutation {
87+
return globalRegistry.List()
8288
}
8389

8490
func jsonSet(json []byte, path string, value interface{}) ([]byte, error) {

pkg/jsonflag/mutation_test.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@ package jsonflag
22

33
import (
44
"testing"
5-
6-
"github.com/urfave/cli/v3"
75
)
86

97
func TestApply(t *testing.T) {
10-
Clear()
8+
ClearMutations()
119

12-
globalRegistry.Register(Body, "name", "test")
13-
globalRegistry.Register(Query, "page", 1)
14-
globalRegistry.Register(Header, "authorization", "Bearer token")
10+
Mutate(Body, "name", "test")
11+
Mutate(Query, "page", 1)
12+
Mutate(Header, "authorization", "Bearer token")
1513

16-
body, query, header, err := globalRegistry.ApplyMutations(
14+
body, query, header, err := ApplyMutations(
1715
[]byte(`{}`),
1816
[]byte(`{}`),
1917
[]byte(`{}`),

0 commit comments

Comments
 (0)