Skip to content

Commit

Permalink
rename allow-prereleases option (#64)
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Chacin <[email protected]>
  • Loading branch information
pablochacin authored Nov 28, 2024
1 parent 2219042 commit 70d5a3b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 52 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ k6build local -k v0.50.0 -e GOPROXY=http://localhost:80 -q
## Flags

```
--allow-prereleases allow building pre-releases.
--allow-build-semvers allow building versions with build metadata (e.g v0.0.0+build).
-c, --catalog string dependencies catalog (default "https://registry.k6.io/catalog.json")
-g, --copy-go-env copy go environment (default true)
-d, --dependency stringArray list of dependencies in form package:constrains
Expand Down Expand Up @@ -253,17 +253,17 @@ k6build server -e GOPROXY=http://localhost:80
## Flags

```
--allow-prereleases allow building pre-releases.
-c, --catalog string dependencies catalog. Can be path to a local file or an URL.
(default "https://registry.k6.io/catalog.json")
-g, --copy-go-env copy go environment (default true)
--enable-cgo enable CGO for building binaries.
-e, --env stringToString build environment variables (default [])
-h, --help help for server
-l, --log-level string log level (default "INFO")
-p, --port int port server will listen (default 8000)
--store-url string store server url (default "http://localhost:9000/store")
-v, --verbose print build process output
--allow-build-semvers allow building versions with build metadata (e.g v0.0.0+build).
-c, --catalog string dependencies catalog. Can be path to a local file or an URL.
(default "https://registry.k6.io/catalog.json")
-g, --copy-go-env copy go environment (default true)
--enable-cgo enable CGO for building binaries.
-e, --env stringToString build environment variables (default [])
-h, --help help for server
-l, --log-level string log level (default "INFO")
-p, --port int port server will listen (default 8000)
--store-url string store server url (default "http://localhost:9000/store")
-v, --verbose print build process output
```

## SEE ALSO
Expand Down
8 changes: 6 additions & 2 deletions cmd/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ func New() *cobra.Command { //nolint:funlen
cmd.Flags().StringToStringVarP(&config.BuildEnv, "env", "e", nil, "build environment variables")
cmd.Flags().StringVarP(&output, "output", "o", "k6", "path to put the binary as an executable.")
cmd.Flags().BoolVarP(&quiet, "quiet", "q", false, "don't print artifact's details")
cmd.Flags().BoolVar(&config.AllowPrereleases, "allow-prereleases", false, "allow building pre-releases.")

cmd.Flags().BoolVar(
&config.AllowBuildSemvers,
"allow-build-semvers",
false,
"allow building versions with build metadata (e.g v0.0.0+build).",
)
return cmd
}
7 changes: 6 additions & 1 deletion cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,12 @@ func New() *cobra.Command { //nolint:funlen
cmd.Flags().IntVarP(&port, "port", "p", 8000, "port server will listen")
cmd.Flags().StringVarP(&logLevel, "log-level", "l", "INFO", "log level")
cmd.Flags().BoolVar(&enableCgo, "enable-cgo", false, "enable CGO for building binaries.")
cmd.Flags().BoolVar(&config.AllowPrereleases, "allow-prereleases", false, "allow building pre-releases.")
cmd.Flags().BoolVar(
&config.AllowBuildSemvers,
"allow-build-semvers",
false,
"allow building versions with build metadata (e.g v0.0.0+build).",
)

return cmd
}
74 changes: 37 additions & 37 deletions pkg/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ const (
k6Dep = "k6"
k6Path = "go.k6.io/k6"

opRe = `(?<operator>[=|~|>|<|\^|>=|<=|!=]){0,1}(?:\s*)`
verRe = `(?P<version>[v|V](?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*))`
preRe = `(?:[+|-|])(?P<prerelease>(?:[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))`
opRe = `(?<operator>[=|~|>|<|\^|>=|<=|!=]){0,1}(?:\s*)`
verRe = `(?P<version>[v|V](?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*))`
buildRe = `(?:[+|-|])(?P<build>(?:[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))`
)

var (
ErrAccessingArtifact = errors.New("accessing artifact") //nolint:revive
ErrBuildingArtifact = errors.New("building artifact") //nolint:revive
ErrInitializingBuilder = errors.New("initializing builder") //nolint:revive
ErrInvalidParameters = errors.New("invalid build parameters") //nolint:revive
ErrPrereleaseNotAllowed = errors.New("pre-releases are not allowed") //nolint:revive
ErrAccessingArtifact = errors.New("accessing artifact") //nolint:revive
ErrBuildingArtifact = errors.New("building artifact") //nolint:revive
ErrInitializingBuilder = errors.New("initializing builder") //nolint:revive
ErrInvalidParameters = errors.New("invalid build parameters") //nolint:revive
ErrBuildSemverNotAllowed = errors.New("semvers with build metadata not allowed") //nolint:revive

constrainRe = regexp.MustCompile(opRe + verRe + preRe)
constrainRe = regexp.MustCompile(opRe + verRe + buildRe)
)

// BuildServiceConfig defines the configuration for a Local build service
Expand All @@ -54,17 +54,17 @@ type BuildServiceConfig struct {
CopyGoEnv bool
// set verbose build mode
Verbose bool
// Allow prerelease versions
AllowPrereleases bool
// Allow semvers with build metadata
AllowBuildSemvers bool
}

// buildSrv implements the BuildService interface
type localBuildSrv struct {
allowPrereleases bool
catalog k6catalog.Catalog
builder k6foundry.Builder
store store.ObjectStore
mutexes sync.Map
allowBuildSemvers bool
catalog k6catalog.Catalog
builder k6foundry.Builder
store store.ObjectStore
mutexes sync.Map
}

// NewBuildService creates a local build service using the given configuration
Expand Down Expand Up @@ -109,10 +109,10 @@ func NewBuildService(ctx context.Context, config BuildServiceConfig) (k6build.Bu
}

return &localBuildSrv{
allowPrereleases: config.AllowPrereleases,
catalog: catalog,
builder: builder,
store: store,
allowBuildSemvers: config.AllowBuildSemvers,
catalog: catalog,
builder: builder,
store: store,
}, nil
}

Expand Down Expand Up @@ -155,21 +155,21 @@ func (b *localBuildSrv) Build( //nolint:funlen
sort.Slice(deps, func(i, j int) bool { return deps[i].Name < deps[j].Name })
resolved := map[string]string{}

// check if it is a pre-release version of the form v0.0.0-hash
// if it is a prerelease we don't check with the catalog, but instead we use
// the prerelease as version when building this module
// check if it is a semver of the form v0.0.0+<build>
// if it is, we don't check with the catalog, but instead we use
// the build metadata as version when building this module
// the build process will return the actual version built in the build info
// and we can check that version with the catalog
var k6Mod k6catalog.Module
prerelease, err := isPrerelease(k6Constrains)
buildMetadata, err := hasBuildMetadata(k6Constrains)
if err != nil {
return k6build.Artifact{}, err
}
if prerelease != "" {
if !b.allowPrereleases {
return k6build.Artifact{}, k6build.NewWrappedError(ErrInvalidParameters, ErrPrereleaseNotAllowed)
if buildMetadata != "" {
if !b.allowBuildSemvers {
return k6build.Artifact{}, k6build.NewWrappedError(ErrInvalidParameters, ErrBuildSemverNotAllowed)
}
k6Mod = k6catalog.Module{Path: k6Path, Version: prerelease}
k6Mod = k6catalog.Module{Path: k6Path, Version: buildMetadata}
} else {
k6Mod, err = b.catalog.Resolve(ctx, k6catalog.Dependency{Name: k6Dep, Constrains: k6Constrains})
if err != nil {
Expand Down Expand Up @@ -221,9 +221,9 @@ func (b *localBuildSrv) Build( //nolint:funlen
return k6build.Artifact{}, k6build.NewWrappedError(ErrAccessingArtifact, err)
}

// if this is a prerelease, we must use the actual version built
// if the version has a build metadata, we must use the actual version built
// TODO: check this version is supported
if prerelease != "" {
if buildMetadata != "" {
resolved[k6Dep] = buildInfo.ModVersions[k6Mod.Path]
}

Expand Down Expand Up @@ -257,12 +257,12 @@ func (b *localBuildSrv) lockArtifact(id string) func() {
}
}

// isPrerelease checks if the constrain references a pre-release version.
// hasBuildMetadata checks if the constrain references a version with a build metadata.
// E.g. v0.1.0+build-effa45f
func isPrerelease(constrain string) (string, error) {
func hasBuildMetadata(constrain string) (string, error) {
opInx := constrainRe.SubexpIndex("operator")
verIdx := constrainRe.SubexpIndex("version")
preIdx := constrainRe.SubexpIndex("prerelease")
preIdx := constrainRe.SubexpIndex("build")
matches := constrainRe.FindStringSubmatch(constrain)

if matches == nil {
Expand All @@ -271,20 +271,20 @@ func isPrerelease(constrain string) (string, error) {

op := matches[opInx]
ver := matches[verIdx]
prerelease := matches[preIdx]
build := matches[preIdx]

if op != "" && op != "=" {
return "", k6build.NewWrappedError(
ErrInvalidParameters,
fmt.Errorf("only exact match is allowed for pre-release versions"),
fmt.Errorf("only exact match is allowed for versions with build metadata"),
)
}

if ver != "v0.0.0" {
return "", k6build.NewWrappedError(
ErrInvalidParameters,
fmt.Errorf("prerelease version must start with v0.0.0"),
fmt.Errorf("version with build metadata must start with v0.0.0"),
)
}
return prerelease, nil
return build, nil
}

0 comments on commit 70d5a3b

Please sign in to comment.