forked from imjerrybao/apex
/
apex_invoke.go
98 lines (75 loc) · 1.76 KB
/
apex_invoke.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"encoding/json"
"fmt"
"io"
"os"
"github.com/spf13/cobra"
"github.com/apex/apex/function"
"github.com/apex/log"
)
type InvokeCmdLocalValues struct {
Async bool
Logs bool
name string
}
const invokeCmdExample = ` Invoke a function with input json
$ apex invoke foo < request.json`
var invokeCmd = &cobra.Command{
Use: "invoke <name>",
Short: "Invoke a function",
Example: invokeCmdExample,
PreRun: invokeCmdPreRun,
Run: invokeCmdRun,
}
var invokeCmdLocalValues = InvokeCmdLocalValues{}
func init() {
lv := &invokeCmdLocalValues
f := invokeCmd.Flags()
f.BoolVarP(&lv.Async, "async", "a", false, "Async invocation")
f.BoolVarP(&lv.Logs, "logs", "L", false, "Print logs")
}
func invokeCmdPreRun(c *cobra.Command, args []string) {
lv := &invokeCmdLocalValues
if len(args) < 1 {
log.Fatal("Missing name argument")
}
lv.name = args[0]
}
// reads request json from stdin and outputs the responses
func invokeCmdRun(c *cobra.Command, args []string) {
lv := &invokeCmdLocalValues
dec := json.NewDecoder(os.Stdin)
kind := function.RequestResponse
if lv.Async {
kind = function.Event
}
fn, err := pv.project.FunctionByName(lv.name)
if err != nil {
log.Fatalf("error: %s", err)
}
for {
var v map[string]interface{}
err := dec.Decode(&v)
if err == io.EOF {
break
}
if err != nil {
log.Fatalf("error parsing response: %s", err)
}
var reply, logs io.Reader
if e, ok := v["event"].(map[string]interface{}); ok {
reply, logs, err = fn.Invoke(e, v["context"], kind)
} else {
reply, logs, err = fn.Invoke(v, nil, kind)
}
if err != nil {
log.Fatalf("error response: %s", err)
}
if lv.Logs {
io.Copy(os.Stderr, logs)
}
io.Copy(os.Stdout, reply)
fmt.Fprintf(os.Stdout, "\n")
}
}