Merge pull request #184 from fnproject/cli-logs

Display call ID for sync/async calls in CLI
This commit is contained in:
oracloud
2017-08-08 18:05:37 +02:00
committed by GitHub
3 changed files with 48 additions and 7 deletions

View File

@@ -1,6 +1,7 @@
package client
import (
"encoding/json"
"fmt"
"io"
"net/http"
@@ -8,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 {
@@ -21,7 +24,16 @@ func EnvAsHeader(req *http.Request, selectedEnv []string) {
}
}
func CallFN(u string, content io.Reader, output io.Writer, method string, env []string) error {
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, includeCallID bool) error {
if method == "" {
if content == nil {
method = "GET"
@@ -45,8 +57,30 @@ 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)
// for sync calls
if call_id, found := resp.Header[FN_CALL_ID]; found {
if includeCallID {
fmt.Fprint(os.Stderr, fmt.Sprintf("Call ID: %v\n", call_id[0]))
}
io.Copy(output, resp.Body)
} else {
// for async calls and error discovering
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(os.Stderr, fmt.Sprintf("Call ID: %v\n", c.CallID))
} else {
fmt.Fprint(output, fmt.Sprintf("Error: %v\n", c.Error.Message))
}
} else {
return err
}
}
if resp.StatusCode >= 400 {
// TODO: parse out 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",
},
)
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.Bool("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())
}