-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
152 lines (126 loc) · 3.01 KB
/
options.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
/*
Copyright 2024 The bee Authors
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library;
*/
package bee
import (
"context"
"os"
"path/filepath"
"runtime"
"github.com/olive-io/bpmn/tracing"
"go.uber.org/zap"
"github.com/olive-io/bee/plugins/callback"
"github.com/olive-io/bee/plugins/filter"
)
var (
DefaultParallel = runtime.NumCPU() * 2
)
type Callable func(ctx context.Context, host, action string, in []byte, opts ...RunOption) ([]byte, error)
type Options struct {
dir string
parallel int
check bool
logger *zap.Logger
caller Callable
}
func newOptions() *Options {
home, _ := os.UserHomeDir()
options := Options{
dir: filepath.Join(home, ".bee"),
parallel: DefaultParallel,
logger: zap.NewExample(),
}
return &options
}
type Option func(*Options)
func SetDir(dir string) Option {
return func(opt *Options) {
opt.dir = dir
}
}
func SetParallel(parallel int) Option {
return func(opt *Options) {
opt.parallel = parallel
}
}
func SetCheck(check bool) Option {
return func(opt *Options) {
opt.check = check
}
}
func SetLogger(lg *zap.Logger) Option {
return func(opt *Options) {
opt.logger = lg
}
}
func SetCaller(caller Callable) Option {
return func(opt *Options) {
opt.caller = caller
}
}
type RunOptions struct {
Callback callback.ICallBack
Filter filter.IFilter
Tracer chan tracing.ITrace
Metadata map[string]any
ExtraArgs map[string]string
sync bool
}
func newRunOptions() *RunOptions {
options := RunOptions{
Callback: callback.NewCallBack(),
Filter: filter.NewFilter(),
}
return &options
}
type RunOption func(*RunOptions)
func WithRunSync(b bool) RunOption {
return func(opt *RunOptions) {
opt.sync = b
}
}
func WithRunCallback(cb callback.ICallBack) RunOption {
return func(opt *RunOptions) {
opt.Callback = cb
}
}
func WithRunFilter(f filter.IFilter) RunOption {
return func(opt *RunOptions) {
opt.Filter = f
}
}
func WithRunTracer(tracer chan tracing.ITrace) RunOption {
return func(opt *RunOptions) {
opt.Tracer = tracer
}
}
func WithMetadata(md map[string]any) RunOption {
return func(opt *RunOptions) {
if opt.Metadata == nil {
opt.Metadata = map[string]any{}
}
for key, value := range md {
opt.Metadata[key] = value
}
}
}
func WithArgs(args map[string]string) RunOption {
return func(opt *RunOptions) {
if opt.ExtraArgs == nil {
opt.ExtraArgs = map[string]string{}
}
for key, value := range args {
opt.ExtraArgs[key] = value
}
}
}