From 6fcc16fe3bb654ab8e904c499beeb6e55cdb9689 Mon Sep 17 00:00:00 2001 From: Denis Makogon Date: Thu, 3 Aug 2017 12:35:34 +0300 Subject: [PATCH 1/7] Display call ID for sync/async calls in CLI --- cli/client/call_fn.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/cli/client/call_fn.go b/cli/client/call_fn.go index 3b9bfe7b0..8165912f6 100644 --- a/cli/client/call_fn.go +++ b/cli/client/call_fn.go @@ -1,6 +1,7 @@ package client import ( + "encoding/json" "fmt" "io" "net/http" @@ -21,6 +22,10 @@ func EnvAsHeader(req *http.Request, selectedEnv []string) { } } +type callID struct { + CallID string `json:"call_id"` +} + func CallFN(u string, content io.Reader, output io.Writer, method string, env []string) error { if method == "" { if content == nil { @@ -45,8 +50,14 @@ func CallFN(u string, content io.Reader, output io.Writer, method string, env [] if err != nil { return fmt.Errorf("error running route: %s", err) } - - io.Copy(output, resp.Body) + if call_id, found := resp.Header["Fn_call_id"]; found { + fmt.Fprint(output, fmt.Sprintf("Call ID: %v\n", call_id[0])) + io.Copy(output, resp.Body) + } else { + c := &callID{} + json.NewDecoder(resp.Body).Decode(c) + fmt.Fprint(output, fmt.Sprintf("Call ID: %v\n", c.CallID)) + } if resp.StatusCode >= 400 { // TODO: parse out error message From baf9406ad7b6155a533d3c17806973150a0db5fe Mon Sep 17 00:00:00 2001 From: Denis Makogon Date: Thu, 3 Aug 2017 13:07:26 +0300 Subject: [PATCH 2/7] Check API errors along with call ID --- cli/client/call_fn.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/cli/client/call_fn.go b/cli/client/call_fn.go index 8165912f6..a9dcf712e 100644 --- a/cli/client/call_fn.go +++ b/cli/client/call_fn.go @@ -22,8 +22,13 @@ func EnvAsHeader(req *http.Request, selectedEnv []string) { } } +type APIErr struct { + Message string `json:"message"` +} + type callID struct { CallID string `json:"call_id"` + Error APIErr `json:"error"` } func CallFN(u string, content io.Reader, output io.Writer, method string, env []string) error { @@ -56,7 +61,11 @@ func CallFN(u string, content io.Reader, output io.Writer, method string, env [] } else { c := &callID{} json.NewDecoder(resp.Body).Decode(c) - fmt.Fprint(output, fmt.Sprintf("Call ID: %v\n", c.CallID)) + if c.CallID != "" { + fmt.Fprint(output, fmt.Sprintf("Call ID: %v\n", c.CallID)) + } else { + fmt.Fprint(output, fmt.Sprintf("Error: %v\n", c.Error.Message)) + } } if resp.StatusCode >= 400 { From 86b2cc40560658e993fb5a5c6c22e46e9420c4dd Mon Sep 17 00:00:00 2001 From: Denis Makogon Date: Thu, 3 Aug 2017 14:34:37 +0300 Subject: [PATCH 3/7] Addressing comments --- cli/client/call_fn.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/client/call_fn.go b/cli/client/call_fn.go index a9dcf712e..caf0c552b 100644 --- a/cli/client/call_fn.go +++ b/cli/client/call_fn.go @@ -22,13 +22,13 @@ func EnvAsHeader(req *http.Request, selectedEnv []string) { } } -type APIErr struct { +type apiErr struct { Message string `json:"message"` } type callID struct { CallID string `json:"call_id"` - Error APIErr `json:"error"` + Error apiErr `json:"error"` } func CallFN(u string, content io.Reader, output io.Writer, method string, env []string) error { From 6a78a0f477c1bd557ec92661e3e7ccae0ec23682 Mon Sep 17 00:00:00 2001 From: Denis Makogon Date: Thu, 3 Aug 2017 16:14:02 +0300 Subject: [PATCH 4/7] Addressing comments --- cli/client/call_fn.go | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/cli/client/call_fn.go b/cli/client/call_fn.go index caf0c552b..b058eefd1 100644 --- a/cli/client/call_fn.go +++ b/cli/client/call_fn.go @@ -9,6 +9,8 @@ import ( "strings" ) +const FN_CALL_ID = "Fn_call_id" + func EnvAsHeader(req *http.Request, selectedEnv []string) { detectedEnv := os.Environ() if len(selectedEnv) > 0 { @@ -55,16 +57,26 @@ func CallFN(u string, content io.Reader, output io.Writer, method string, env [] if err != nil { return fmt.Errorf("error running route: %s", err) } - if call_id, found := resp.Header["Fn_call_id"]; found { + // for sync calls + if call_id, found := resp.Header[FN_CALL_ID]; found { fmt.Fprint(output, fmt.Sprintf("Call ID: %v\n", call_id[0])) io.Copy(output, resp.Body) } else { + // for async calls and error discovering c := &callID{} - json.NewDecoder(resp.Body).Decode(c) - if c.CallID != "" { - fmt.Fprint(output, fmt.Sprintf("Call ID: %v\n", c.CallID)) + err = json.NewDecoder(resp.Body).Decode(c) + if err == nil { + // decode would not fail in both cases: + // - call id in body + // - error in body + // that's why we need to check values of attributes + if c.CallID != "" { + fmt.Fprint(output, fmt.Sprintf("Call ID: %v\n", c.CallID)) + } else { + fmt.Fprint(output, fmt.Sprintf("Error: %v\n", c.Error.Message)) + } } else { - fmt.Fprint(output, fmt.Sprintf("Error: %v\n", c.Error.Message)) + return err } } From e4bade376a868600960e9a29cec498a72a652e57 Mon Sep 17 00:00:00 2001 From: Denis Makogon Date: Thu, 3 Aug 2017 16:28:21 +0300 Subject: [PATCH 5/7] Adding CLI bool flag --display-call-id Change affects: fn call fn routes call --- cli/client/call_fn.go | 10 +++++++--- cli/routes.go | 13 ++++++++++--- cli/testfn.go | 2 +- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/cli/client/call_fn.go b/cli/client/call_fn.go index b058eefd1..925874da2 100644 --- a/cli/client/call_fn.go +++ b/cli/client/call_fn.go @@ -33,7 +33,7 @@ type callID struct { Error apiErr `json:"error"` } -func CallFN(u string, content io.Reader, output io.Writer, method string, env []string) error { +func CallFN(u string, content io.Reader, output io.Writer, method string, env []string, displayCallID bool) error { if method == "" { if content == nil { method = "GET" @@ -59,7 +59,9 @@ func CallFN(u string, content io.Reader, output io.Writer, method string, env [] } // for sync calls if call_id, found := resp.Header[FN_CALL_ID]; found { - fmt.Fprint(output, fmt.Sprintf("Call ID: %v\n", call_id[0])) + if displayCallID { + fmt.Fprint(output, fmt.Sprintf("Call ID: %v\n", call_id[0])) + } io.Copy(output, resp.Body) } else { // for async calls and error discovering @@ -71,7 +73,9 @@ func CallFN(u string, content io.Reader, output io.Writer, method string, env [] // - error in body // that's why we need to check values of attributes if c.CallID != "" { - fmt.Fprint(output, fmt.Sprintf("Call ID: %v\n", c.CallID)) + if displayCallID { + fmt.Fprint(output, fmt.Sprintf("Call ID: %v\n", c.CallID)) + } } else { fmt.Fprint(output, fmt.Sprintf("Error: %v\n", c.Error.Message)) } diff --git a/cli/routes.go b/cli/routes.go index 9a713fb5e..8670246bf 100644 --- a/cli/routes.go +++ b/cli/routes.go @@ -60,6 +60,13 @@ var updateRouteFlags = append(routeFlags, Usage: "defines whether skip func file or not", }) +var callFnFlags = append(runflags(), + cli.BoolFlag{ + Name: "display-call-id", + Usage: "whether display call ID or not for sync calls", + }, +) + func routes() cli.Command { r := routesCmd{client: client.APIClient()} @@ -73,7 +80,7 @@ func routes() cli.Command { Usage: "call a route", ArgsUsage: " [image]", Action: r.call, - Flags: runflags(), + Flags: callFnFlags, }, { Name: "list", @@ -143,7 +150,7 @@ func call() cli.Command { Name: "call", Usage: "call a remote function", ArgsUsage: " ", - Flags: runflags(), + Flags: callFnFlags, Action: r.call, } } @@ -200,7 +207,7 @@ func (a *routesCmd) call(c *cli.Context) error { u.Path = path.Join(u.Path, "r", appName, route) content := stdin() - return client.CallFN(u.String(), content, os.Stdout, c.String("method"), c.StringSlice("e")) + return client.CallFN(u.String(), content, os.Stdout, c.String("method"), c.StringSlice("e"), c.BoolT("display-call-id")) } func routeWithFlags(c *cli.Context, rt *fnmodels.Route) { diff --git a/cli/testfn.go b/cli/testfn.go index 81105706b..e83bb0f6c 100644 --- a/cli/testfn.go +++ b/cli/testfn.go @@ -211,7 +211,7 @@ func runremotetest(target string, in *inputMap, expectedOut *outputMap, expected os.Setenv(k, v) restrictedEnv = append(restrictedEnv, k) } - if err := client.CallFN(target, stdin, &stdout, "", restrictedEnv); err != nil { + if err := client.CallFN(target, stdin, &stdout, "", restrictedEnv, false); err != nil { return fmt.Errorf("%v\nstdout:%s\n", err, stdout.String()) } From ab8134cbbb6b8aa345ee37d672fac4ef5f6f5591 Mon Sep 17 00:00:00 2001 From: Denis Makogon Date: Thu, 3 Aug 2017 18:00:34 +0300 Subject: [PATCH 6/7] Apply opposite logic call ID CLI flag use --ignore-call-id flag to mute call ID call id now goes to STDERR --- cli/client/call_fn.go | 10 +++++----- cli/routes.go | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/cli/client/call_fn.go b/cli/client/call_fn.go index 925874da2..5a91123f0 100644 --- a/cli/client/call_fn.go +++ b/cli/client/call_fn.go @@ -33,7 +33,7 @@ type callID struct { Error apiErr `json:"error"` } -func CallFN(u string, content io.Reader, output io.Writer, method string, env []string, displayCallID bool) error { +func CallFN(u string, content io.Reader, output io.Writer, method string, env []string, ignoreCallID bool) error { if method == "" { if content == nil { method = "GET" @@ -59,8 +59,8 @@ func CallFN(u string, content io.Reader, output io.Writer, method string, env [] } // for sync calls if call_id, found := resp.Header[FN_CALL_ID]; found { - if displayCallID { - fmt.Fprint(output, fmt.Sprintf("Call ID: %v\n", call_id[0])) + if !ignoreCallID { + fmt.Fprint(os.Stderr, fmt.Sprintf("Call ID: %v\n", call_id[0])) } io.Copy(output, resp.Body) } else { @@ -73,8 +73,8 @@ func CallFN(u string, content io.Reader, output io.Writer, method string, env [] // - error in body // that's why we need to check values of attributes if c.CallID != "" { - if displayCallID { - fmt.Fprint(output, fmt.Sprintf("Call ID: %v\n", c.CallID)) + if !ignoreCallID { + fmt.Fprint(os.Stderr, fmt.Sprintf("Call ID: %v\n", c.CallID)) } } else { fmt.Fprint(output, fmt.Sprintf("Error: %v\n", c.Error.Message)) diff --git a/cli/routes.go b/cli/routes.go index 8670246bf..79f2404f0 100644 --- a/cli/routes.go +++ b/cli/routes.go @@ -62,8 +62,8 @@ var updateRouteFlags = append(routeFlags, var callFnFlags = append(runflags(), cli.BoolFlag{ - Name: "display-call-id", - Usage: "whether display call ID or not for sync calls", + Name: "ignore-call-id", + Usage: "whether display call ID or not", }, ) @@ -207,7 +207,7 @@ func (a *routesCmd) call(c *cli.Context) error { u.Path = path.Join(u.Path, "r", appName, route) content := stdin() - return client.CallFN(u.String(), content, os.Stdout, c.String("method"), c.StringSlice("e"), c.BoolT("display-call-id")) + return client.CallFN(u.String(), content, os.Stdout, c.String("method"), c.StringSlice("e"), c.Bool("ignore-call-id")) } func routeWithFlags(c *cli.Context, rt *fnmodels.Route) { From 3af069ca5ef4c6a8825722bcc3115140d7cc01c7 Mon Sep 17 00:00:00 2001 From: Denis Makogon Date: Thu, 3 Aug 2017 23:28:35 +0300 Subject: [PATCH 7/7] Addressing comments --- cli/client/call_fn.go | 8 +++----- cli/routes.go | 4 ++-- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/cli/client/call_fn.go b/cli/client/call_fn.go index 5a91123f0..5b3e5106b 100644 --- a/cli/client/call_fn.go +++ b/cli/client/call_fn.go @@ -33,7 +33,7 @@ type callID struct { Error apiErr `json:"error"` } -func CallFN(u string, content io.Reader, output io.Writer, method string, env []string, ignoreCallID bool) error { +func CallFN(u string, content io.Reader, output io.Writer, method string, env []string, includeCallID bool) error { if method == "" { if content == nil { method = "GET" @@ -59,7 +59,7 @@ func CallFN(u string, content io.Reader, output io.Writer, method string, env [] } // for sync calls if call_id, found := resp.Header[FN_CALL_ID]; found { - if !ignoreCallID { + if includeCallID { fmt.Fprint(os.Stderr, fmt.Sprintf("Call ID: %v\n", call_id[0])) } io.Copy(output, resp.Body) @@ -73,9 +73,7 @@ func CallFN(u string, content io.Reader, output io.Writer, method string, env [] // - error in body // that's why we need to check values of attributes if c.CallID != "" { - if !ignoreCallID { - fmt.Fprint(os.Stderr, fmt.Sprintf("Call ID: %v\n", c.CallID)) - } + fmt.Fprint(os.Stderr, fmt.Sprintf("Call ID: %v\n", c.CallID)) } else { fmt.Fprint(output, fmt.Sprintf("Error: %v\n", c.Error.Message)) } diff --git a/cli/routes.go b/cli/routes.go index 79f2404f0..ecb7eb99f 100644 --- a/cli/routes.go +++ b/cli/routes.go @@ -62,7 +62,7 @@ var updateRouteFlags = append(routeFlags, var callFnFlags = append(runflags(), cli.BoolFlag{ - Name: "ignore-call-id", + Name: "display-call-id", Usage: "whether display call ID or not", }, ) @@ -207,7 +207,7 @@ func (a *routesCmd) call(c *cli.Context) error { u.Path = path.Join(u.Path, "r", appName, route) content := stdin() - return client.CallFN(u.String(), content, os.Stdout, c.String("method"), c.StringSlice("e"), c.Bool("ignore-call-id")) + return client.CallFN(u.String(), content, os.Stdout, c.String("method"), c.StringSlice("e"), c.Bool("display-call-id")) } func routeWithFlags(c *cli.Context, rt *fnmodels.Route) {