-
Notifications
You must be signed in to change notification settings - Fork 4
/
watch_pipeline_log.go
79 lines (63 loc) · 1.68 KB
/
watch_pipeline_log.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
package pipeline
import (
"fmt"
"time"
"github.com/calyptia/cli/completer"
"github.com/spf13/cobra"
cloud "github.com/calyptia/api/types"
cfg "github.com/calyptia/cli/config"
)
func NewCmdWatchPipelineLogs(config *cfg.Config) *cobra.Command {
var interval int
completer := completer.Completer{Config: config}
cmd := &cobra.Command{
Use: "pipeline_log [pipeline name or id]",
Short: "Get a specific pipeline log",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
pipelineKey := args[0] // Pipeline ID or name is the first argument
pipelineID, err := completer.LoadPipelineID(pipelineKey)
if err != nil {
return
}
status := cloud.PipelineLogStatusDone
watched := make(map[string]bool)
// Function to fetch and display logs
fetchLogs := func() {
ff, err := config.Cloud.PipelineLogs(config.Ctx, cloud.ListPipelineLogs{
PipelineID: pipelineID,
Status: &status,
})
if err != nil {
fmt.Println("Error fetching pipeline logs:", err)
return
}
for _, log := range ff.Items {
if _, ok := watched[log.ID]; !ok {
fmt.Println(log.Logs)
watched[log.ID] = true
}
}
}
// Immediate log fetching if interval is not set
if interval <= 0 {
fetchLogs()
return
}
// Fetch logs every 'interval' seconds, similar to Unix watch
ticker := time.NewTicker(time.Duration(interval) * time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
fetchLogs()
case <-config.Ctx.Done():
return
}
}
},
}
flags := cmd.Flags()
flags.IntVarP(&interval, "interval", "i", 5, "Interval in seconds to fetch the logs")
return cmd
}