-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtask_options.go
More file actions
179 lines (156 loc) · 4.19 KB
/
Copy pathtask_options.go
File metadata and controls
179 lines (156 loc) · 4.19 KB
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
172
173
174
175
176
177
178
179
package utaskscheduler
import (
"context"
"fmt"
"syscall"
"time"
"github.com/google/uuid"
)
type taskOptions struct {
// common
UID uuid.UUID
timeout time.Duration
outputChannel *chan TaskOutput
progressChannel *chan TaskStatusUpdate
returnChannel *chan bool
meta interface{}
printStartAndEndInOutput bool
taskType TaskType
logger Logger
// functionTask
fn func(ctx context.Context, outputChannel chan string) int
// shellTask
shellCommand string
shellArgs []string
shellEnv []string
shellWorkingDir string
shellTermSignal syscall.Signal
shellDoNotKillOrphans bool
shellPanicIfLostControl bool
}
type TaskOption interface {
apply(*taskOptions) error
}
type funcTaskOption struct {
f func(*taskOptions) error
}
func (fdo *funcTaskOption) apply(do *taskOptions) error {
return fdo.f(do)
}
func newFuncTaskOption(f func(*taskOptions) error) *funcTaskOption {
return &funcTaskOption{f: f}
}
func WithUID(uid uuid.UUID) TaskOption {
return newFuncTaskOption(func(o *taskOptions) error {
o.UID = uid
return nil
})
}
func WithTimeout(timeout time.Duration) TaskOption {
return newFuncTaskOption(func(o *taskOptions) error {
o.timeout = timeout
return nil
})
}
func WithOutputChannel(outputChannel chan TaskOutput) TaskOption {
return newFuncTaskOption(func(o *taskOptions) error {
o.outputChannel = &outputChannel
return nil
})
}
func WithProgressChannel(progressChannel chan TaskStatusUpdate) TaskOption {
return newFuncTaskOption(func(o *taskOptions) error {
o.progressChannel = &progressChannel
return nil
})
}
func WithReturnChannel(returnChannel chan bool) TaskOption {
return newFuncTaskOption(func(o *taskOptions) error {
o.returnChannel = &returnChannel
return nil
})
}
func WithMeta(meta interface{}) TaskOption {
return newFuncTaskOption(func(o *taskOptions) error {
o.meta = meta
return nil
})
}
func WithPrintStartAndEndInOutput() TaskOption {
return newFuncTaskOption(func(o *taskOptions) error {
o.printStartAndEndInOutput = true
return nil
})
}
func WithLogger(logger Logger) TaskOption {
return newFuncTaskOption(func(o *taskOptions) error {
o.logger = logger
return nil
})
}
func WithFunction(fn func(ctx context.Context, outputChannel chan string) int) TaskOption {
return newFuncTaskOption(func(o *taskOptions) error {
if o.shellCommand != "" || len(o.shellArgs) != 0 {
return fmt.Errorf("cannot set both function and shell command")
}
o.taskType = TASK_TYPE_FUNCTION
o.fn = fn
return nil
})
}
func WithShell(command string, args ...string) TaskOption {
return newFuncTaskOption(func(o *taskOptions) error {
if o.fn != nil {
return fmt.Errorf("cannot set both function and shell command")
}
o.taskType = TASK_TYPE_SHELL
o.shellCommand = command
o.shellArgs = args
return nil
})
}
func WithShellEnvironment(env []string) TaskOption {
return newFuncTaskOption(func(o *taskOptions) error {
if o.fn != nil {
return fmt.Errorf("ShellEnvironment is not compatible with function")
}
o.shellEnv = env
return nil
})
}
func WithShellWorkingDir(workingDir string) TaskOption {
return newFuncTaskOption(func(o *taskOptions) error {
if o.fn != nil {
return fmt.Errorf("ShellWorkingDir is not compatible with function")
}
o.shellWorkingDir = workingDir
return nil
})
}
func WithShellTermSignal(termSignal syscall.Signal) TaskOption {
return newFuncTaskOption(func(o *taskOptions) error {
if o.fn != nil {
return fmt.Errorf("ShellTermSignal is not compatible with function")
}
o.shellTermSignal = termSignal
return nil
})
}
func WithShellDoNotKillOrphans() TaskOption {
return newFuncTaskOption(func(o *taskOptions) error {
if o.fn != nil {
return fmt.Errorf("ShellDoNotKillOrphans is not compatible with function")
}
o.shellDoNotKillOrphans = true
return nil
})
}
func WithShellPanicIfLostControl() TaskOption {
return newFuncTaskOption(func(o *taskOptions) error {
if o.fn != nil {
return fmt.Errorf("ShellPanicIfLostControl is not compatible with function")
}
o.shellPanicIfLostControl = true
return nil
})
}