-
Notifications
You must be signed in to change notification settings - Fork 6
/
executor.go
171 lines (144 loc) · 4.02 KB
/
executor.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package commands
import (
"context"
"fmt"
"os"
"os/signal"
"time"
"github.com/UpCloudLtd/progress"
"github.com/UpCloudLtd/progress/messages"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/clierrors"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/config"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
internal "github.com/UpCloudLtd/upcloud-cli/v3/internal/service"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/service"
"github.com/gemalto/flume"
)
// Executor represents the execution context for commands
type Executor interface {
Context() context.Context
PushProgressUpdate(messages.Update)
PushProgressStarted(msg string)
PushProgressUpdateMessage(key, msg string)
PushProgressSuccess(msg string)
StopProgressLog()
WaitFor(waitFn func() error, timeout time.Duration) error
Server() service.Server
Storage() service.Storage
Network() service.Network
Firewall() service.Firewall
IPAddress() service.IPAddress
Account() service.Account
All() internal.AllServices
Debug(msg string, args ...interface{})
WithLogger(args ...interface{}) Executor
}
type executeResult struct {
Job int
Result output.Output
Error error
ResolvedArgument resolvedArgument
}
type executorImpl struct {
Config *config.Config
progress *progress.Progress
service internal.AllServices
logger flume.Logger
}
func (e executorImpl) WithLogger(args ...interface{}) Executor {
e.logger = e.logger.With(args...)
return &e
}
func (e *executorImpl) Context() context.Context {
return e.Config.Context()
}
func (e *executorImpl) Debug(msg string, args ...interface{}) {
e.logger.Debug(msg, args...)
}
func (e *executorImpl) WaitFor(waitFn func() error, timeout time.Duration) error {
rv := make(chan error)
timedOut := time.After(timeout)
go func() {
rv <- waitFn()
}()
select {
case returned := <-rv:
return returned
case <-timedOut:
return fmt.Errorf("timed out")
}
}
func (e *executorImpl) PushProgressUpdate(update messages.Update) {
err := e.progress.Push(update)
if err != nil {
e.Debug(fmt.Sprintf("Failed to push progress update: %s", err.Error()))
}
}
func (e *executorImpl) PushProgressStarted(msg string) {
e.PushProgressUpdate(messages.Update{
Message: msg,
Status: messages.MessageStatusStarted,
})
}
func (e *executorImpl) PushProgressUpdateMessage(key, msg string) {
e.PushProgressUpdate(messages.Update{
Key: key,
Message: msg,
})
}
func (e *executorImpl) PushProgressSuccess(msg string) {
e.PushProgressUpdate(messages.Update{
Key: msg,
Status: messages.MessageStatusSuccess,
})
}
func (e *executorImpl) StopProgressLog() {
e.progress.Stop()
}
func (e executorImpl) Server() service.Server {
return e.service
}
func (e executorImpl) Storage() service.Storage {
return e.service
}
func (e executorImpl) Network() service.Network {
return e.service
}
func (e executorImpl) Firewall() service.Firewall {
return e.service
}
func (e executorImpl) IPAddress() service.IPAddress {
return e.service
}
func (e executorImpl) Account() service.Account {
return e.service
}
func (e executorImpl) All() internal.AllServices {
return e.service
}
// NewExecutor creates the default Executor
func NewExecutor(cfg *config.Config, svc internal.AllServices, logger flume.Logger) Executor {
executor := &executorImpl{
Config: cfg,
progress: progress.NewProgress(config.GetProgressOutputConfig()),
logger: logger,
service: svc,
}
executor.progress.Start()
// Handle possible interrupts during execution
sigintChan := make(chan os.Signal, 1)
signal.Notify(sigintChan, os.Interrupt)
go func() {
<-sigintChan
// Cancel the app context
cfg.Cancel()
executor.PushProgressUpdate(messages.Update{
Message: "Execution cancelled with interrupt signal",
Details: "If you tried to create, modify, or delete resource(s), note that the operation might be already in progress. Please check the status of related resource(s).",
Status: messages.MessageStatusWarning,
})
executor.StopProgressLog()
os.Exit(clierrors.InterruptSignalCode)
}()
return executor
}