-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
136 lines (110 loc) · 2.48 KB
/
main.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"github.com/creasty/aws-sns-push/aws"
"github.com/creasty/aws-sns-push/util"
"github.com/creasty/aws-sns-push/version"
)
var (
flagYes bool
flagVersion bool
)
func init() {
flag.BoolVar(&flagYes, "y", false, "Send without confirmation")
flag.BoolVar(&flagVersion, "v", false, "Show version")
flag.Usage = showHelp
flag.Parse()
}
func main() {
args := flag.Args()
if flagVersion {
showVersion()
return
}
if len(args) < 1 {
showHelp()
return
}
target := util.ParseTarget(args[0])
if target.Mode == util.TargetModeUnknown {
fmt.Fprintf(os.Stderr, "Invalid target: %q\n", target.String)
showHelp()
}
doSend(target)
}
func showVersion() {
fmt.Printf("%s (%s)\n", version.Version, version.Revision)
}
func showHelp() {
fmt.Printf(`Send SNS push notifications painlessly.
USAGE:
aws-sns-push [OPTIONS] TARGET
TARGET:
1. {application-name}/{user-id}
e.g., sample-prod/12345
2. {application-name}/{device-token}
e.g., sample-prod/ffffff
3. {endpoint-arn}
e.g., arn:aws:sns:ap-northeast-1:0000:endpoint/APNS/sample-prod/ffffff
OPTIONS:
-y Send without confirmation
-v Show version
-h Show help
`)
os.Exit(1)
}
func doSend(target util.Target) {
s := aws.NewSNS()
endpoints := make([]string, 0)
isPiped := util.IsPiped()
switch target.Mode {
case util.TargetModeEndpointArn:
endpoints = append(endpoints, target.EndpointArn)
case util.TargetModeUserID, util.TargetModeDeviceToken:
eps, err := s.FindEndpointsFor(target.ApplicationName, target.UserID, target.DeviceToken)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
endpoints = append(endpoints, eps...)
}
if !flagYes {
fmt.Println("==> Endpoints")
for _, ep := range endpoints {
fmt.Printf("- %s\n", ep)
}
}
if !isPiped {
fmt.Println("==> Enter JSON message (Ctrl-D)")
}
bytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
message := string(bytes)
if message == "" {
fmt.Fprintf(os.Stderr, "No message given\n")
os.Exit(1)
}
if !flagYes {
if isPiped {
fmt.Printf("==> Message\n%s\n", message)
}
if f, err := util.AskBool("Proceed to send"); !f {
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
} else {
fmt.Fprintf(os.Stderr, "canceled\n")
}
os.Exit(1)
}
}
if err := s.Send(endpoints, message); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}