/
conn_logs_tail.go
65 lines (55 loc) · 1.43 KB
/
conn_logs_tail.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
// Copyright (c) 2021, SailPoint Technologies, Inc. All rights reserved.
package connector
import (
"time"
connclient "github.com/sailpoint-oss/sailpoint-cli/cmd/connector/client"
"github.com/sailpoint-oss/sailpoint-cli/internal/client"
"github.com/spf13/cobra"
)
func newConnLogsTailCmd(client client.Client) *cobra.Command {
cmd := &cobra.Command{
Use: "tail",
Short: "Tail Logs",
Example: "sail logs tail",
RunE: func(cmd *cobra.Command, args []string) error {
if err := tailLogs(client, cmd); err != nil {
return err
}
return nil
},
}
return cmd
}
func tailLogs(spClient client.Client, cmd *cobra.Command) error {
handleLogs := func(logEvents *connclient.LogEvents, cmd *cobra.Command) error {
if err := printLogs(logEvents, cmd); err != nil {
return err
}
for _, l := range logEvents.Logs {
updateLastSeenTime(l.Timestamp)
}
return nil
}
for {
logInput.Filter.StartTime = nextFromTime()
if err := getAllLogs(spClient, cmd, handleLogs); err != nil {
return err
}
time.Sleep(2 * time.Second)
}
}
var lastSeenTime *int64
func updateLastSeenTime(ts time.Time) {
nextTimeMilli := ts.UnixMilli()
if lastSeenTime == nil || nextTimeMilli > *lastSeenTime {
lastSeenTime = &nextTimeMilli
}
}
func nextFromTime() *time.Time {
from := time.Now().Add(-5 * time.Minute)
if lastSeenTime != nil {
//to fetch from next millisecond
from = time.UnixMilli(*lastSeenTime + 1)
}
return &from
}