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: enable to use short file extensions in format #2313

Merged
merged 1 commit into from
Oct 7, 2023
Merged
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
2 changes: 1 addition & 1 deletion pkg/asset/exclude.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func Exclude(pkgName, assetName, version string) bool {
".jar": {},
".py": {},
}
if format := GetFormat(assetName); format == formatRaw {
if format := getFormat(assetName); format == formatRaw {
ext := osfile.Ext(assetName, version)
if len(ext) > 0 && len(ext) < 6 {
if _, ok := allowedExts[ext]; !ok {
Expand Down
76 changes: 4 additions & 72 deletions pkg/asset/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,10 @@ package asset

import (
"strings"

"github.com/mholt/archiver/v3"
)

const formatRaw string = "raw"

// mholt/archiver/v3 not support but aqua support
func aquaSupportFormat(assetName string) string {
if strings.HasSuffix(assetName, ".dmg") {
return "dmg"
}
if strings.HasSuffix(assetName, ".pkg") {
return "pkg"
}
return formatRaw
}

func RemoveExtFromAsset(assetName string) (string, string) {
formats := []string{
"tar.br",
Expand All @@ -28,6 +15,7 @@ func RemoveExtFromAsset(assetName string) (string, string) {
"tar.sz",
"tar.xz",
"tbr",
"tbz",
"tbz2",
"tgz",
"tlz4",
Expand Down Expand Up @@ -58,63 +46,7 @@ func RemoveExtFromAsset(assetName string) (string, string) {
return assetName, "raw"
}

func GetFormat(assetName string) string { //nolint:funlen,cyclop
a, err := archiver.ByExtension(assetName)
if err != nil {
return aquaSupportFormat(assetName)
}
switch a.(type) {
case *archiver.Rar:
return "rar"
case *archiver.Tar:
return "tar"
case *archiver.TarBrotli:
if strings.HasSuffix(assetName, ".tbr") {
return "tbr"
}
return "tar.br"
case *archiver.TarBz2:
if strings.HasSuffix(assetName, ".tbz2") {
return "tbz2"
}
return "tar.bz2"
case *archiver.TarGz:
if strings.HasSuffix(assetName, ".tgz") {
return "tgz"
}
return "tar.gz"
case *archiver.TarLz4:
if strings.HasSuffix(assetName, ".tlz4") {
return "tlz4"
}
return "tar.lz4"
case *archiver.TarSz:
if strings.HasSuffix(assetName, ".tsz") {
return "tsz"
}
return "tar.sz"
case *archiver.TarXz:
if strings.HasSuffix(assetName, ".txz") {
return "txz"
}
return "tar.xz"
case *archiver.TarZstd:
return "tar.zst"
case *archiver.Zip:
return "zip"
case *archiver.Gz:
return "gz"
case *archiver.Bz2:
return "bz2"
case *archiver.Lz4:
return "lz4"
case *archiver.Snappy:
return "sz"
case *archiver.Xz:
return "xz"
case *archiver.Zstd:
return "zst"
default:
return aquaSupportFormat(assetName)
}
func getFormat(assetName string) string {
_, format := RemoveExtFromAsset(assetName)
return format
}
30 changes: 20 additions & 10 deletions pkg/asset/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,61 @@ import (
"github.com/aquaproj/aqua/v2/pkg/asset"
)

func TestGetFormat(t *testing.T) {
func TestRemoveExtFromAsset(t *testing.T) {
t.Parallel()
data := []struct {
name string
assetName string
exp string
format string
}{
{
name: "tar.gz",
assetName: "tfcmt_linux_amd64.tar.gz",
exp: "tar.gz",
exp: "tfcmt_linux_amd64",
format: "tar.gz",
},
{
name: "tgz",
assetName: "tfcmt_linux_amd64.tgz",
exp: "tgz",
exp: "tfcmt_linux_amd64",
format: "tgz",
},
{
name: "exe",
assetName: "tfcmt_windows_amd64.exe",
exp: "raw",
exp: "tfcmt_windows_amd64.exe",
format: "raw",
},
{
name: "js",
assetName: "tfcmt.js",
exp: "raw",
exp: "tfcmt.js",
format: "raw",
},
{
name: "dmg",
assetName: "aws-vault-darwin-amd64.dmg",
exp: "dmg",
exp: "aws-vault-darwin-amd64",
format: "dmg",
},
{
name: "pkg",
assetName: "aws-vault-darwin-amd64.pkg",
exp: "pkg",
exp: "aws-vault-darwin-amd64",
format: "pkg",
},
}
for _, d := range data {
d := d
t.Run(d.name, func(t *testing.T) {
t.Parallel()
format := asset.GetFormat(d.assetName)
if format != d.exp {
t.Fatalf("wanted %v, got %v", d.exp, format)
assetWithoutExt, format := asset.RemoveExtFromAsset(d.assetName)
if assetWithoutExt != d.exp {
t.Fatalf("wanted %v, got %v", d.exp, assetWithoutExt)
}
if format != d.format {
t.Fatalf("wanted %v, got %v", d.format, format)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/asset/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func ParseAssetName(assetName, version string) *AssetInfo { //nolint:cyclop
assetInfo.DarwinAll = true
}
}
assetInfo.Format = GetFormat(assetName)
assetInfo.Format = getFormat(assetName)
if assetInfo.Format != formatRaw {
assetInfo.Template = assetInfo.Template[:len(assetInfo.Template)-len(assetInfo.Format)] + "{{.Format}}"
}
Expand Down
20 changes: 19 additions & 1 deletion pkg/unarchive/unarchive.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (u *UnarchiverImpl) getUnarchiver(src *File, dest string) (coreUnarchiver,
if src.Type != "" {
f = "." + src.Type
}
arc, err := archiver.ByExtension(f)
arc, err := byExtension(f)
if err != nil {
return nil, fmt.Errorf("get the unarchiver or decompressor by the file extension: %w", err)
}
Expand All @@ -123,3 +123,21 @@ func (u *UnarchiverImpl) getUnarchiver(src *File, dest string) (coreUnarchiver,
}
return nil, errUnsupportedFileFormat
}

func byExtension(filename string) (interface{}, error) {
formats := map[string]interface{}{
"tbr": archiver.NewTarBrotli(),
"tbz": archiver.NewTarBz2(),
"tbz2": archiver.NewTarBz2(),
"tgz": archiver.NewTarGz(),
"tlz4": archiver.NewTarLz4(),
"tsz": archiver.NewTarSz(),
"txz": archiver.NewTarXz(),
}
for format, arc := range formats {
if strings.HasSuffix(filename, "."+format) {
return arc, nil
}
}
return archiver.ByExtension(filename) //nolint:wrapcheck
}