From 86c14b3a3aec656efcf8581611ae33e4bb08eb86 Mon Sep 17 00:00:00 2001 From: Rahul De Date: Sun, 22 Dec 2024 08:26:05 +0000 Subject: [PATCH] [breaking] allow handlers to return errors --- README.md | 24 +++++++++++++----------- lib.go | 8 ++++---- lib_test.go | 4 +++- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 63728e2..32c43f8 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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, } ``` diff --git a/lib.go b/lib.go index 8ef7eb9..be43720 100644 --- a/lib.go +++ b/lib.go @@ -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 @@ -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 diff --git a/lib_test.go b/lib_test.go index 9931c61..bb9f28a 100644 --- a/lib_test.go +++ b/lib_test.go @@ -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",