This repository has been archived by the owner on May 1, 2024. It is now read-only.
forked from blmayer/awslambdarpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
awslambdarpc.go
111 lines (94 loc) · 2.63 KB
/
awslambdarpc.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
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
awslambdarpc is an utility to make requests to your local AWS Lambda.
This tool is a CLI, and running awslambdarpc help will show your options.
It uses the client package for the real interaction with AWS Lambda, you can
import and use it if you wish.
Usage:
awslambdarpc [options]
Available options:
-a, --address
the address of your local running function, defaults to localhost:8080
-e, --event
path to the event JSON to be used as input
-d, --data
data passed to the function as input, in JSON format, defaults to "{}"
help, -h, --help
show this help
To make a request to a lambda function running at localhost:3000 and passing
the contents of a file, events/input.json as payload:
awslambdarpc -a localhost:3000 -e events/input.json
You can do passing the data directly with the -d flag:
awslambdarpc -a localhost:3000 -d '{"body": "Hello World!"}'
*/
package main
import (
"os"
"github.com/chchaffin/awslambdarpc/client"
)
const help = `awslambdarpc is an utility to make requests to your local AWS Lambda
Usage:
awslambdarpc [options]
Available options:
-a
--address the address of your local running function, defaults to localhost:8080
-e
--event path to the event JSON to be used as input
-d
--data data passed to the function as input, in JSON format, defaults to "{}"
help
-h
--help show this help
Examples:
awslambdarpc -a localhost:3000 -e events/input.json
awslambdarpc -a localhost:3000 -d '{"body": "Hello World!"}'`
func main() {
addr := "localhost:8080"
payload := []byte("{}")
var eventFile string
for i := 1; i < len(os.Args); i++ {
switch os.Args[i] {
case "-a", "--address":
i++
addr = os.Args[i]
case "-e", "--event":
i++
eventFile = os.Args[i]
// Read event file
if os.Args[i] != "" {
f, err := os.Open(eventFile)
if err != nil {
os.Stderr.WriteString("error opening file: " + err.Error() + "\n")
os.Exit(-3)
}
fileInfo, _ := f.Stat()
content := make([]byte, fileInfo.Size())
n, err := f.Read(content)
if int64(n) != fileInfo.Size() {
os.Stderr.WriteString("error: could not read whole file" + "\n")
os.Exit(-4)
}
if err != nil {
os.Stderr.WriteString("error reading file: " + err.Error() + "\n")
os.Exit(-5)
}
payload = content
}
case "-d", "--data":
i++
payload = []byte(os.Args[i])
case "-h", "--help", "help":
println(help)
os.Exit(0)
default:
os.Stderr.WriteString("error: wrong argument\n")
println(help)
os.Exit(-1)
}
}
res, err := client.Invoke(addr, payload)
if err != nil {
os.Stderr.WriteString(err.Error() + "\n")
os.Exit(-2)
}
println(string(res))
}