diff --git a/pkg/cli/generate_registry.go b/pkg/cli/generate_registry.go index bdd8c65b4..4969407b6 100644 --- a/pkg/cli/generate_registry.go +++ b/pkg/cli/generate_registry.go @@ -83,6 +83,10 @@ func (runner *Runner) newGenerateRegistryCommand() *cli.Command { Name: "deep", Usage: "Resolve version_overrides", }, + &cli.StringFlag{ + Name: "version-filter", + Usage: "version filter", + }, }, } } diff --git a/pkg/cli/runner.go b/pkg/cli/runner.go index ce3cda46f..48d896f31 100644 --- a/pkg/cli/runner.go +++ b/pkg/cli/runner.go @@ -52,6 +52,7 @@ func (runner *Runner) setParam(c *cli.Context, commandName string, param *config param.All = c.Bool("all") param.Prune = c.Bool("prune") param.SelectVersion = c.Bool("select-version") + param.VersionFilter = c.String("version-filter") param.File = c.String("f") param.LogColor = os.Getenv("AQUA_LOG_COLOR") param.AQUAVersion = runner.LDFlags.Version diff --git a/pkg/config/package.go b/pkg/config/package.go index d52e8e883..4cbb5d4b6 100644 --- a/pkg/config/package.go +++ b/pkg/config/package.go @@ -160,6 +160,7 @@ type Param struct { Dest string HomeDir string OutTestData string + VersionFilter string MaxParallelism int Args []string Tags map[string]struct{} diff --git a/pkg/controller/generate-registry/generate.go b/pkg/controller/generate-registry/generate.go index 46cef1c28..1c5d4df32 100644 --- a/pkg/controller/generate-registry/generate.go +++ b/pkg/controller/generate-registry/generate.go @@ -12,6 +12,7 @@ import ( "github.com/aquaproj/aqua/v2/pkg/config" "github.com/aquaproj/aqua/v2/pkg/config/registry" "github.com/aquaproj/aqua/v2/pkg/controller/generate/output" + "github.com/aquaproj/aqua/v2/pkg/expr" "github.com/aquaproj/aqua/v2/pkg/github" yaml "github.com/goccy/go-yaml" "github.com/sirupsen/logrus" @@ -51,7 +52,15 @@ func (ctrl *Controller) GenerateRegistry(ctx context.Context, param *config.Para } func (ctrl *Controller) genRegistry(ctx context.Context, param *config.Param, logE *logrus.Entry, pkgName string) error { - pkgInfo, versions := ctrl.getPackageInfo(ctx, logE, pkgName, param.Deep) + var versionFilter *expr.Program + if param.VersionFilter != "" { + a, err := expr.CompileVersionFilter(param.VersionFilter) + if err != nil { + return fmt.Errorf("compile the version-filter: %w", err) + } + versionFilter = a + } + pkgInfo, versions := ctrl.getPackageInfo(ctx, logE, pkgName, param.Deep, versionFilter) if param.OutTestData != "" { if err := ctrl.testdataOutputter.Output(&output.Param{ List: listPkgsFromVersions(pkgName, versions), @@ -87,7 +96,7 @@ func (ctrl *Controller) getRelease(ctx context.Context, repoOwner, repoName, ver return release, err //nolint:wrapcheck } -func (ctrl *Controller) getPackageInfo(ctx context.Context, logE *logrus.Entry, arg string, deep bool) (*registry.PackageInfo, []string) { +func (ctrl *Controller) getPackageInfo(ctx context.Context, logE *logrus.Entry, arg string, deep bool, versionFilter *expr.Program) (*registry.PackageInfo, []string) { pkgName, version, _ := strings.Cut(arg, "@") splitPkgNames := strings.Split(pkgName, "/") pkgInfo := ®istry.PackageInfo{ @@ -112,7 +121,7 @@ func (ctrl *Controller) getPackageInfo(ctx context.Context, logE *logrus.Entry, pkgInfo.Description = strings.TrimRight(strings.TrimSpace(repo.GetDescription()), ".!?") } if deep && version == "" { - return ctrl.getPackageInfoWithVersionOverrides(ctx, logE, pkgName, pkgInfo) + return ctrl.getPackageInfoWithVersionOverrides(ctx, logE, pkgName, pkgInfo, versionFilter) } release, err := ctrl.getRelease(ctx, pkgInfo.RepoOwner, pkgInfo.RepoName, version) if err != nil { diff --git a/pkg/controller/generate-registry/generate_internal_test.go b/pkg/controller/generate-registry/generate_internal_test.go index 8fceeb4ab..0566f4cec 100644 --- a/pkg/controller/generate-registry/generate_internal_test.go +++ b/pkg/controller/generate-registry/generate_internal_test.go @@ -113,7 +113,7 @@ func TestController_getPackageInfo(t *testing.T) { //nolint:funlen Repo: d.repo, } ctrl := NewController(nil, gh, nil) - pkgInfo, _ := ctrl.getPackageInfo(ctx, logE, d.pkgName, true) + pkgInfo, _ := ctrl.getPackageInfo(ctx, logE, d.pkgName, true, nil) if diff := cmp.Diff(d.exp, pkgInfo); diff != "" { t.Fatal(diff) } diff --git a/pkg/controller/generate-registry/version_overrides.go b/pkg/controller/generate-registry/version_overrides.go index 75a4405ab..513c29aea 100644 --- a/pkg/controller/generate-registry/version_overrides.go +++ b/pkg/controller/generate-registry/version_overrides.go @@ -9,6 +9,7 @@ import ( "github.com/aquaproj/aqua/v2/pkg/config/aqua" "github.com/aquaproj/aqua/v2/pkg/config/registry" + "github.com/aquaproj/aqua/v2/pkg/expr" "github.com/aquaproj/aqua/v2/pkg/github" "github.com/aquaproj/aqua/v2/pkg/util" "github.com/hashicorp/go-version" @@ -59,20 +60,28 @@ func listPkgsFromVersions(pkgName string, versions []string) []*aqua.Package { return pkgs } -func (ctrl *Controller) getPackageInfoWithVersionOverrides(ctx context.Context, logE *logrus.Entry, pkgName string, pkgInfo *registry.PackageInfo) (*registry.PackageInfo, []string) { +func (ctrl *Controller) getPackageInfoWithVersionOverrides(ctx context.Context, logE *logrus.Entry, pkgName string, pkgInfo *registry.PackageInfo, versionFilter *expr.Program) (*registry.PackageInfo, []string) { //nolint:cyclop ghReleases := ctrl.listReleases(ctx, logE, pkgInfo) - releases := make([]*Release, len(ghReleases)) - for i, release := range ghReleases { + releases := make([]*Release, 0, len(ghReleases)) + for _, release := range ghReleases { tag := release.GetTagName() + if versionFilter != nil { + if f, err := expr.EvaluateVersionFilter(versionFilter, tag); err != nil { + logE.WithError(err).WithField("release_version", tag).Debug("evaluate version filter") + continue + } else if !f { + continue + } + } v, err := version.NewVersion(tag) if err != nil { logE.WithField("tag_name", tag).WithError(err).Warn("parse a tag as semver") } - releases[i] = &Release{ + releases = append(releases, &Release{ ID: release.GetID(), Tag: tag, Version: v, - } + }) } sort.Slice(releases, func(i, j int) bool { r1 := releases[i] diff --git a/pkg/controller/generate/github_release.go b/pkg/controller/generate/github_release.go index 907007d78..3501f1d2d 100644 --- a/pkg/controller/generate/github_release.go +++ b/pkg/controller/generate/github_release.go @@ -4,7 +4,6 @@ import ( "context" "strings" - "github.com/antonmedv/expr/vm" "github.com/aquaproj/aqua/v2/pkg/config/registry" "github.com/aquaproj/aqua/v2/pkg/expr" "github.com/aquaproj/aqua/v2/pkg/github" @@ -46,7 +45,7 @@ func (ctrl *Controller) getVersionFromLatestRelease(ctx context.Context, logE *l type Filter struct { Prefix string - Filter *vm.Program + Filter *expr.Program Constraint string } diff --git a/pkg/expr/version_filter.go b/pkg/expr/version_filter.go index 1c444a95e..2aa5d3251 100644 --- a/pkg/expr/version_filter.go +++ b/pkg/expr/version_filter.go @@ -7,7 +7,9 @@ import ( "github.com/antonmedv/expr/vm" ) -func CompileVersionFilter(versionFilter string) (*vm.Program, error) { +type Program = vm.Program + +func CompileVersionFilter(versionFilter string) (*Program, error) { return expr.Compile(versionFilter, expr.AsBool(), expr.Env(map[string]interface{}{ //nolint:wrapcheck "Version": "", "semver": emptySemver, @@ -16,7 +18,7 @@ func CompileVersionFilter(versionFilter string) (*vm.Program, error) { })) } -func CompileVersionFilterForTest(versionFilter string) *vm.Program { +func CompileVersionFilterForTest(versionFilter string) *Program { p, err := expr.Compile(versionFilter, expr.AsBool(), expr.Env(map[string]interface{}{ "Version": "", "semver": emptySemver, @@ -29,7 +31,7 @@ func CompileVersionFilterForTest(versionFilter string) *vm.Program { return p } -func EvaluateVersionFilter(prog *vm.Program, v string) (bool, error) { +func EvaluateVersionFilter(prog *Program, v string) (bool, error) { return evaluateBoolProg(prog, map[string]interface{}{ "Version": v, "semver": getCompareFunc(v),