Skip to content

Commit

Permalink
feat(generate): support specifying version
Browse files Browse the repository at this point in the history
  • Loading branch information
suzuki-shunsuke committed Nov 3, 2022
1 parent 0d5a472 commit 0c87e2c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
31 changes: 24 additions & 7 deletions pkg/controller/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func (ctrl *Controller) Generate(ctx context.Context, logE *logrus.Entry, param
type FindingPackage struct {
PackageInfo *registry.PackageInfo
RegistryName string
Version string
}

func (ctrl *Controller) listPkgs(ctx context.Context, logE *logrus.Entry, param *config.Param, cfg *aqua.Config, cfgFilePath string, args ...string) ([]*aqua.Package, error) {
Expand Down Expand Up @@ -189,7 +190,9 @@ func (ctrl *Controller) listPkgsWithoutFinder(ctx context.Context, logE *logrus.
outputPkgs := []*aqua.Package{}
for _, pkgName := range pkgNames {
pkgName = getGeneratePkg(pkgName)
findingPkg, ok := m[pkgName]
key, version, _ := strings.Cut(pkgName, "@")
findingPkg, ok := m[key]
findingPkg.Version = version
if !ok {
return nil, logerr.WithFields(errUnknownPkg, logrus.Fields{"package_name": pkgName}) //nolint:wrapcheck
}
Expand Down Expand Up @@ -220,27 +223,41 @@ func (ctrl *Controller) getVersionFromGitHub(ctx context.Context, logE *logrus.E
return ctrl.getVersionFromLatestRelease(ctx, logE, pkgInfo)
}

func (ctrl *Controller) getVersion(ctx context.Context, logE *logrus.Entry, param *config.Param, pkg *FindingPackage) string {
if pkg.Version != "" {
return pkg.Version
}
if ctrl.github == nil {
return ""
}
pkgInfo := pkg.PackageInfo
if pkgInfo.HasRepo() {
return ctrl.getVersionFromGitHub(ctx, logE, param, pkgInfo)
}
return ""
}

func (ctrl *Controller) getOutputtedPkg(ctx context.Context, logE *logrus.Entry, param *config.Param, pkg *FindingPackage) *aqua.Package {
outputPkg := &aqua.Package{
Name: pkg.PackageInfo.GetName(),
Registry: pkg.RegistryName,
Version: "[SET PACKAGE VERSION]",
Version: pkg.Version,
}
if outputPkg.Registry == registryStandard {
outputPkg.Registry = ""
}
if ctrl.github == nil {
version := ctrl.getVersion(ctx, logE, param, pkg)
if version == "" {
outputPkg.Version = "[SET PACKAGE VERSION]"
return outputPkg
}
if pkgInfo := pkg.PackageInfo; pkgInfo.HasRepo() {
version := ctrl.getVersionFromGitHub(ctx, logE, param, pkgInfo)
pkgInfo := pkg.PackageInfo
if pkgInfo.HasRepo() {
repoOwner := pkgInfo.RepoOwner
repoName := pkgInfo.RepoName
if pkgName := pkgInfo.GetName(); pkgName == repoOwner+"/"+repoName || strings.HasPrefix(pkgName, repoOwner+"/"+repoName+"/") {
outputPkg.Name += "@" + version
outputPkg.Version = ""
} else {
outputPkg.Version = version
}
return outputPkg
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/controller/generate/read_pkg_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"io"
"strings"

"github.com/aquaproj/aqua/pkg/config"
"github.com/aquaproj/aqua/pkg/config/aqua"
Expand All @@ -27,10 +28,12 @@ func (ctrl *Controller) readGeneratedPkgsFromFile(ctx context.Context, logE *log
scanner := bufio.NewScanner(file)
for scanner.Scan() {
txt := getGeneratePkg(scanner.Text())
findingPkg, ok := m[txt]
key, version, _ := strings.Cut(txt, "@")
findingPkg, ok := m[key]
if !ok {
return nil, logerr.WithFields(errUnknownPkg, logrus.Fields{"package_name": txt}) //nolint:wrapcheck
}
findingPkg.Version = version
outputPkg := ctrl.getOutputtedPkg(ctx, logE, param, findingPkg)
outputPkgs = append(outputPkgs, outputPkg)
}
Expand Down

0 comments on commit 0c87e2c

Please sign in to comment.