From 62034723f26ea40177237b25d0043ce5816a024b Mon Sep 17 00:00:00 2001 From: Pablo Chacin Date: Thu, 28 Nov 2024 13:47:48 +0100 Subject: [PATCH] rename allow-prereleases option Signed-off-by: Pablo Chacin --- README.md | 24 +++++++------- cmd/local/local.go | 8 +++-- cmd/server/server.go | 7 ++++- pkg/local/local.go | 74 ++++++++++++++++++++++---------------------- 4 files changed, 61 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index 5915829..f49dde9 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/cmd/local/local.go b/cmd/local/local.go index d2d124f..7f8dc23 100644 --- a/cmd/local/local.go +++ b/cmd/local/local.go @@ -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 } diff --git a/cmd/server/server.go b/cmd/server/server.go index 9a72bf5..68cdf49 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -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 } diff --git a/pkg/local/local.go b/pkg/local/local.go index 81326d3..941987e 100644 --- a/pkg/local/local.go +++ b/pkg/local/local.go @@ -24,19 +24,19 @@ const ( k6Dep = "k6" k6Path = "go.k6.io/k6" - opRe = `(?[=|~|>|<|\^|>=|<=|!=]){0,1}(?:\s*)` - verRe = `(?P[v|V](?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*))` - preRe = `(?:[+|-|])(?P(?:[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))` + opRe = `(?[=|~|>|<|\^|>=|<=|!=]){0,1}(?:\s*)` + verRe = `(?P[v|V](?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*))` + buildRe = `(?:[+|-|])(?P(?:[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 @@ -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 @@ -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 } @@ -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+ + // 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 { @@ -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] } @@ -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 { @@ -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 }