-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathanalyze.go
95 lines (72 loc) · 1.83 KB
/
analyze.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
package k6exec
import (
"log/slog"
"os"
"strings"
"github.com/grafana/k6deps"
)
func analyze(args []string, opts *Options) (k6deps.Dependencies, error) {
depsOpts := newDepsOptions(args, opts)
// we call Analyze before logging because it will return the name of the manifest, in any
deps, err := k6deps.Analyze(depsOpts)
slog.Debug("analyzing sources", depsOptsAttrs(depsOpts)...)
if err == nil && len(deps) > 0 {
slog.Debug("found dependencies", "deps", deps.String())
}
return deps, err
}
func newDepsOptions(args []string, opts *Options) *k6deps.Options {
dopts := &k6deps.Options{
Env: opts.Env,
Manifest: opts.Manifest,
LookupEnv: opts.LookupEnv,
FindManifest: opts.FindManifest,
}
scriptname, hasScript := scriptArg(args)
if !hasScript {
return dopts
}
if _, err := os.Stat(scriptname); err != nil { //nolint:forbidigo
return dopts
}
if strings.HasSuffix(scriptname, ".tar") {
dopts.Archive.Name = scriptname
} else {
dopts.Script.Name = scriptname
}
return dopts
}
func scriptArg(args []string) (string, bool) {
if len(args) == 0 {
return "", false
}
cmd := args[0]
if cmd != "run" && cmd != "archive" && cmd != "inspect" && cmd != "cloud" {
return "", false
}
if len(args) == 1 {
return "", false
}
last := args[len(args)-1]
if last[0] == '-' {
return "", false
}
return last, true
}
func depsOptsAttrs(opts *k6deps.Options) []any {
attrs := []any{}
if opts.Manifest.Name != "" {
attrs = append(attrs, "Manifest", opts.Manifest.Name)
}
if opts.Archive.Name != "" {
attrs = append(attrs, "Archive", opts.Archive.Name)
}
// ignore script if archive is present
if opts.Archive.Name == "" && opts.Script.Name != "" {
attrs = append(attrs, "Script", opts.Script.Name)
}
if opts.Env.Name != "" {
attrs = append(attrs, "Env", opts.Env.Name)
}
return attrs
}