Skip to content

Commit

Permalink
[breaking] allow handlers to return errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lispyclouds committed Dec 22, 2024
1 parent d9609cb commit 86c14b3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ What if you can influence the CLI behaviour from the server? This enables you to

### Status

Alpha, stabilising API and looking for design/usage feedback!
Alpha, looking for design/usage feedback!

### Ideally support:
- more of the OpenAPI types and their checks. eg arrays, enums, objects, multi types etc
Expand Down Expand Up @@ -56,17 +56,19 @@ Define a cobra root command:

```go
rootCmd := &cobra.Command{
Use: "calc",
Short: "My Calc",
Long: "My Calc powered by OpenAPI",
Use: "calc",
Short: "My Calc",
Long: "My Calc powered by OpenAPI",
}
```

Define one or more handler functions of the following signature:
```go
func handler(opts *cobra.Command, args []string, data climate.HandlerData) {
// do something more useful
slog.Info("called!", "data", fmt.Sprintf("%+v", data))
func handler(opts *cobra.Command, args []string, data climate.HandlerData) error {
slog.Info("called!", "data", fmt.Sprintf("%+v", data))
err := doSomethingUseful(data)

return err
}
```
#### Handler Data
Expand All @@ -89,10 +91,10 @@ Define the handlers for the necessary operations. These map to the `operationId`

```go
handlers := map[string]Handler{
"AddGet": handler,
"AddPost": handler,
"HealthCheck": handler,
"GetInfo": handler,
"AddGet": handler,
"AddPost": handler,
"HealthCheck": handler,
"GetInfo": handler,
}
```

Expand Down
8 changes: 4 additions & 4 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type HandlerData struct {
}

// The handler signature
type Handler func(opts *cobra.Command, args []string, data HandlerData)
type Handler func(opts *cobra.Command, args []string, data HandlerData) error

type extensions struct {
hidden bool
Expand Down Expand Up @@ -260,12 +260,12 @@ func BootstrapV3(rootCmd *cobra.Command, model libopenapi.DocumentModel[v3.Docum
if op.Summary != "" {
cmd.Short = op.Summary
}
cmd.Run = func(opts *cobra.Command, args []string) {
cmd.RunE = func(opts *cobra.Command, args []string) error {
if err := interpolatePath(&cmd, &hData); err != nil {
slog.Error("Error interpolating path", "err", err)
return err
}

handler(opts, args, hData)
return handler(opts, args, hData)
}

cmd.Use = op.OperationId // default
Expand Down
4 changes: 3 additions & 1 deletion lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,14 @@ func TestBootstrapV3(t *testing.T) {
model, err := LoadFileV3("api.yaml")
assert.NoError(t, err)

handler := func(opts *cobra.Command, args []string, data HandlerData) {
handler := func(opts *cobra.Command, args []string, data HandlerData) error {
assert.Equal(t, data.PathParams, []ParamMeta{{Name: "p1", Type: Integer}})
assert.Equal(t, data.QueryParams, []ParamMeta{{Name: "p2", Type: String}})
assert.Equal(t, data.HeaderParams, []ParamMeta{{Name: "p3", Type: Number}})
assert.Equal(t, data.CookieParams, []ParamMeta{{Name: "p4", Type: Boolean}})
assert.Equal(t, data.RequestBodyParam, &ParamMeta{Name: "req-body", Type: String})

return nil
}
rootCmd := &cobra.Command{
Use: "calc",
Expand Down

0 comments on commit 86c14b3

Please sign in to comment.