From 09feb84549a388823a9402dc83d491dc405a6c4e Mon Sep 17 00:00:00 2001 From: Shunsuke Suzuki Date: Sat, 7 Oct 2023 17:26:36 +0900 Subject: [PATCH] feat: enable to use short file extensions in `format` (#2313) --- pkg/asset/exclude.go | 2 +- pkg/asset/format.go | 76 ++------------------------------------ pkg/asset/format_test.go | 30 ++++++++++----- pkg/asset/generate.go | 2 +- pkg/unarchive/unarchive.go | 20 +++++++++- 5 files changed, 45 insertions(+), 85 deletions(-) diff --git a/pkg/asset/exclude.go b/pkg/asset/exclude.go index 6181dce44..9e3fd2087 100644 --- a/pkg/asset/exclude.go +++ b/pkg/asset/exclude.go @@ -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 { diff --git a/pkg/asset/format.go b/pkg/asset/format.go index bdc2c49e7..2ef022d96 100644 --- a/pkg/asset/format.go +++ b/pkg/asset/format.go @@ -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", @@ -28,6 +15,7 @@ func RemoveExtFromAsset(assetName string) (string, string) { "tar.sz", "tar.xz", "tbr", + "tbz", "tbz2", "tgz", "tlz4", @@ -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 } diff --git a/pkg/asset/format_test.go b/pkg/asset/format_test.go index 32b51e1af..d8b223b91 100644 --- a/pkg/asset/format_test.go +++ b/pkg/asset/format_test.go @@ -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) } }) } diff --git a/pkg/asset/generate.go b/pkg/asset/generate.go index ab2239481..355172704 100644 --- a/pkg/asset/generate.go +++ b/pkg/asset/generate.go @@ -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}}" } diff --git a/pkg/unarchive/unarchive.go b/pkg/unarchive/unarchive.go index 3bade0f1e..136b93a46 100644 --- a/pkg/unarchive/unarchive.go +++ b/pkg/unarchive/unarchive.go @@ -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) } @@ -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 +}