-
Notifications
You must be signed in to change notification settings - Fork 5
/
sqslite.go
100 lines (89 loc) · 1.98 KB
/
sqslite.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
package main
import (
"encoding/json"
"encoding/xml"
"flag"
"io/ioutil"
"os"
"github.com/crowdmob/goamz/sqs"
)
// formatResp takes the response and formats it in JSON or XML.
func formatResp(format string, resp interface{}) ([]byte, error) {
if format == "json" {
return json.Marshal(resp)
}
return xml.Marshal(resp)
}
func main() {
access := os.Getenv("AWS_ACCESS_KEY_ID")
secret := os.Getenv("AWS_SECRET_ACCESS_KEY")
cmd := flag.String("c", "r", "command (r=receive, s=send, d=delete)")
qName := flag.String("q", "", "queue name")
region := flag.String("re", "us-east-1", "region")
format := flag.String("f", "xml", "response format (xml or json)")
maxNumberOfMessages := flag.Int("mN", 1, "maximum messages")
flag.Parse()
// Check required environment variables
if access == "" {
panic("AWS_ACCESS_KEY_ID is undefined")
}
if secret == "" {
panic("AWS_SECRET_ACCESS_KEY is undefined")
}
// If required flags are are not filled
if *qName == "" || *region == "" {
flag.PrintDefaults()
os.Exit(1)
}
var b []byte
var err error
if *cmd == "s" || *cmd == "d" {
b, err = ioutil.ReadAll(os.Stdin)
if err != nil {
panic(err)
}
}
c, err := sqs.NewFrom(access, secret, *region)
if err != nil {
panic(err)
}
q, err := c.GetQueue(*qName)
if err != nil {
panic(err)
}
if *cmd == "s" {
resp, err := q.SendMessage(string(b))
if err != nil {
panic(err)
}
b, err := formatResp(*format, resp)
if err != nil {
panic(err)
}
os.Stdout.Write(b)
} else if *cmd == "r" {
resp, err := q.ReceiveMessage(*maxNumberOfMessages)
if err != nil {
panic(err)
}
b, err := formatResp(*format, resp)
if err != nil {
panic(err)
}
os.Stdout.Write(b)
} else if *cmd == "d" {
m := &sqs.Message{ReceiptHandle: string(b)}
resp, err := q.DeleteMessage(m)
if err != nil {
panic(err)
}
b, err := formatResp(*format, resp)
if err != nil {
panic(err)
}
os.Stdout.Write(b)
} else {
flag.PrintDefaults()
panic("Invalid command")
}
}