@@ -3,7 +3,9 @@ package read
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "os"
6
7
"strings"
8
+ "time"
7
9
8
10
"github.com/sirupsen/logrus"
9
11
"github.com/spf13/cobra"
@@ -12,12 +14,19 @@ import (
12
14
"terraform-provider-iterative/task/common"
13
15
)
14
16
17
+ type status string
18
+
19
+ const (
20
+ statusQueued status = "queued"
21
+ statusSucceeded status = "succeeded"
22
+ statusFailed status = "failed"
23
+ statusRunning status = "running"
24
+ )
25
+
15
26
type Options struct {
16
27
Parallelism int
17
28
Timestamps bool
18
- Status bool
19
- Events bool
20
- Logs bool
29
+ Follow bool
21
30
}
22
31
23
32
func New (cloud * common.Cloud ) * cobra.Command {
@@ -35,9 +44,7 @@ func New(cloud *common.Cloud) *cobra.Command {
35
44
36
45
cmd .Flags ().IntVar (& o .Parallelism , "parallelism" , 1 , "parallelism" )
37
46
cmd .Flags ().BoolVar (& o .Timestamps , "timestamps" , false , "display timestamps" )
38
- cmd .Flags ().BoolVar (& o .Status , "status" , true , "read status" )
39
- cmd .Flags ().BoolVar (& o .Logs , "logs" , false , "read logs" )
40
- cmd .MarkFlagsMutuallyExclusive ("status" , "logs" )
47
+ cmd .Flags ().BoolVar (& o .Follow , "follow" , false , "follow logs" )
41
48
42
49
return cmd
43
50
}
@@ -50,7 +57,6 @@ func (o *Options) Run(cmd *cobra.Command, args []string, cloud *common.Cloud) er
50
57
}
51
58
52
59
ctx , cancel := context .WithTimeout (context .Background (), cloud .Timeouts .Read )
53
- defer cancel ()
54
60
55
61
id , err := common .ParseIdentifier (args [0 ])
56
62
if err != nil {
@@ -62,39 +68,70 @@ func (o *Options) Run(cmd *cobra.Command, args []string, cloud *common.Cloud) er
62
68
return err
63
69
}
64
70
65
- if err := tsk .Read (ctx ); err != nil {
66
- return err
67
- }
71
+ var last int
72
+ for {
73
+ if err := tsk .Read (ctx ); err != nil {
74
+ return err
75
+ }
68
76
69
- switch {
70
- case o .Logs :
71
- return o .printLogs (ctx , tsk )
72
- case o .Status :
73
- return o .printStatus (ctx , tsk )
74
- }
77
+ logs , err := o .getLogs (ctx , tsk )
78
+ if err != nil {
79
+ return err
80
+ }
81
+
82
+ status , err := o .getStatus (ctx , tsk )
83
+ if err != nil {
84
+ return err
85
+ }
75
86
76
- return nil
87
+ if delta := strings .Join (logs [last :], "\n " ); delta != "" {
88
+ fmt .Println (delta )
89
+ last = len (logs )
90
+ }
91
+
92
+ switch o .Follow {
93
+ case true :
94
+ // disable debug logs for subsequent iterations
95
+ logrus .SetLevel (logrus .WarnLevel )
96
+ // create a new context to reset timeout on every iteration
97
+ ctx , cancel = context .WithTimeout (context .Background (), cloud .Timeouts .Read )
98
+ defer cancel ()
99
+ case false :
100
+ return nil
101
+ }
102
+
103
+ switch status {
104
+ case statusSucceeded :
105
+ os .Exit (0 )
106
+ case statusFailed :
107
+ os .Exit (1 )
108
+ default :
109
+ time .Sleep (3 * time .Second )
110
+ }
111
+ }
77
112
}
78
113
79
- func (o * Options ) printLogs (ctx context.Context , tsk task.Task ) error {
114
+ func (o * Options ) getLogs (ctx context.Context , tsk task.Task ) ([] string , error ) {
80
115
logs , err := tsk .Logs (ctx )
81
116
if err != nil {
82
- return err
117
+ return nil , err
83
118
}
84
119
120
+ var result []string
121
+
85
122
for _ , log := range logs {
86
123
for _ , line := range strings .Split (strings .Trim (log , "\n " ), "\n " ) {
87
124
if ! o .Timestamps {
88
125
_ , line , _ = strings .Cut (line , " " )
89
126
}
90
- fmt . Println ( line )
127
+ result = append ( result , line )
91
128
}
92
129
}
93
130
94
- return nil
131
+ return result , nil
95
132
}
96
133
97
- func (o * Options ) printStatus (ctx context.Context , tsk task.Task ) error {
134
+ func (o * Options ) getStatus (ctx context.Context , tsk task.Task ) ( status , error ) {
98
135
for _ , event := range tsk .Events (ctx ) {
99
136
line := fmt .Sprintf ("%s: %s" , event .Code , strings .Join (event .Description , " " ))
100
137
if o .Timestamps {
@@ -106,21 +143,21 @@ func (o *Options) printStatus(ctx context.Context, tsk task.Task) error {
106
143
107
144
status , err := tsk .Status (ctx )
108
145
if err != nil {
109
- return err
146
+ return "" , err
110
147
}
111
148
112
- message := "queued"
149
+ result := statusQueued
113
150
114
- if status ["succeeded" ] >= o .Parallelism {
115
- message = "succeeded"
151
+ if status [common . StatusCodeSucceeded ] >= o .Parallelism {
152
+ result = statusSucceeded
116
153
}
117
- if status ["failed" ] > 0 {
118
- message = "failed"
154
+ if status [common . StatusCodeFailed ] > 0 {
155
+ result = statusFailed
119
156
}
120
- if status ["running" ] >= o .Parallelism {
121
- message = "running"
157
+ if status [common . StatusCodeActive ] >= o .Parallelism {
158
+ result = statusRunning
122
159
}
123
160
124
- fmt . Println ( message )
125
- return nil
161
+ logrus . Debug ( result )
162
+ return result , nil
126
163
}
0 commit comments