more accurately check for binary extension

This commit is contained in:
vilmibm 2021-09-20 16:02:20 -05:00
parent eeca998640
commit b00e8a5681
2 changed files with 67 additions and 11 deletions

View file

@ -247,12 +247,8 @@ func checkValidExtension(rootCmd *cobra.Command, m extensions.ExtensionManager,
}
func isBinExtension(client *http.Client, repo ghrepo.Interface) (isBin bool, err error) {
hs, err := hasScript(client, repo)
if err != nil || hs {
return
}
_, err = fetchLatestRelease(client, repo)
var r *release
r, err = fetchLatestRelease(client, repo)
if err != nil {
httpErr, ok := err.(api.HTTPError)
if ok && httpErr.StatusCode == 404 {
@ -262,7 +258,16 @@ func isBinExtension(client *http.Client, repo ghrepo.Interface) (isBin bool, err
return
}
isBin = true
for _, a := range r.Assets {
dists := possibleDists()
for _, d := range dists {
if strings.HasSuffix(a.Name, d) {
isBin = true
break
}
}
}
return
}
@ -272,3 +277,52 @@ func normalizeExtensionSelector(n string) string {
}
return strings.TrimPrefix(n, "gh-")
}
func possibleDists() []string {
return []string{
"aix-ppc64",
"android-386",
"android-amd64",
"android-arm",
"android-arm64",
"darwin-amd64",
"darwin-arm64",
"dragonfly-amd64",
"freebsd-386",
"freebsd-amd64",
"freebsd-arm",
"freebsd-arm64",
"illumos-amd64",
"ios-amd64",
"ios-arm64",
"js-wasm",
"linux-386",
"linux-amd64",
"linux-arm",
"linux-arm64",
"linux-mips",
"linux-mips64",
"linux-mips64le",
"linux-mipsle",
"linux-ppc64",
"linux-ppc64le",
"linux-riscv64",
"linux-s390x",
"netbsd-386",
"netbsd-amd64",
"netbsd-arm",
"netbsd-arm64",
"openbsd-386",
"openbsd-amd64",
"openbsd-arm",
"openbsd-arm64",
"openbsd-mips64",
"plan9-386",
"plan9-amd64",
"plan9-arm",
"solaris-amd64",
"windows-386",
"windows-amd64",
"windows-arm",
}
}

View file

@ -40,6 +40,9 @@ func TestNewCmdExtension(t *testing.T) {
name: "install a git extension",
args: []string{"install", "owner/gh-some-ext"},
httpStubs: func(reg *httpmock.Registry) {
reg.Register(
httpmock.REST("GET", "repos/owner/gh-some-ext/releases/latest"),
httpmock.StatusStringResponse(404, "nope"))
reg.Register(
httpmock.REST("GET", "repos/owner/gh-some-ext/contents/gh-some-ext"),
httpmock.StringResponse("a script"))
@ -64,12 +67,11 @@ func TestNewCmdExtension(t *testing.T) {
name: "install a binary extension",
args: []string{"install", "owner/gh-bin-ext"},
httpStubs: func(reg *httpmock.Registry) {
reg.Register(
httpmock.REST("GET", "repos/owner/gh-bin-ext/contents/gh-bin-ext"),
httpmock.StatusStringResponse(404, "no"))
reg.Register(
httpmock.REST("GET", "repos/owner/gh-bin-ext/releases/latest"),
httpmock.StringResponse("{}"))
httpmock.JSONResponse(release{
Assets: []releaseAsset{
{Name: "gh-foo-windows-amd64"}}}))
},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.ListFunc = func(bool) []extensions.Extension {