-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions.go
251 lines (202 loc) · 6.09 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package k6deps
import (
"bytes"
"errors"
"io"
"os"
"path/filepath"
"github.com/grafana/k6pack"
)
// EnvDependencies holds the name of the environment variable thet describes additional dependencies.
const EnvDependencies = "K6_DEPENDENCIES"
// Source describes a generic dependency source.
// Such a source can be the k6 script, the manifest file, or an environment variable (e.g. K6_DEPENDENCIES).
type Source struct {
// Name contains the name of the source (file, environment variable, etc.).
Name string
// Reader provides streaming access to the source content as an alternative to Contents.
Reader io.Reader
// Contents contains the content of the source (e.g. script)
Contents []byte
// Ignore disables automatic search and processing of that source.
Ignore bool
}
// IsEmpty returns true if the source is empty.
func (s *Source) IsEmpty() bool {
return len(s.Contents) == 0 && s.Reader == nil && len(s.Name) == 0
}
func (s *Source) getReader() (io.Reader, func() error, error) {
if s.Reader != nil {
return s.Reader, nil, nil
}
if len(s.Contents) > 0 {
return bytes.NewReader(s.Contents), nil, nil
}
fileName, err := filepath.Abs(s.Name)
if err != nil {
return nil, nil, err
}
file, err := os.Open(filepath.Clean(fileName)) //nolint:forbidigo
if err != nil {
return nil, nil, err
}
return file, file.Close, nil
}
// Options contains the parameters of the dependency analysis.
type Options struct {
// Script contains the properties of the k6 test script to be analyzed.
Script Source
// Archive contains the properties of the k6 archive to be analyzed.
// If archive is specified, the other three sources will not be taken into account,
// since the archive may contain them.
// An archive is a tar file, which can be created using the k6 archive command, for example.
Archive Source
// Manifest contains the properties of the manifest file to be analyzed.
// If the Ignore property is not set and no manifest file is specified,
// the package.json file closest to the script is searched for.
Manifest Source
// Env contains the properties of the environment variable to be analyzed.
// If the Ignore property is not set and no variable is specified,
// the value of the variable named K6_DEPENDENCIES is read.
Env Source
// LookupEnv function is used to query the value of the environment variable
// specified in the Env option Name if the Contents of the Env option is empty.
// If empty, os.LookupEnv will be used.
LookupEnv func(key string) (value string, ok bool)
// FindManifest function is used to find manifest file for the given scriptfile
// if the Contents of Manifest option is empty.
// If the scriptfile parameter is empty, FindManifest starts searching
// for the manifest file from the current directory
// If missing, the closest manifest file will be used.
FindManifest func(scriptfile string) (contents []byte, filename string, ok bool, err error)
}
func (opts *Options) findManifest(filename string) ([]byte, string, bool, error) {
if opts.FindManifest != nil {
return opts.FindManifest(filename)
}
return findManifest(filename)
}
func (opts *Options) lookupEnv(key string) (string, bool) {
if opts.LookupEnv != nil {
return opts.LookupEnv(key)
}
return os.LookupEnv(key) //nolint:forbidigo
}
func loadSources(opts *Options) error {
if !opts.Archive.Ignore && !opts.Archive.IsEmpty() {
return loadArchive(opts)
}
if err := loadManifest(opts); err != nil {
return err
}
if err := loadScript(opts); err != nil {
return err
}
loadEnv(opts)
return nil
}
func loadManifest(opts *Options) error {
if len(opts.Manifest.Name) == 0 && !opts.Manifest.Ignore {
pkg, pkgfile, found, err := opts.findManifest(opts.Script.Name)
if err != nil {
return err
}
if found {
opts.Manifest.Name = pkgfile
opts.Manifest.Contents = pkg
}
return nil
}
if len(opts.Manifest.Name) == 0 || len(opts.Manifest.Contents) > 0 || opts.Manifest.Ignore {
return nil
}
pkg, err := os.ReadFile(opts.Manifest.Name) //nolint:forbidigo
if err != nil {
return err
}
opts.Manifest.Contents = pkg
return nil
}
func loadScript(opts *Options) error {
if len(opts.Script.Name) == 0 || len(opts.Script.Contents) > 0 || opts.Script.Ignore {
return nil
}
scriptfile, err := filepath.Abs(opts.Script.Name)
if err != nil {
return err
}
contents, err := os.ReadFile(scriptfile) //nolint:forbidigo,gosec
if err != nil {
return err
}
script, _, err := k6pack.Pack(string(contents), &k6pack.Options{Filename: scriptfile})
if err != nil {
return err
}
opts.Script.Name = scriptfile
opts.Script.Contents = script
return nil
}
func loadEnv(opts *Options) {
if len(opts.Env.Contents) > 0 || opts.Env.Ignore {
return
}
key := opts.Env.Name
if len(key) == 0 {
key = EnvDependencies
}
value, found := opts.lookupEnv(key)
if !found || len(value) == 0 {
return
}
opts.Env.Name = key
opts.Env.Contents = []byte(value)
}
func findManifest(filename string) ([]byte, string, bool, error) {
if len(filename) == 0 {
filename = "any_file"
}
abs, err := filepath.Abs(filename)
if err != nil {
return nil, "", false, err
}
for dir := filepath.Dir(abs); ; dir = filepath.Dir(dir) {
filename := filepath.Clean(filepath.Join(dir, "package.json"))
if _, err := os.Stat(filename); !errors.Is(err, os.ErrNotExist) { //nolint:forbidigo
contents, err := os.ReadFile(filename) //nolint:forbidigo
return contents, filename, err == nil, err
}
if dir[len(dir)-1] == filepath.Separator {
break
}
}
return nil, "", false, nil
}
//nolint:forbidigo
func loadArchive(opts *Options) error {
if opts.Archive.Ignore || opts.Archive.IsEmpty() {
return nil
}
reader, closer, err := opts.Archive.getReader()
if err != nil {
return err
}
if closer != nil {
defer closer() //nolint:errcheck
}
dir, err := os.MkdirTemp("", "k6deps-*")
if err != nil {
return err
}
defer os.RemoveAll(dir) //nolint:errcheck
err = extractArchive(dir, reader)
if err != nil {
return err
}
// archive should be self contained
opts.Script.Ignore = true
opts.Archive.Ignore = true
opts.Env.Ignore = true
opts.Manifest.Ignore = true
return loadMetadata(dir, opts)
}