Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support files_dir #1988

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions pkg/config/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,27 @@ type Package struct {
Registry *aqua.Registry
}

func (cpkg *Package) renderFilesDir(rt *runtime.Runtime) (string, error) {
pkg := cpkg.Package
pkgInfo := cpkg.PackageInfo
if pkgInfo.FilesDir == nil {
return "", nil
}
s, err := template.Execute(*pkgInfo.FilesDir, map[string]interface{}{
"Version": pkg.Version,
"SemVer": cpkg.SemVer(),
"GOOS": rt.GOOS,
"GOARCH": rt.GOARCH,
"OS": replace(rt.GOOS, pkgInfo.GetReplacements()),
"Arch": getArch(pkgInfo.GetRosetta2(), pkgInfo.GetReplacements(), rt),
"Format": pkgInfo.GetFormat(),
})
if err != nil {
return "", err //nolint:wrapcheck
}
return filepath.FromSlash(s), nil // FromSlash is needed for Windows. https://github.com/aquaproj/aqua/issues/2013
}

func (cpkg *Package) RenderSrc(file *registry.File, rt *runtime.Runtime) (string, error) {
pkg := cpkg.Package
pkgInfo := cpkg.PackageInfo
Expand Down Expand Up @@ -150,14 +171,18 @@ func (cpkg *Package) getFileSrc(file *registry.File, rt *runtime.Runtime) (strin
if unarchive.IsUnarchived(pkgInfo.GetFormat(), assetName) {
return filepath.Base(assetName), nil
}
filesDir, err := cpkg.renderFilesDir(rt)
if err != nil {
return "", fmt.Errorf("render files_dir: %w", err)
}
if file.Src == "" {
return file.Name, nil
return filepath.Join(filesDir, file.Name), nil
}
src, err := cpkg.RenderSrc(file, rt)
if err != nil {
return "", fmt.Errorf("render the template file.src: %w", err)
}
return src, nil
return filepath.Join(filesDir, src), nil
}

const (
Expand Down
11 changes: 11 additions & 0 deletions pkg/config/registry/package_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type PackageInfo struct {
Format string `json:"format,omitempty" jsonschema:"example=tar.gz,example=raw,example=zip,example=dmg" yaml:",omitempty"`
Overrides []*Override `json:"overrides,omitempty" yaml:",omitempty"`
FormatOverrides []*FormatOverride `yaml:"format_overrides,omitempty" json:"format_overrides,omitempty"`
FilesDir *string `yaml:"files_dir,omitempty" json:"files_dir,omitempty"`
Files []*File `json:"files,omitempty" yaml:",omitempty"`
Replacements Replacements `json:"replacements,omitempty" yaml:",omitempty"`
SupportedEnvs SupportedEnvs `yaml:"supported_envs,omitempty" json:"supported_envs,omitempty"`
Expand Down Expand Up @@ -84,6 +85,7 @@ func (pkgInfo *PackageInfo) Copy() *PackageInfo {
Private: pkgInfo.Private,
ErrorMessage: pkgInfo.ErrorMessage,
NoAsset: pkgInfo.NoAsset,
FilesDir: pkgInfo.FilesDir,
}
return pkg
}
Expand Down Expand Up @@ -113,6 +115,7 @@ func (pkgInfo *PackageInfo) resetByPkgType(typ string) {
pkgInfo.SLSAProvenance = nil
pkgInfo.Format = ""
pkgInfo.Rosetta2 = nil
pkgInfo.FilesDir = nil
}
}

Expand Down Expand Up @@ -140,6 +143,9 @@ func (pkgInfo *PackageInfo) overrideVersion(child *VersionOverride) *PackageInfo
if child.Files != nil {
pkg.Files = child.Files
}
if child.FilesDir != nil {
pkg.FilesDir = child.FilesDir
}
if child.URL != nil {
pkg.URL = child.URL
}
Expand Down Expand Up @@ -234,6 +240,10 @@ func (pkgInfo *PackageInfo) OverrideByRuntime(rt *runtime.Runtime) { //nolint:cy
pkgInfo.Files = ov.Files
}

if ov.FilesDir != nil {
pkgInfo.FilesDir = ov.FilesDir
}

if ov.URL != nil {
pkgInfo.URL = ov.URL
}
Expand Down Expand Up @@ -265,6 +275,7 @@ type VersionOverride struct {
Path *string `yaml:",omitempty" json:"path,omitempty"`
URL *string `yaml:",omitempty" json:"url,omitempty"`
Files []*File `yaml:",omitempty" json:"files,omitempty"`
FilesDir *string `yaml:"files_dir,omitempty" json:"files_dir,omitempty"`
Format string `yaml:",omitempty" json:"format,omitempty" jsonschema:"example=tar.gz,example=raw,example=zip"`
FormatOverrides FormatOverrides `yaml:"format_overrides,omitempty" json:"format_overrides,omitempty"`
Overrides Overrides `yaml:",omitempty" json:"overrides,omitempty"`
Expand Down
1 change: 1 addition & 0 deletions pkg/config/registry/replacements.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Override struct {
Format string `yaml:",omitempty" json:"format,omitempty" jsonschema:"example=tar.gz,example=raw,example=zip"`
Asset *string `yaml:",omitempty" json:"asset,omitempty"`
Files []*File `yaml:",omitempty" json:"files,omitempty"`
FilesDir *string `yaml:"files_dir,omitempty" json:"files_dir,omitempty"`
URL *string `yaml:",omitempty" json:"url,omitempty"`
CompleteWindowsExt *bool `json:"complete_windows_ext,omitempty" yaml:"complete_windows_ext,omitempty"`
WindowsExt string `json:"windows_ext,omitempty" yaml:"windows_ext,omitempty"`
Expand Down