-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
50 lines (40 loc) · 1.29 KB
/
command.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
package k6exec
import (
"context"
"errors"
"fmt"
"os/exec"
"path/filepath"
"github.com/grafana/k6deps"
)
// Command returns the exec.Cmd struct to execute k6 with the given dependencies and arguments.
func Command(ctx context.Context, args []string, deps k6deps.Dependencies, opts *Options) (*exec.Cmd, error) {
dir, err := opts.stateSubdir()
if err != nil {
return nil, fmt.Errorf("%w: %s", ErrState, err.Error())
}
exe := filepath.Join(dir, k6binary)
loc, err := build(ctx, deps, opts)
if err != nil {
return nil, fmt.Errorf("%w: %s", ErrBuild, err.Error())
}
client, err := opts.client()
if err != nil {
return nil, fmt.Errorf("%w: %s", ErrDownload, err.Error())
}
if err = download(ctx, loc, exe, client); err != nil {
return nil, fmt.Errorf("%w: %s", ErrDownload, err.Error())
}
cmd := exec.CommandContext(ctx, exe, args...) //nolint:gosec
return cmd, nil
}
var (
// ErrDownload is returned if an error occurs during download.
ErrDownload = errors.New("download error")
// ErrBuild is returned if an error occurs during build.
ErrBuild = errors.New("build error")
// ErrCache is returned if an error occurs during cache handling.
ErrCache = errors.New("cache error")
// ErrState is returned if an error occurs during state handling.
ErrState = errors.New("state error")
)