-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.go
104 lines (87 loc) · 2.67 KB
/
read.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
package main
import (
"fmt"
"github.com/jeffrom/logd/internal"
"github.com/jeffrom/logd/logd"
"github.com/jeffrom/logd/protocol"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var showLineNumbers bool
func init() {
pflags := ReadCmd.PersistentFlags()
dconf := logd.DefaultConfig
pflags.IntVar(&tmpConfig.Limit, "limit", dconf.Limit, "limit minimum number of messages per read to `MESSAGES`")
pflags.Uint64Var(&tmpConfig.Offset, "offset", dconf.Offset, "start reading messages from `OFFSET`")
pflags.BoolVarP(&tmpConfig.ReadForever, "read-forever", "F", dconf.ReadForever, "Keep reading input until the program is killed")
pflags.StringVar(&topicFlag, "topic", "default", "a `TOPIC` for the read")
pflags.IntVar(&tmpConfig.ConnRetries, "retries", dconf.ConnRetries, "total number of connection retries")
pflags.DurationVar(&tmpConfig.ConnRetryInterval, "retry-interval", dconf.ConnRetryInterval, "initial retry interval duration")
pflags.Float64Var(&tmpConfig.ConnRetryMultiplier, "retry-multiplier", dconf.ConnRetryMultiplier, "retry interval multiplier")
pflags.DurationVar(&tmpConfig.ConnRetryMaxInterval, "retry-max-interval", dconf.ConnRetryMaxInterval, "maximum retry interval")
pflags.BoolVarP(&showLineNumbers, "numbers", "n", false, "print offset and delta with messages")
}
var ReadCmd = &cobra.Command{
Use: "read",
Aliases: []string{"r"},
Short: "Read messages from the log",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
internal.Debugf(tmpConfig.ToGeneralConfig(), "%+v", tmpConfig)
if err := doRead(tmpConfig, cmd); err != nil {
panic(err)
}
},
}
func doRead(conf *logd.Config, c *cobra.Command) error {
done := make(chan struct{})
handleKills(done)
conf.UseTail = !c.PersistentFlags().Lookup("offset").Changed
if conf.ReadForever {
conf.Limit = 1000
}
out, err := getFile(conf.OutputPath, false)
if err != nil {
return err
}
if out != nil {
defer out.Close()
}
scanner, err := logd.DialScannerConfig(conf.Host, conf)
if err != nil {
return err
}
defer scanner.Close()
t, err := c.PersistentFlags().GetString("topic")
if err != nil {
panic(err)
}
scanner.SetTopic(t)
go func() {
select {
case <-done:
scanner.Stop()
}
}()
n := 0
for scanner.Scan() {
if showLineNumbers {
fmt.Fprintf(out, "%d.%d ", scanner.Message().Offset, scanner.Message().Delta)
}
_, err := out.Write(scanner.Message().BodyBytes())
if err != nil && errors.Cause(err) == protocol.ErrNotFound {
break
}
internal.LogError(err)
_, err = out.Write([]byte("\n"))
internal.LogError(err)
n++
if !conf.ReadForever && n > conf.Limit {
break
}
}
if n == 0 {
fmt.Println(t, "topic is empty")
}
return nil
}