From 343e9b6fa03ad9b8271ea3472b6cc3ecb0fda31c Mon Sep 17 00:00:00 2001 From: Srinandan Sridhar <13950006+srinandan@users.noreply.github.com> Date: Wed, 25 Sep 2024 15:52:22 -0700 Subject: [PATCH] bug: allow setting continueOnErr to false or not at all #546 (#548) * bug: allow setting continueOnErr to false or not at all #546 * chore: update unit tests #546 --- internal/client/flowhooks/flowhook_test.go | 4 +++- internal/client/flowhooks/flowhooks.go | 6 +++--- internal/cmd/flowhooks/crtfh.go | 19 ++++++++++++++----- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/internal/client/flowhooks/flowhook_test.go b/internal/client/flowhooks/flowhook_test.go index bbba65ea7..f7a4b0a35 100644 --- a/internal/client/flowhooks/flowhook_test.go +++ b/internal/client/flowhooks/flowhook_test.go @@ -37,7 +37,9 @@ func TestAttach(t *testing.T) { if _, err := sharedflows.Create(name, path.Join(cliPath, testFolder, "test_flow.zip")); err != nil { t.Fatalf("%v", err) } - if _, err := Attach("PreProxyFlowHook", "test description", name, true); err != nil { + cPtr := new(bool) + *cPtr = true + if _, err := Attach("PreProxyFlowHook", "test description", name, cPtr); err != nil { t.Fatalf("%v", err) } } diff --git a/internal/client/flowhooks/flowhooks.go b/internal/client/flowhooks/flowhooks.go index cbc3c0e8e..dc73bf9fb 100644 --- a/internal/client/flowhooks/flowhooks.go +++ b/internal/client/flowhooks/flowhooks.go @@ -23,7 +23,7 @@ import ( ) // Attach -func Attach(name string, description string, sharedflow string, continueOnErr bool) (respBody []byte, err error) { +func Attach(name string, description string, sharedflow string, continueOnErr *bool) (respBody []byte, err error) { u, _ := url.Parse(apiclient.GetApigeeBaseURL()) flowhook := []string{} @@ -36,8 +36,8 @@ func Attach(name string, description string, sharedflow string, continueOnErr bo flowhook = append(flowhook, "\"sharedFlow\":\""+sharedflow+"\"") - if continueOnErr { - flowhook = append(flowhook, "\"continueOnError\":"+strconv.FormatBool(continueOnErr)) + if continueOnErr != nil { + flowhook = append(flowhook, "\"continueOnError\":"+strconv.FormatBool(*continueOnErr)) } payload := "{" + strings.Join(flowhook, ",") + "}" diff --git a/internal/cmd/flowhooks/crtfh.go b/internal/cmd/flowhooks/crtfh.go index fea3f23b0..1c5b1f2c7 100644 --- a/internal/cmd/flowhooks/crtfh.go +++ b/internal/cmd/flowhooks/crtfh.go @@ -15,8 +15,10 @@ package flowhooks import ( + "fmt" "internal/apiclient" "internal/client/flowhooks" + "strconv" "github.com/spf13/cobra" ) @@ -34,14 +36,21 @@ var CreateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { cmd.SilenceUsage = true - _, err = flowhooks.Attach(name, description, sharedflow, continueOnErr) + if continueOnErrStr != "" { + continueOnErrPtr = new(bool) + *continueOnErrPtr, err = strconv.ParseBool(continueOnErrStr) + if err != nil { + return fmt.Errorf("continueOnErr should be a boolean value: %v", err) + } + } + _, err = flowhooks.Attach(name, description, sharedflow, continueOnErrPtr) return }, } var ( - description, sharedflow string - continueOnErr bool + continueOnErrPtr *bool + description, sharedflow, continueOnErrStr string ) func init() { @@ -51,8 +60,8 @@ func init() { "", "Description for the flowhook") CreateCmd.Flags().StringVarP(&sharedflow, "sharedflow", "s", "", "Sharedflow name") - CreateCmd.Flags().BoolVarP(&continueOnErr, "continue", "c", - true, "Continue on error") + CreateCmd.Flags().StringVarP(&continueOnErrStr, "continue", "c", + "", "Continue on error") _ = CreateCmd.MarkFlagRequired("name") _ = CreateCmd.MarkFlagRequired("sharedflow")