-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
140 lines (112 loc) · 3.72 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
package k6exec
import (
"fmt"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"syscall"
"github.com/adrg/xdg"
"github.com/gregjones/httpcache"
"github.com/gregjones/httpcache/diskcache"
)
// Options contains the optional parameters of the Command function.
type Options struct {
// AppName contains the name of the application. It is used to define the default value of CacheDir.
// If empty, it defaults to os.Args[0].
AppName string
// CacheDir specifies the name of the directory where the cacheable files can be cached.
// Its default is determined based on the XDG Base Directory Specification.
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
CacheDir string
// StateDir specifies the name of the directory where the k6 running state is stored,
// including the k6 binary and extension catalog. Each execution has a sub-directory,
// which is deleted after successful execution.
// Its default is determined based on the XDG Base Directory Specification.
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
StateDir string
// Client is used during HTTP communication with the build service.
// If absent, http.DefaultClient will be used.
Client *http.Client
// ExtensionCatalogURL contains the URL of the k6 extension catalog to be used.
// If absent, DefaultExtensionCatalogURL will be used.
ExtensionCatalogURL *url.URL
// BuildServiceURL contains the URL of the k6 build service to be used.
// If the value is not nil, the k6 binary is built using the build service instead of the local build.
BuildServiceURL *url.URL
}
// DefaultExtensionCatalogURL contains the address of the default k6 extension catalog.
const DefaultExtensionCatalogURL = "https://registry.k6.io/catalog.json"
func (o *Options) appname() string {
if o != nil && len(o.AppName) > 0 {
return o.AppName
}
return filepath.Base(os.Args[0]) //nolint:forbidigo
}
func (o *Options) client() (*http.Client, error) {
if o != nil && o.Client != nil {
return o.Client, nil
}
cachedir, err := o.cacheDir()
if err != nil {
return nil, err
}
dir := filepath.Join(cachedir, "http")
err = os.MkdirAll(dir, syscall.S_IRUSR|syscall.S_IWUSR|syscall.S_IXUSR) //nolint:forbidigo
if err != nil {
return nil, fmt.Errorf("%w: %s", ErrCache, err.Error())
}
transport := httpcache.NewTransport(diskcache.New(dir))
return &http.Client{Transport: transport}, nil
}
func (o *Options) extensionCatalogURL() *url.URL {
if o != nil && o.ExtensionCatalogURL != nil {
return o.ExtensionCatalogURL
}
loc, _ := url.Parse(DefaultExtensionCatalogURL)
return loc
}
func (o *Options) xdgDir(option string, xdgfunc func(string) (string, error), e error) (string, error) {
var xdgdir string
if o != nil && len(option) != 0 {
xdgdir = option
} else {
dir, err := xdgfunc(o.appname())
if err != nil {
return "", fmt.Errorf("%w: %s", e, err.Error())
}
xdgdir = dir
}
err := os.MkdirAll(xdgdir, syscall.S_IRUSR|syscall.S_IWUSR|syscall.S_IXUSR) //nolint:forbidigo
if err != nil {
return "", fmt.Errorf("%w: %s", e, err.Error())
}
return xdgdir, nil
}
func (o *Options) cacheDir() (string, error) {
var option string
if o != nil {
option = o.CacheDir
}
return o.xdgDir(option, xdg.CacheFile, ErrCache)
}
func (o *Options) stateDir() (string, error) {
var option string
if o != nil {
option = o.StateDir
}
return o.xdgDir(option, xdg.StateFile, ErrState)
}
func (o *Options) stateSubdir() (string, error) {
dir, err := o.stateDir()
if err != nil {
return "", err
}
dir = filepath.Join(dir, strconv.Itoa(os.Getpid())) //nolint:forbidigo
err = os.MkdirAll(dir, syscall.S_IRUSR|syscall.S_IWUSR|syscall.S_IXUSR) //nolint:forbidigo
if err != nil {
return "", err
}
return dir, nil
}