-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch.go
67 lines (60 loc) · 1.42 KB
/
watch.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
package commands
import (
"fmt"
"os"
"time"
"github.com/argoproj/pkg/errors"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
wfv1 "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo/workflow/util"
)
func NewWatchCommand() *cobra.Command {
var command = &cobra.Command{
Use: "watch WORKFLOW",
Short: "watch a workflow until it completes",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
cmd.HelpFunc()(cmd, args)
os.Exit(1)
}
InitWorkflowClient()
watchWorkflow(args[0])
},
}
return command
}
func watchWorkflow(name string) {
fieldSelector := fields.ParseSelectorOrDie(fmt.Sprintf("metadata.name=%s", name))
opts := metav1.ListOptions{
FieldSelector: fieldSelector.String(),
}
wf, err := wfClient.Get(name, metav1.GetOptions{})
errors.CheckError(err)
watchIf, err := wfClient.Watch(opts)
errors.CheckError(err)
ticker := time.NewTicker(time.Second)
for {
select {
case next := <-watchIf.ResultChan():
wf, _ = next.Object.(*wfv1.Workflow)
case <-ticker.C:
}
if wf == nil {
watchIf.Stop()
watchIf, err = wfClient.Watch(opts)
errors.CheckError(err)
continue
}
err := util.DecompressWorkflow(wf)
errors.CheckError(err)
print("\033[H\033[2J")
print("\033[0;0H")
printWorkflowHelper(wf, "")
if !wf.Status.FinishedAt.IsZero() {
break
}
}
watchIf.Stop()
}