Skip to content

Commit ef505d5

Browse files
authored
Merge pull request #171 from WillAbides/fixgz
fix compressed files
2 parents 8f2e71d + fece2c1 commit ef505d5

File tree

4 files changed

+74
-24
lines changed

4 files changed

+74
-24
lines changed

Diff for: internal/bindown/extract.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package bindown
33
import (
44
"os"
55
"path/filepath"
6+
"strings"
67

78
"github.com/mholt/archiver/v3"
89
"github.com/willabides/bindown/v4/internal/cache"
@@ -55,9 +56,20 @@ func extract(archivePath, extractDir string) error {
5556
return err
5657
}
5758
tarPath := filepath.Join(downloadDir, dlName)
58-
_, err = archiver.ByExtension(dlName)
59+
byExt, err := archiver.ByExtension(dlName)
5960
if err != nil {
6061
return copyFile(tarPath, filepath.Join(extractDir, dlName))
6162
}
62-
return archiver.Unarchive(tarPath, extractDir)
63+
switch x := byExt.(type) {
64+
case archiver.Unarchiver:
65+
return x.Unarchive(tarPath, extractDir)
66+
case archiver.Decompressor:
67+
dest := filepath.Join(
68+
extractDir,
69+
strings.TrimSuffix(dlName, filepath.Ext(dlName)),
70+
)
71+
return archiver.FileCompressor{Decompressor: x}.DecompressFile(tarPath, dest)
72+
default:
73+
return copyFile(tarPath, filepath.Join(extractDir, dlName))
74+
}
6375
}

Diff for: internal/builddep/builddep.go

+45-20
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ var archiveSuffixes = []string{
244244
".tzst",
245245
".rar",
246246
".zip",
247+
}
248+
249+
var compressSuffixes = []string{
247250
".br",
248251
".gz",
249252
".bz2",
@@ -278,6 +281,7 @@ func parseDownload(dlURL, version string, systems []bindown.System) (*dlFile, bo
278281
return nil, false
279282
}
280283
isArchive := false
284+
isCompress := false
281285
suffix := ""
282286
for _, s := range archiveSuffixes {
283287
if strings.HasSuffix(dlURL, s) {
@@ -286,6 +290,15 @@ func parseDownload(dlURL, version string, systems []bindown.System) (*dlFile, bo
286290
break
287291
}
288292
}
293+
if !isArchive {
294+
for _, s := range compressSuffixes {
295+
if strings.HasSuffix(dlURL, s) {
296+
suffix = s
297+
isCompress = true
298+
break
299+
}
300+
}
301+
}
289302
if strings.HasSuffix(dlURL, ".exe") {
290303
suffix = ".exe"
291304
}
@@ -301,13 +314,14 @@ func parseDownload(dlURL, version string, systems []bindown.System) (*dlFile, bo
301314
priority += archSub.priority
302315
}
303316
return &dlFile{
304-
origUrl: dlURL,
305-
url: tmpl,
306-
osSub: osSub,
307-
archSub: archSub,
308-
suffix: suffix,
309-
isArchive: isArchive,
310-
priority: priority,
317+
origUrl: dlURL,
318+
url: tmpl,
319+
osSub: osSub,
320+
archSub: archSub,
321+
suffix: suffix,
322+
isArchive: isArchive,
323+
isCompress: isCompress,
324+
priority: priority,
311325
}, true
312326
}
313327

@@ -361,24 +375,35 @@ func parseDownloads(dlUrls []string, binName, version string, allowedSystems []b
361375
if len(systemFiles[system]) == 1 {
362376
continue
363377
}
364-
// prefer archives
365-
slices.SortFunc(systemFiles[system], func(a, b *dlFile) bool {
366-
return a.isArchive && !b.isArchive
367-
})
368-
cutOff = slices.IndexFunc(systemFiles[system], func(f *dlFile) bool {
369-
return !f.isArchive
370-
})
371-
if cutOff != -1 {
372-
systemFiles[system] = systemFiles[system][:cutOff]
378+
// prefer archives then compresses
379+
var archiveSystems, compressSystems, plainSystems []*dlFile
380+
for _, file := range systemFiles[system] {
381+
switch {
382+
case file.isArchive:
383+
archiveSystems = append(archiveSystems, file)
384+
case file.isCompress:
385+
compressSystems = append(compressSystems, file)
386+
default:
387+
plainSystems = append(plainSystems, file)
388+
}
373389
}
374-
if len(systemFiles[system]) == 1 {
375-
continue
390+
var sf []*dlFile
391+
switch {
392+
case len(archiveSystems) > 0:
393+
sf = archiveSystems
394+
case len(compressSystems) > 0:
395+
sf = compressSystems
396+
default:
397+
sf = plainSystems
398+
}
399+
if len(sf) < 2 {
400+
systemFiles[system] = sf
376401
}
377402
// now arbitrarily pick the first one alphabetically by origUrl
378-
slices.SortFunc(systemFiles[system], func(a, b *dlFile) bool {
403+
slices.SortFunc(sf, func(a, b *dlFile) bool {
379404
return a.origUrl < b.origUrl
380405
})
381-
systemFiles[system] = systemFiles[system][:1]
406+
systemFiles[system] = sf[:1]
382407
}
383408

384409
templates := maps.Keys(urlFrequency)

Diff for: internal/builddep/depgroup.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (g *depGroup) regroupByArchivePath(ctx context.Context, binName, version st
5050
return []*depGroup{gr}, nil
5151
}
5252
// trust that if the first isn't an archive, none of them are
53-
if !gr.files[0].isArchive {
53+
if !gr.files[0].isArchive && !gr.files[0].isCompress {
5454
gr.archivePath = path.Base(gr.files[0].url)
5555
return []*depGroup{gr}, nil
5656
}

Diff for: internal/builddep/dlfile.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type dlFile struct {
2525
archSub *systemSub
2626
suffix string
2727
isArchive bool
28+
isCompress bool
2829
priority int
2930
archiveFiles []*archiveFile
3031
checksum string
@@ -45,14 +46,26 @@ func (f *dlFile) clone() *dlFile {
4546
}
4647

4748
func (f *dlFile) setArchiveFiles(ctx context.Context, binName, version string) error {
48-
if !f.isArchive {
49+
if !f.isArchive && !f.isCompress {
4950
return nil
5051
}
5152
parsedUrl, err := url.Parse(f.origUrl)
5253
if err != nil {
5354
return err
5455
}
5556
filename := path.Base(parsedUrl.EscapedPath())
57+
if f.isCompress {
58+
f.archiveFiles = []*archiveFile{parseArchiveFile(
59+
strings.TrimSuffix(filename, f.suffix),
60+
binName,
61+
f.osSub.val,
62+
f.archSub.val,
63+
version,
64+
true,
65+
)}
66+
return nil
67+
}
68+
5669
req, err := http.NewRequestWithContext(ctx, http.MethodGet, f.origUrl, http.NoBody)
5770
if err != nil {
5871
return err

0 commit comments

Comments
 (0)