-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
179 lines (141 loc) · 4.11 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
package main
import (
"bufio"
"context"
"fmt"
"time"
"github.com/alecthomas/kong"
"github.com/araddon/dateparse"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/elbv2"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/eminugurkenar/tailb/between"
"github.com/eminugurkenar/tailb/loadbalancer"
"github.com/eminugurkenar/tailb/output"
"github.com/eminugurkenar/tailb/parse"
"github.com/eminugurkenar/tailb/read"
)
func printTable(lbs []*loadbalancer.LoadBalancer) {
var maxNameL int
var maxKindL int
var maxArnResourceL int
for _, lb := range lbs {
if len(lb.GetName()) > maxNameL {
maxNameL = len(lb.GetName())
}
if len(lb.GetKind()) > maxKindL {
maxKindL = len(lb.GetName())
}
if len(lb.GetArnResource()) > maxArnResourceL {
maxArnResourceL = len(lb.GetArnResource())
}
}
maxNameL += 3
maxKindL += 3
maxArnResourceL += 3
for i, lb := range lbs {
if i == 0 { // table header
fmt.Printf("%-*s ", maxKindL, "Type")
fmt.Printf("%-*s ", maxNameL, "Name")
fmt.Printf("%-*s ", maxArnResourceL, "Arn")
fmt.Printf("%s \n", "Log Storage")
}
fmt.Printf("%-*s ", maxKindL, lb.GetKind())
fmt.Printf("%-*s ", maxNameL, lb.GetName())
fmt.Printf("%-*s ", maxArnResourceL, lb.GetArnResource())
fmt.Printf("%s \n", lb.GetLogStorage())
}
}
func listLoadBalancers(ctx context.Context, s *session.Session) error {
elbc := elbv2.New(s)
lbf := loadbalancer.NewLoadBalancerFinder(elbc)
lbs, err := lbf.ListLoadBalancers(ctx)
if err != nil {
return err
}
printTable(lbs)
return nil
}
func tail(ctx context.Context, s *session.Session, lbn string, start, end time.Time, fields []string) error {
elbc := elbv2.New(s)
s3c := s3.New(s)
lbf := loadbalancer.NewLoadBalancerFinder(elbc)
lb, err := lbf.GetLoadBalancer(ctx, lbn)
if err != nil {
return err
}
b := between.NewBetween(start, end)
days := b.ListDays()
var prefixes []string
for _, d := range days {
prefixes = append(prefixes, fmt.Sprintf("%s/%s", lb.GetAccessLogPrefix(), d.Format("2006/01/02")))
}
p, err := parse.NewParser(lb.GetKind())
if err != nil {
return err
}
of, err := output.DefaultOutputFormatOptions(lb.GetKind())
if err != nil {
return err
}
if len(fields) > 0 {
of, err = output.NewOutputFormatOptions(lb.GetKind(), fields)
}
o := output.NewOutputFormater("json", of)
lr := read.NewObjectStorageReader(s3c, lb.GetAccessLogBucket(), prefixes, ctx)
scanner := bufio.NewScanner(lr)
for scanner.Scan() {
log, err := p.Parse(scanner.Text())
if err != nil {
return err
}
flog, err := o.Format(log)
if err != nil {
return err
}
fmt.Println(string(flog))
}
if err := scanner.Err(); err != nil {
return err
}
return nil
}
type AnyDate string
func (a *AnyDate) Validate() error {
_, err := dateparse.ParseLocal(string(*a))
return err
}
var CLI struct {
Tail struct {
Loadbalancer string `arg:"" name:"loadbalancer" help:"Loadbalancer to tail."`
Start AnyDate `short:"s" default:"${start}" required help:"start date."`
End AnyDate `short:"e" help:"end date." default:"${end}." required`
Lines int64 `short:"n" default:"-1" required help:"number of lines to print, (default -1, till end)." name:"nlines"`
Fields []string `short:"f" help:"fields to be shown in output."`
} `cmd:"" name:"tail" default:"withargs" help:"tail logs of given loadbalancer (default command)."`
ListLoadBalancers struct {
} `cmd:"" name:"ls" help:"list loadbalancers."`
}
func main() {
s := session.New()
ctx := context.Background()
kctx := kong.Parse(&CLI,
kong.Vars{"start": time.Now().AddDate(0, 0, -1).Format("2006-01-02 15:04:05")},
kong.Vars{"end": time.Now().Format("2006-01-02 15:04:05")},
)
switch kctx.Command() {
case "tail <loadbalancer>":
//already validated dates
start, _ := dateparse.ParseLocal(string(CLI.Tail.Start))
end, _ := dateparse.ParseLocal(string(CLI.Tail.End))
if err := tail(ctx, s, CLI.Tail.Loadbalancer, start, end, CLI.Tail.Fields); err != nil {
fmt.Println(err)
}
case "ls":
if err := listLoadBalancers(ctx, s); err != nil {
fmt.Println(err)
}
default:
panic(kctx.Command())
}
}