implement xz support

This commit is contained in:
rhysd 2017-12-31 12:10:34 +09:00
parent 684283e69c
commit f8d9336cde
2 changed files with 39 additions and 17 deletions

View File

@ -25,7 +25,7 @@ func findSuitableReleaseAndAsset(rels []*github.RepositoryRelease) (*github.Repo
// Generate candidates
cs := make([]string, 0, 8)
for _, sep := range []rune{'_', '-'} {
for _, ext := range []string{".zip", ".tar.gz", ".gzip", ".gz", ""} {
for _, ext := range []string{".zip", ".tar.gz", ".gzip", ".gz", ".tar.xz", ".xz", ""} {
suffix := fmt.Sprintf("%s%c%s%s", runtime.GOOS, sep, runtime.GOARCH, ext)
cs = append(cs, suffix)
if runtime.GOOS == "windows" {

View File

@ -6,12 +6,32 @@ import (
"bytes"
"compress/gzip"
"fmt"
"github.com/ulikunitz/xz"
"io"
"io/ioutil"
"path/filepath"
"strings"
)
func unarchiveTar(src io.Reader, url, cmd string) (io.Reader, error) {
t := tar.NewReader(src)
for {
h, err := t.Next()
if err == io.EOF {
break
}
if err != nil {
return nil, fmt.Errorf("Failed to unarchive .tar file: %s", err)
}
_, name := filepath.Split(h.Name)
if name == cmd {
return t, nil
}
}
return nil, fmt.Errorf("File '%s' for the command is not found in %s", cmd, url)
}
// UncompressCommand uncompresses the given source. Archive and compression format is automatically detected from 'url' parameter, which represents the URL of asset. This returns a reader for the uncompressed command given by 'cmd'.
func UncompressCommand(src io.Reader, url, cmd string) (io.Reader, error) {
if strings.HasSuffix(url, ".zip") {
@ -46,22 +66,7 @@ func UncompressCommand(src io.Reader, url, cmd string) (io.Reader, error) {
return nil, fmt.Errorf("Failed to uncompress .tar.gz file: %s", err)
}
t := tar.NewReader(gz)
for {
h, err := t.Next()
if err == io.EOF {
break
}
if err != nil {
return nil, fmt.Errorf("Failed to unarchive .tar file: %s", err)
}
_, name := filepath.Split(h.Name)
if name == cmd {
return t, nil
}
}
return nil, fmt.Errorf("File '%s' for the command is not found in %s", cmd, url)
return unarchiveTar(gz, url, cmd)
} else if strings.HasSuffix(url, ".gzip") || strings.HasSuffix(url, ".gz") {
log.Println("Uncompressing gzip file", url)
@ -76,6 +81,23 @@ func UncompressCommand(src io.Reader, url, cmd string) (io.Reader, error) {
}
return r, nil
} else if strings.HasSuffix(url, ".tar.xz") {
log.Println("Uncompressing tar.xz file", url)
xzip, err := xz.NewReader(src)
if err != nil {
return nil, fmt.Errorf("Failed to uncompress .tar.xz file: %s", err)
}
return unarchiveTar(xzip, url, cmd)
} else if strings.HasSuffix(url, ".xz") {
log.Println("Uncompressing xzip file", url)
xzip, err := xz.NewReader(src)
if err != nil {
return nil, fmt.Errorf("Failed to uncompress xzip file downloaded from %s: %s", url, err)
}
return xzip, nil
}
log.Println("Uncompression is not needed", url)