Skip to content

Commit

Permalink
feat: add the template variable AssetWithoutExt (#2310)
Browse files Browse the repository at this point in the history
* feat: support omitting {{.Format}} in Asset and provide the variable AssetWithoutExt

* fix: remove appendExt

* refactor: rename a function

* fix: remove GetFormatAliases

* fix: fix AssetWithoutExt

* fix: fix lint errors
  • Loading branch information
suzuki-shunsuke authored Oct 7, 2023
1 parent cc5cab9 commit 2d1b74e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 24 deletions.
39 changes: 39 additions & 0 deletions pkg/asset/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,45 @@ func aquaSupportFormat(assetName string) string {
return formatRaw
}

func RemoveExtFromAsset(assetName string) (string, string) {
formats := []string{
"tar.br",
"tar.bz2",
"tar.gz",
"tar.lz4",
"tar.sz",
"tar.xz",
"tbr",
"tbz2",
"tgz",
"tlz4",
"tsz",
"txz",

"tar.zst",

"zip",
"gz",
"bz2",
"lz4",
"sz",
"xz",
"zst",

"dmg",
"pkg",

"rar",
"tar",
}
for _, format := range formats {
if s := strings.TrimSuffix(assetName, "."+format); s != assetName {
return s, format
}
}
return assetName, "raw"
}

func GetFormat(assetName string) string { //nolint:funlen,cyclop
a, err := archiver.ByExtension(assetName)
if err != nil {
Expand Down
27 changes: 17 additions & 10 deletions pkg/config/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
texttemplate "text/template"

"github.com/aquaproj/aqua/v2/pkg/asset"
"github.com/aquaproj/aqua/v2/pkg/config/aqua"
"github.com/aquaproj/aqua/v2/pkg/config/registry"
"github.com/aquaproj/aqua/v2/pkg/runtime"
Expand Down Expand Up @@ -46,6 +47,7 @@ func (p *Package) RenderAsset(rt *runtime.Runtime) (string, error) {
if asset == "" {
return "", nil
}

if !isWindows(rt.GOOS) {
return asset, nil
}
Expand Down Expand Up @@ -124,6 +126,7 @@ func (p *Package) RenderURL(rt *runtime.Runtime) (string, error) {
if err != nil {
return "", err
}

if !isWindows(rt.GOOS) {
return s, nil
}
Expand All @@ -142,18 +145,22 @@ func (e *FileNotFoundError) Unwrap() error {
return e.Err
}

func (p *Package) renderSrc(file *registry.File, rt *runtime.Runtime) (string, error) {
func (p *Package) renderSrc(assetName string, file *registry.File, rt *runtime.Runtime) (string, error) {
pkg := p.Package
pkgInfo := p.PackageInfo
format := pkgInfo.GetFormat()
assetWithoutExt, _ := asset.RemoveExtFromAsset(assetName)
s, err := template.Execute(file.Src, map[string]interface{}{
"Version": pkg.Version,
"SemVer": p.semVer(),
"GOOS": rt.GOOS,
"GOARCH": rt.GOARCH,
"OS": replace(rt.GOOS, pkgInfo.Replacements),
"Arch": getArch(pkgInfo.Rosetta2, pkgInfo.Replacements, rt),
"Format": pkgInfo.GetFormat(),
"FileName": file.Name,
"Version": pkg.Version,
"SemVer": p.semVer(),
"GOOS": rt.GOOS,
"GOARCH": rt.GOARCH,
"OS": replace(rt.GOOS, pkgInfo.Replacements),
"Arch": getArch(pkgInfo.Rosetta2, pkgInfo.Replacements, rt),
"Format": format,
"FileName": file.Name,
"Asset": assetName,
"AssetWithoutExt": assetWithoutExt,
})
if err != nil {
return "", err //nolint:wrapcheck
Expand Down Expand Up @@ -203,7 +210,7 @@ func (p *Package) fileSrcWithoutWindowsExt(file *registry.File, rt *runtime.Runt
if file.Src == "" {
return file.Name, nil
}
src, err := p.renderSrc(file, rt)
src, err := p.renderSrc(assetName, file, rt)
if err != nil {
return "", fmt.Errorf("render the template file.src: %w", err)
}
Expand Down
30 changes: 16 additions & 14 deletions pkg/template/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,26 @@ import (
)

type Artifact struct {
Version string
SemVer string
OS string
Arch string
Format string
Asset string
Version string
SemVer string
OS string
Arch string
Format string
Asset string
AssetWithoutExt string
}

func renderParam(art *Artifact, rt *runtime.Runtime) map[string]interface{} {
return map[string]interface{}{
"Version": art.Version,
"SemVer": art.SemVer,
"GOOS": rt.GOOS,
"GOARCH": rt.GOARCH,
"OS": art.OS,
"Arch": art.Arch,
"Format": art.Format,
"Asset": art.Asset,
"Version": art.Version,
"SemVer": art.SemVer,
"GOOS": rt.GOOS,
"GOARCH": rt.GOARCH,
"OS": art.OS,
"Arch": art.Arch,
"Format": art.Format,
"Asset": art.Asset,
"AssetWithoutExt": art.AssetWithoutExt,
}
}

Expand Down

0 comments on commit 2d1b74e

Please sign in to comment.