diff --git a/pkg/asset/format.go b/pkg/asset/format.go index 239ec29ca..bdc2c49e7 100644 --- a/pkg/asset/format.go +++ b/pkg/asset/format.go @@ -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 { diff --git a/pkg/config/package.go b/pkg/config/package.go index ff86a3de6..bb58a4ee7 100644 --- a/pkg/config/package.go +++ b/pkg/config/package.go @@ -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" @@ -46,6 +47,7 @@ func (p *Package) RenderAsset(rt *runtime.Runtime) (string, error) { if asset == "" { return "", nil } + if !isWindows(rt.GOOS) { return asset, nil } @@ -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 } @@ -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 @@ -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) } diff --git a/pkg/template/render.go b/pkg/template/render.go index 45aa0309b..dbc7d2880 100644 --- a/pkg/template/render.go +++ b/pkg/template/render.go @@ -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, } }