-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
200 lines (177 loc) · 4.12 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"fmt"
"os"
"github.com/urfave/cli"
)
const VERSION = "0.0.3"
type Args struct {
NoColor bool
Start string
End string
}
var awsCommandFlags = []cli.Flag{
cli.StringFlag{
Name: "aws-access-key-id",
Usage: "aws access key id",
EnvVar: "AWS_ACCESS_KEY_ID",
},
cli.StringFlag{
Name: "aws-secret-access-key",
Usage: "aws secret access key",
EnvVar: "AWS_SECRET_ACCESS_KEY",
},
cli.StringFlag{
Name: "aws-session-token",
Usage: "aws session token",
EnvVar: "AWS_SESSION_TOKEN",
},
cli.StringFlag{
Name: "profile",
Usage: "aws profile",
EnvVar: "AWS_PROFILE",
},
cli.StringFlag{
Name: "aws-region",
Usage: "aws region",
EnvVar: "AWS_REGION",
},
}
var commandGet = cli.Command{
Name: "get",
Usage: "get log stream",
ArgsUsage: "log_group_name log_stream_name",
Action: runGet,
Flags: append(awsCommandFlags, []cli.Flag{
cli.StringFlag{
Name: "f,filter-pattern",
Usage: "A valid CloudWatch Logs filter `PATTERN` to use for filtering the response. If not provided, all the events are matched."},
cli.BoolFlag{Name: "w,watch", Usage: "Query for new log lines constantly"},
cli.BoolFlag{Name: "G,no-group", Usage: "Do not display group name"},
cli.BoolFlag{Name: "S,no-stream", Usage: "Do not display stream name"},
cli.BoolFlag{
Name: "timestamp",
Usage: "Add creation timestamp to the output"},
cli.BoolFlag{
Name: "ingestion-time",
Usage: "Add ingestion time to the output"},
cli.StringFlag{
Name: "s,start",
Value: "5m",
Usage: "`START` time"},
cli.StringFlag{Name: "e,end", Usage: "`END` time"},
cli.BoolFlag{Name: "no-color", Usage: "Do not color output"},
// cli.StringFlag{Name: "q,query", Usage: "JMESPath `QUERY` to use in filtering the response data"},
}...),
}
var commandListGroups = cli.Command{
Name: "groups",
Usage: "list log groups",
Action: runListGroups,
Flags: append(awsCommandFlags, []cli.Flag{
cli.StringFlag{
Name: "p,log-group-prefix",
Value: "/",
Usage: "List only groups matching the `PREFIX`"},
}...),
}
var commandListStreams = cli.Command{
Name: "streams",
Usage: "list log stream",
ArgsUsage: "log_group_name",
Action: runListStreams,
Flags: append(awsCommandFlags, []cli.Flag{
cli.StringFlag{
Name: "s,start",
Value: "1h",
Usage: "`START` time"},
cli.StringFlag{Name: "e,end", Usage: "`END` time"},
cli.StringFlag{
Name: "p,log-stream-prefix",
Usage: "List only stream matching the prefix"},
}...),
}
func main() {
app := cli.NewApp()
app.Name = "awslogsgo"
app.Usage = "AWS Cloudwatch Logs Reader"
app.Version = VERSION
app.Commands = []cli.Command{
commandGet,
commandListGroups,
commandListStreams,
}
err := app.Run(os.Args)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func runGet(c *cli.Context) error {
start, err := parseTime(c.String("start"))
if err != nil {
return err
}
end, err := parseTime(c.String("end"))
if err != nil {
return err
}
filter := c.String("f")
group := c.Args().Get(0)
streamPrefix := c.Args().Get(1)
ac, err := awsConfig(awsConfigArgs(c))
if err != nil {
return err
}
r, err := NewCloudwatchLogsReader(ac, group, streamPrefix, filter, start, end)
if err != nil {
return err
}
stream, err := r.Stream(c.Bool("w"))
if err != nil {
return err
}
w, err := NewStdoutWriter(
c.Bool("no-color"),
c.Bool("G"),
c.Bool("S"),
c.Bool("timestamp"),
c.Bool("ingestion-time"),
)
if err != nil {
return err
}
return w.Write(stream)
}
func runListGroups(c *cli.Context) error {
prefix := c.String("p")
ac, err := awsConfig(awsConfigArgs(c))
if err != nil {
return err
}
return ListGroup(ac, prefix)
}
func runListStreams(c *cli.Context) error {
group := c.Args().Get(0)
prefix := c.String("p")
start, err := parseTime(c.String("start"))
if err != nil {
return err
}
end, err := parseTime(c.String("end"))
if err != nil {
return err
}
ac, err := awsConfig(awsConfigArgs(c))
if err != nil {
return err
}
st, err := ListStreams(ac, group, prefix, start, end)
if err != nil {
return err
}
for _, s := range st {
fmt.Println(s)
}
return nil
}