Adding CLI bool flag --display-call-id

Change affects:
  fn call
  fn routes call
This commit is contained in:
Denis Makogon
2017-08-03 16:28:21 +03:00
parent 6a78a0f477
commit e4bade376a
3 changed files with 18 additions and 7 deletions

View File

@@ -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))
}

View File

@@ -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: "<app> </path> [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: "<app> </path>",
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) {

View File

@@ -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())
}