Skip to content

Commit

Permalink
improve matching of executable from archive (with zip files)
Browse files Browse the repository at this point in the history
  • Loading branch information
creativeprojects committed Apr 6, 2024
1 parent c39124e commit 1140692
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
31 changes: 14 additions & 17 deletions decompress.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"io"
"path/filepath"
"regexp"
"strings"

"github.com/ulikunitz/xz"
Expand All @@ -29,6 +30,8 @@ var (
{".xz", unxz},
{".bz2", unbz2},
}
// pattern copied from bottom of the page: https://semver.org/
semverPattern = `(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?`
)

// DecompressCommand decompresses the given source. Archive and compression format is
Expand Down Expand Up @@ -138,23 +141,17 @@ func unbz2(src io.Reader, cmd, os, arch string) (io.Reader, error) {
}

func matchExecutableName(cmd, os, arch, target string) bool {
if cmd == target || cmd+".exe" == target {
return true
}

// When the contained executable name is full name (e.g. foo_darwin_amd64),
// it is also regarded as a target executable file.
for _, delimiter := range []rune{'_', '-'} {
c := fmt.Sprintf("%s%c%s%c%s", cmd, delimiter, os, delimiter, arch)
if os == "windows" {
c += ".exe"
}
if c == target {
return true
}
}

return false
cmd = strings.TrimSuffix(cmd, ".exe")
pattern := regexp.MustCompile(
fmt.Sprintf(
`^%s([_-]%s)?([_-]%s[_-]%s)?(\.exe)?$`,
regexp.QuoteMeta(cmd),
semverPattern,
regexp.QuoteMeta(os),
regexp.QuoteMeta(arch),
),
)
return pattern.MatchString(target)
}

func unarchiveTar(src io.Reader, cmd, os, arch string) (io.Reader, error) {
Expand Down
27 changes: 27 additions & 0 deletions decompress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,44 @@ func TestMatchExecutableName(t *testing.T) {
target string
found bool
}{
// valid
{"gostuff", "linux", "amd64", "gostuff", true},
{"gostuff", "linux", "amd64", "gostuff_0.16.0", true},
{"gostuff", "linux", "amd64", "gostuff-0.16.0", true},
{"gostuff", "linux", "amd64", "gostuff_linux_amd64", true},
{"gostuff", "linux", "amd64", "gostuff-linux-amd64", true},
{"gostuff", "linux", "amd64", "gostuff_0.16.0_linux_amd64", true},
{"gostuff", "linux", "amd64", "gostuff-0.16.0-linux-amd64", true},
// invalid
{"gostuff", "linux", "amd64", "gostuff_darwin_amd64", false},
{"gostuff", "linux", "amd64", "gostuff0.16.0", false},
{"gostuff", "linux", "amd64", "gostuff_0.16.0_amd64", false},
{"gostuff", "linux", "amd64", "gostuff_0.16.0_linux", false},
// windows valid
{"gostuff", "windows", "amd64", "gostuff.exe", true},
{"gostuff", "windows", "amd64", "gostuff_0.16.0.exe", true},
{"gostuff", "windows", "amd64", "gostuff-0.16.0.exe", true},
{"gostuff", "windows", "amd64", "gostuff_windows_amd64.exe", true},
{"gostuff", "windows", "amd64", "gostuff-windows-amd64.exe", true},
{"gostuff", "windows", "amd64", "gostuff_0.16.0_windows_amd64.exe", true},
{"gostuff", "windows", "amd64", "gostuff-0.16.0-windows-amd64.exe", true},
// windows invalid
{"gostuff", "windows", "amd64", "gostuff_darwin_amd64.exe", false},
{"gostuff", "windows", "amd64", "gostuff0.16.0.exe", false},
{"gostuff", "windows", "amd64", "gostuff_0.16.0_amd64.exe", false},
{"gostuff", "windows", "amd64", "gostuff_0.16.0_windows.exe", false},
}

for _, testItem := range testData {
t.Run(testItem.target, func(t *testing.T) {
assert.Equal(t, testItem.found, matchExecutableName(testItem.cmd, testItem.os, testItem.arch, testItem.target))
})
// also try with .exe already in cmd for windows
if testItem.os == "windows" {
t.Run(testItem.target, func(t *testing.T) {
assert.Equal(t, testItem.found, matchExecutableName(testItem.cmd+".exe", testItem.os, testItem.arch, testItem.target))
})
}
}
}

Expand Down

0 comments on commit 1140692

Please sign in to comment.