-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoptions.go
186 lines (130 loc) · 2.73 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
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
180
181
182
183
184
185
186
package runner
import (
"context"
"fmt"
"io/ioutil"
"os"
)
type Option func(options *Options)
type Options struct {
Version string
Timeout int64
Out string
NoTruncate bool
tempOut bool
DumpResult string
tempDumpResult bool
v8path string
Context context.Context
customValues []string
commonValues []string
}
func (ro *Options) Option(fn Option) {
fn(ro)
}
func (ro *Options) setCustomValue(key, sep, value string) {
ro.customValues = append(ro.customValues, fmt.Sprintf("%s%s%s", key, sep, value))
}
func (ro *Options) Options(opts ...Option) {
for _, fn := range opts {
ro.Option(fn)
}
}
func (ro Options) Values() []string {
outValue := ro.Out
if ro.NoTruncate {
outValue += " -NoTruncate"
}
ro.setCustomValue("/Out", " ", outValue)
ro.setCustomValue("/DumpResult", " ", ro.DumpResult)
return ro.customValues
}
func (ro *Options) NewOutFile() {
tempLog, _ := ioutil.TempFile("", "v8_log_*.txt")
ro.Out = tempLog.Name()
ro.tempOut = true
tempLog.Close()
}
func (ro *Options) RemoveOutFile() {
_ = os.Remove(ro.Out)
}
func (ro *Options) NewDumpResultFile() {
tempLog, _ := ioutil.TempFile("", "v8_DumpResult_*.txt")
ro.DumpResult = tempLog.Name()
ro.tempDumpResult = true
tempLog.Close()
}
func (ro *Options) RemoveDumpResultFile() {
_ = os.Remove(ro.DumpResult)
}
func (ro *Options) RemoveTempFiles() {
if ro.tempDumpResult {
_ = os.Remove(ro.DumpResult)
}
if ro.tempOut {
_ = os.Remove(ro.Out)
}
}
func WithTimeout(timeout int64) Option {
return func(r *Options) {
r.Timeout = timeout
if r.Context == nil {
r.Context = context.Background()
}
}
}
func WithContext(ctx context.Context) Option {
return func(r *Options) {
r.Context = ctx
}
}
func WithOut(file string, noTruncate bool) Option {
return func(r *Options) {
r.Out = file
r.tempOut = false
r.NoTruncate = noTruncate
}
}
func WithPath(path string) Option {
return func(r *Options) {
r.v8path = path
}
}
func WithDumpResult(file string) Option {
return func(r *Options) {
r.DumpResult = file
r.tempDumpResult = false
}
}
func WithVersion(version string) Option {
return func(r *Options) {
r.Version = version
}
}
func WithCommonValues(cv []string) Option {
return func(r *Options) {
r.commonValues = cv
}
}
func WithCredentials(user, password string) Option {
return func(r *Options) {
if len(user) == 0 {
return
}
r.setCustomValue("/N", " ", user)
if len(password) > 0 {
r.setCustomValue("/P", " ", password)
}
}
}
func WithUnlockCode(uc string) Option {
return func(r *Options) {
if len(uc) == 0 {
return
}
r.setCustomValue("/UC", " ", uc)
}
}
func WithUC(uc string) Option {
return WithUnlockCode(uc)
}