Skip to content

Commit

Permalink
feat: add vars (#3052)
Browse files Browse the repository at this point in the history
* feat: add vars

* docs: update JSON Schema

* fix: pass vars to template and support validation and default values

* docs: update JSON Schema

* fix: suppress lint errors

* refactor: split functions
  • Loading branch information
suzuki-shunsuke authored Aug 27, 2024
1 parent 5ae1844 commit e7abead
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 51 deletions.
3 changes: 3 additions & 0 deletions json-schema/aqua-yaml.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@
},
"go_version_file": {
"type": "string"
},
"vars": {
"type": "object"
}
},
"additionalProperties": false,
Expand Down
34 changes: 34 additions & 0 deletions json-schema/registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,12 @@
"minisign": {
"$ref": "#/$defs/Minisign"
},
"vars": {
"items": {
"$ref": "#/$defs/Var"
},
"type": "array"
},
"envs": {
"$ref": "#/$defs/SupportedEnvs"
}
Expand Down Expand Up @@ -522,6 +528,12 @@
"minisign": {
"$ref": "#/$defs/Minisign"
},
"vars": {
"items": {
"$ref": "#/$defs/Var"
},
"type": "array"
},
"version_constraint": {
"type": "string"
},
Expand Down Expand Up @@ -615,6 +627,22 @@
},
"type": "array"
},
"Var": {
"properties": {
"name": {
"type": "string"
},
"required": {
"type": "boolean"
},
"default": true
},
"additionalProperties": false,
"type": "object",
"required": [
"name"
]
},
"VersionOverride": {
"properties": {
"version_constraint": {
Expand Down Expand Up @@ -719,6 +747,12 @@
"build": {
"$ref": "#/$defs/Build"
},
"vars": {
"items": {
"$ref": "#/$defs/Var"
},
"type": "array"
},
"overrides": {
"$ref": "#/$defs/Overrides"
},
Expand Down
21 changes: 11 additions & 10 deletions pkg/config/aqua/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ import (
)

type Package struct {
Name string `validate:"required" json:"name,omitempty"`
Registry string `validate:"required" yaml:",omitempty" json:"registry,omitempty" jsonschema:"description=Registry name,example=foo,example=local,default=standard"`
Version string `validate:"required" yaml:",omitempty" json:"version,omitempty"`
Import string `yaml:",omitempty" json:"import,omitempty"`
Tags []string `yaml:",omitempty" json:"tags,omitempty"`
Description string `yaml:",omitempty" json:"description,omitempty"`
Link string `yaml:",omitempty" json:"link,omitempty"`
Update *Update `yaml:",omitempty" json:"update,omitempty"`
FilePath string `json:"-" yaml:"-"`
GoVersionFile string `json:"go_version_file,omitempty" yaml:"go_version_file,omitempty"`
Name string `validate:"required" json:"name,omitempty"`
Registry string `validate:"required" yaml:",omitempty" json:"registry,omitempty" jsonschema:"description=Registry name,example=foo,example=local,default=standard"`
Version string `validate:"required" yaml:",omitempty" json:"version,omitempty"`
Import string `yaml:",omitempty" json:"import,omitempty"`
Tags []string `yaml:",omitempty" json:"tags,omitempty"`
Description string `yaml:",omitempty" json:"description,omitempty"`
Link string `yaml:",omitempty" json:"link,omitempty"`
Update *Update `yaml:",omitempty" json:"update,omitempty"`
FilePath string `json:"-" yaml:"-"`
GoVersionFile string `json:"go_version_file,omitempty" yaml:"go_version_file,omitempty"`
Vars map[string]any `json:"vars,omitempty" yaml:",omitempty"`
}

type Update struct {
Expand Down
67 changes: 39 additions & 28 deletions pkg/config/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,44 +84,55 @@ func ListPackages(logE *logrus.Entry, cfg *aqua.Config, rt *runtime.Runtime, reg
"package_version": pkg.Version,
"registry": pkg.Registry,
})
rgst, ok := cfg.Registries[pkg.Registry]
if ok {
if rgst.Ref != "" {
logE = logE.WithField("registry_ref", rgst.Ref)
}
}
pkgInfo, err := getPkgInfoFromRegistries(logE, registries, pkg, m)
if err != nil {
logerr.WithError(logE, err).Error("install the package")
failed = true
continue
}

pkgInfo, err = pkgInfo.Override(logE, pkg.Version, rt)
p, err := listPackage(logE, cfg, rt, registries, pkg, m, env)
if err != nil {
logerr.WithError(logE, err).Error("evaluate version constraints")
logerr.WithError(logE, err).Error("ignore a package because the package version is empty")
failed = true
continue
}
supported, err := pkgInfo.CheckSupported(rt, env)
if err != nil {
logerr.WithError(logE, err).Error("check if the package is supported")
failed = true
if p == nil {
continue
}
if !supported {
logE.Debug("the package isn't supported on this environment")
continue
}
pkgs = append(pkgs, &Package{
Package: pkg,
PackageInfo: pkgInfo,
Registry: rgst,
})
pkgs = append(pkgs, p)
}
return pkgs, failed
}

func listPackage(logE *logrus.Entry, cfg *aqua.Config, rt *runtime.Runtime, registries map[string]*registry.Config, pkg *aqua.Package, m map[string]map[string]*registry.PackageInfo, env string) (*Package, error) {
rgst, ok := cfg.Registries[pkg.Registry]
if ok {
if rgst.Ref != "" {
logE = logE.WithField("registry_ref", rgst.Ref)
}
}
pkgInfo, err := getPkgInfoFromRegistries(logE, registries, pkg, m)
if err != nil {
return nil, errors.New("install the package")
}

pkgInfo, err = pkgInfo.Override(logE, pkg.Version, rt)
if err != nil {
return nil, errors.New("evaluate version constraints")
}
supported, err := pkgInfo.CheckSupported(rt, env)
if err != nil {
return nil, errors.New("check if the package is supported")
}
if !supported {
logE.Debug("the package isn't supported on this environment")
return nil, nil //nolint:nilnil
}
p := &Package{
Package: pkg,
PackageInfo: pkgInfo,
Registry: rgst,
}
if err := p.ApplyVars(); err != nil {
return nil, errors.New("apply the package variable")
}
return p, nil
}

func getPkgInfoFromRegistries(logE *logrus.Entry, registries map[string]*registry.Config, pkg *aqua.Package, m map[string]map[string]*registry.PackageInfo) (*registry.PackageInfo, error) {
pkgInfoMap, ok := m[pkg.Registry]
if !ok {
Expand Down
44 changes: 44 additions & 0 deletions pkg/config/package.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"errors"
"fmt"
"net/url"
"path"
Expand All @@ -14,6 +15,8 @@ import (
"github.com/aquaproj/aqua/v2/pkg/runtime"
"github.com/aquaproj/aqua/v2/pkg/template"
"github.com/aquaproj/aqua/v2/pkg/unarchive"
"github.com/sirupsen/logrus"
"github.com/suzuki-shunsuke/logrus-error/logerr"
)

type Package struct {
Expand Down Expand Up @@ -68,6 +71,7 @@ func (p *Package) TemplateArtifact(rt *runtime.Runtime, asset string) *template.
Arch: getArch(pkgInfo.Rosetta2, pkgInfo.WindowsARMEmulation, pkgInfo.Replacements, rt),
Format: pkgInfo.GetFormat(),
Asset: asset,
Vars: pkg.Vars,
}
}

Expand Down Expand Up @@ -344,6 +348,7 @@ func (p *Package) renderChecksumFile(asset string, rt *runtime.Runtime) (string,
"Arch": getArch(pkgInfo.Rosetta2, pkgInfo.WindowsARMEmulation, replacements, rt),
"Format": pkgInfo.GetFormat(),
"Asset": asset,
"Vars": pkg.Vars,
})
if err != nil {
return "", fmt.Errorf("render a template: %w", err)
Expand All @@ -362,6 +367,7 @@ func (p *Package) renderTemplate(tpl *texttemplate.Template, rt *runtime.Runtime
"OS": replace(rt.GOOS, pkgInfo.Replacements),
"Arch": getArch(pkgInfo.Rosetta2, pkgInfo.WindowsARMEmulation, pkgInfo.Replacements, rt),
"Format": pkgInfo.GetFormat(),
"Vars": pkg.Vars,
})
if err != nil {
return "", fmt.Errorf("render a template: %w", err)
Expand Down Expand Up @@ -390,5 +396,43 @@ func (p *Package) RenderDir(file *registry.File, rt *runtime.Runtime) (string, e
"Arch": getArch(pkgInfo.Rosetta2, pkgInfo.WindowsARMEmulation, pkgInfo.Replacements, rt),
"Format": pkgInfo.GetFormat(),
"FileName": file.Name,
"Vars": pkg.Vars,
})
}

func (p *Package) ApplyVars() error {
if p.PackageInfo.Vars == nil {
return nil
}
for _, v := range p.PackageInfo.Vars {
if v.Name == "" {
return errors.New("a variable name is empty")
}
if err := p.applyVar(v); err != nil {
return fmt.Errorf("apply a variable: %w", logerr.WithFields(err, logrus.Fields{
"var_name": v.Name,
}))
}
}
return nil
}

func (p *Package) applyVar(v *registry.Var) error {
if _, ok := p.Package.Vars[v.Name]; ok {
return nil
}
if v.Default != nil {
if p.Package.Vars == nil {
p.Package.Vars = map[string]any{
v.Name: v.Default,
}
return nil
}
p.Package.Vars[v.Name] = v.Default
return nil
}
if !v.Required {
return nil
}
return errors.New("a variable is required")
}
19 changes: 18 additions & 1 deletion pkg/config/registry/package_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,17 @@ type PackageInfo struct {
Cosign *Cosign `json:"cosign,omitempty"`
SLSAProvenance *SLSAProvenance `json:"slsa_provenance,omitempty" yaml:"slsa_provenance,omitempty"`
Minisign *Minisign `json:"minisign,omitempty" yaml:",omitempty"`
Vars []*Var `json:"vars,omitempty" yaml:",omitempty"`
VersionConstraints string `yaml:"version_constraint,omitempty" json:"version_constraint,omitempty"`
VersionOverrides []*VersionOverride `yaml:"version_overrides,omitempty" json:"version_overrides,omitempty"`
}

type Var struct {
Name string `json:"name"`
Required bool `json:"required,omitempty"`
Default any `json:"default,omitempty"`
}

type Build struct {
Enabled *bool `json:"enabled,omitempty" yaml:",omitempty"`
Type string `json:"type,omitempty" yaml:",omitempty" jsonschema:"enum=go_install,enum=go_build"`
Expand Down Expand Up @@ -115,6 +122,7 @@ type VersionOverride struct {
SLSAProvenance *SLSAProvenance `json:"slsa_provenance,omitempty" yaml:"slsa_provenance,omitempty"`
Minisign *Minisign `json:"minisign,omitempty" yaml:",omitempty"`
Build *Build `json:"build,omitempty" yaml:",omitempty"`
Vars []*Var `json:"vars,omitempty" yaml:",omitempty"`
Overrides Overrides `yaml:",omitempty" json:"overrides,omitempty"`
SupportedEnvs SupportedEnvs `yaml:"supported_envs,omitempty" json:"supported_envs,omitempty"`
}
Expand All @@ -138,6 +146,7 @@ type Override struct {
Cosign *Cosign `json:"cosign,omitempty"`
SLSAProvenance *SLSAProvenance `json:"slsa_provenance,omitempty" yaml:"slsa_provenance,omitempty"`
Minisign *Minisign `json:"minisign,omitempty" yaml:",omitempty"`
Vars []*Var `json:"vars,omitempty" yaml:",omitempty"`
Envs SupportedEnvs `yaml:",omitempty" json:"envs,omitempty"`
}

Expand Down Expand Up @@ -179,6 +188,7 @@ func (p *PackageInfo) Copy() *PackageInfo {
NoAsset: p.NoAsset,
AppendExt: p.AppendExt,
Build: p.Build,
Vars: p.Vars,
}
return pkg
}
Expand Down Expand Up @@ -249,7 +259,7 @@ func (p *PackageInfo) resetByPkgType(typ string) { //nolint:funlen
}
}

func (p *PackageInfo) overrideVersion(child *VersionOverride) *PackageInfo { //nolint:cyclop,funlen
func (p *PackageInfo) overrideVersion(child *VersionOverride) *PackageInfo { //nolint:cyclop,funlen,gocyclo
pkg := p.Copy()
if child.Type != "" {
pkg.resetByPkgType(child.Type)
Expand Down Expand Up @@ -339,6 +349,9 @@ func (p *PackageInfo) overrideVersion(child *VersionOverride) *PackageInfo { //n
if child.Build != nil {
pkg.Build = child.Build
}
if child.Vars != nil {
pkg.Vars = child.Vars
}
return pkg
}

Expand Down Expand Up @@ -428,6 +441,10 @@ func (p *PackageInfo) OverrideByRuntime(rt *runtime.Runtime) { //nolint:cyclop,f
if ov.AppendExt != nil {
p.AppendExt = ov.AppendExt
}

if ov.Vars != nil {
p.Vars = ov.Vars
}
}

func (p *PackageInfo) OverrideByBuild() {
Expand Down
Loading

0 comments on commit e7abead

Please sign in to comment.