-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.go
80 lines (70 loc) · 2.53 KB
/
build.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
// Package k6build defines a service for building k6 binaries
package k6build
import (
"bytes"
"context"
"errors"
"fmt"
)
var ErrBuildFailed = errors.New("build failed") //nolint:revive
// Dependency defines a dependency and its semantic version constrains
type Dependency struct {
// Name is the name of the dependency.
Name string `json:"name,omitempty"`
// Constraints specifies the semantic version constraints. E.g. >v0.2.0
Constraints string `json:"constraints,omitempty"`
}
// Module defines the mapping of a Dependency to a go module that satisfies it
type Module struct {
// Path is the go module path
Path string `json:"path,omitempty"`
// Version is the go module version
Version string `json:"version,omitempty"`
}
// Artifact defines the metadata of binary that satisfies a set of dependencies
// including a URL for downloading it.
type Artifact struct {
// Unique id. Binaries satisfying the same set of dependencies have the same ID
ID string `json:"id,omitempty"`
// URL to fetch the artifact's binary
URL string `json:"url,omitempty"`
// List of dependencies that the artifact provides
Dependencies map[string]string `json:"dependencies,omitempty"`
// platform
Platform string `json:"platform,omitempty"`
// binary checksum (sha256)
Checksum string `json:"checksum,omitempty"`
}
// String returns a text serialization of the Artifact
func (a Artifact) String() string {
return a.toString(true, " ")
}
// Print returns a string with a pretty print of the artifact
func (a Artifact) Print() string {
return a.toString(true, "\n")
}
// PrintSummary returns a string with a pretty print of the artifact
func (a Artifact) PrintSummary() string {
return a.toString(false, "\n")
}
// Print returns a text serialization of the Artifact
func (a Artifact) toString(details bool, sep string) string {
buffer := &bytes.Buffer{}
if details {
buffer.WriteString(fmt.Sprintf("id: %s%s", a.ID, sep))
}
buffer.WriteString(fmt.Sprintf("platform: %s%s", a.Platform, sep))
for dep, version := range a.Dependencies {
buffer.WriteString(fmt.Sprintf("%s:%q%s", dep, version, sep))
}
buffer.WriteString(fmt.Sprintf("checksum: %s%s", a.Checksum, sep))
if details {
buffer.WriteString(fmt.Sprintf("url: %s%s", a.URL, sep))
}
return buffer.String()
}
// BuildService defines the interface for building custom k6 binaries
type BuildService interface {
// Build returns a k6 Artifact that satisfies a set dependencies and version constrains.
Build(ctx context.Context, platform string, k6Constrains string, deps []Dependency) (Artifact, error)
}