Merge branch 'xz'

This commit is contained in:
rhysd 2017-12-31 13:35:44 +09:00
commit a685f32589
12 changed files with 88 additions and 82 deletions

View File

@ -25,7 +25,7 @@ If newer version than itself is detected, it downloads released binary from GitH
- Retrieve the proper binary for the OS and arch where the binary is running
- Update the binary with rollback support on failure
- Tested on Linux, macOS and Windows (using Travis CI and AppVeyor)
- Many archive and compression formats are supported (zip, gzip, tar)
- Many archive and compression formats are supported (zip, tar, gzip, xzip)
And small wrapper CLIs are provided:
@ -169,7 +169,7 @@ You need to put the binaries with the following format.
`{cmd}` is a name of command.
`{goos}` and `{goarch}` are the platform and the arch type of the binary.
`{.ext}` is a file extension. go-github-selfupdate supports `.zip`, `.gzip` and `.tar.gz`.
`{.ext}` is a file extension. go-github-selfupdate supports `.zip`, `.gzip`, `.tar.gz` and `.tar.xz`.
You can also use blank and it means binary is not compressed.
If you compress binary, uncompressed directory or file must contain the executable named `{cmd}`.
@ -182,7 +182,7 @@ page on GitHub as binary for platform `linux` and arch `amd64`.
- `foo-bar_linux_amd64` (executable)
- `foo-bar_linux_amd64.zip` (zip file containing `foo-bar`)
- `foo-bar_linux_amd64.tar.gz` (tar file containing `foo-bar`)
- `foo-bar_linux_amd64.gzip` (gzip file of the executable `foo-bar`)
- `foo-bar_linux_amd64.xz` (xzip file of the executable `foo-bar`)
- `foo-bar-linux-amd64.tar.gz` (`-` is also ok for separator)
To archive the executable directly on Windows, `.exe` can be added before file extension like

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

@ -18,7 +18,7 @@ If newer version than itself is detected, it downloads released binary from GitH
- Retrieve the proper binary for the OS and arch where the binary is running
- Update the binary with rollback support on failure
- Tested on Linux, macOS and Windows
- Many archive and compression formats are supported (zip, gzip, tar)
- Many archive and compression formats are supported (zip, gzip, xzip, tar)
There are some naming rules. Please read following links.

BIN
selfupdate/testdata/bar-not-found.tar.xz vendored Normal file

Binary file not shown.

BIN
selfupdate/testdata/foo.tar.xz vendored Normal file

Binary file not shown.

BIN
selfupdate/testdata/invalid-tar.tar.xz vendored Normal file

Binary file not shown.

1
selfupdate/testdata/invalid-xz.tar.xz vendored Normal file
View File

@ -0,0 +1 @@
hello

1
selfupdate/testdata/invalid.xz vendored Normal file
View File

@ -0,0 +1 @@
hello

BIN
selfupdate/testdata/single-file.xz vendored Normal file

Binary file not shown.

View File

@ -6,13 +6,36 @@ import (
"bytes"
"compress/gzip"
"fmt"
"github.com/ulikunitz/xz"
"io"
"io/ioutil"
"path/filepath"
"strings"
)
// 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 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'. '.zip',
// '.tar.gz', '.tar.xz', '.gz' and '.xz' are supported.
func UncompressCommand(src io.Reader, url, cmd string) (io.Reader, error) {
if strings.HasSuffix(url, ".zip") {
log.Println("Uncompressing zip file", url)
@ -46,22 +69,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 +84,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)

View File

@ -27,6 +27,16 @@ func TestCompressionNotRequired(t *testing.T) {
}
}
func getArchiveFileExt(file string) string {
if strings.HasSuffix(file, ".tar.gz") {
return ".tar.gz"
}
if strings.HasSuffix(file, ".tar.xz") {
return ".tar.xz"
}
return filepath.Ext(file)
}
func TestUncompress(t *testing.T) {
for _, n := range []string{
"testdata/foo.zip",
@ -34,6 +44,8 @@ func TestUncompress(t *testing.T) {
"testdata/single-file.gz",
"testdata/single-file.gzip",
"testdata/foo.tar.gz",
"testdata/foo.tar.xz",
"testdata/single-file.xz",
} {
t.Run(n, func(t *testing.T) {
f, err := os.Open(n)
@ -41,13 +53,7 @@ func TestUncompress(t *testing.T) {
t.Fatal(err)
}
var ext string
if strings.HasSuffix(n, ".tar.gz") {
ext = ".tar.gz"
} else {
ext = filepath.Ext(n)
}
ext := getArchiveFileExt(n)
url := "https://github.com/foo/bar/releases/download/v1.2.3/bar" + ext
r, err := UncompressCommand(f, url, "bar")
if err != nil {
@ -75,19 +81,16 @@ func TestUncompressInvalidArchive(t *testing.T) {
{"testdata/invalid.gz", "Failed to uncompress gzip file"},
{"testdata/invalid-tar.tar.gz", "Failed to unarchive .tar file"},
{"testdata/invalid-gzip.tar.gz", "Failed to uncompress .tar.gz file"},
{"testdata/invalid.xz", "Failed to uncompress xzip file"},
{"testdata/invalid-tar.tar.xz", "Failed to unarchive .tar file"},
{"testdata/invalid-xz.tar.xz", "Failed to uncompress .tar.xz file"},
} {
f, err := os.Open(a.name)
if err != nil {
t.Fatal(err)
}
var ext string
if strings.HasSuffix(a.name, ".tar.gz") {
ext = ".tar.gz"
} else {
ext = filepath.Ext(a.name)
}
ext := getArchiveFileExt(a.name)
url := "https://github.com/foo/bar/releases/download/v1.2.3/bar" + ext
_, err = UncompressCommand(f, url, "bar")
if err == nil {
@ -99,59 +102,29 @@ func TestUncompressInvalidArchive(t *testing.T) {
}
}
func TestTargetNotFoundInZip(t *testing.T) {
for _, f := range []string{
"testdata/empty.zip",
"testdata/bar-not-found.zip",
func TestTargetNotFound(t *testing.T) {
for _, tc := range []struct {
name string
msg string
}{
{"testdata/empty.zip", "command is not found"},
{"testdata/bar-not-found.zip", "command is not found"},
{"testdata/bar-not-found.gzip", "does not match to command"},
{"testdata/empty.tar.gz", "command is not found"},
{"testdata/bar-not-found.tar.gz", "command is not found"},
} {
t.Run(f, func(t *testing.T) {
f, err := os.Open(f)
t.Run(tc.name, func(t *testing.T) {
f, err := os.Open(tc.name)
if err != nil {
t.Fatal(err)
}
_, err = UncompressCommand(f, "https://github.com/foo/bar/releases/download/v1.2.3/bar.zip", "bar")
ext := getArchiveFileExt(tc.name)
url := "https://github.com/foo/bar/releases/download/v1.2.3/bar" + ext
_, err = UncompressCommand(f, url, "bar")
if err == nil {
t.Fatal("Error should be raised for")
}
if !strings.Contains(err.Error(), "command is not found") {
t.Fatal("Unexpected error:", err)
}
})
}
}
func TestTargetNotFoundInGZip(t *testing.T) {
f, err := os.Open("testdata/bar-not-found.gzip")
if err != nil {
t.Fatal(err)
}
_, err = UncompressCommand(f, "https://github.com/foo/bar/releases/download/v1.2.3/bar.gzip", "bar")
if err == nil {
t.Fatal("Error should be raised for")
}
if !strings.Contains(err.Error(), "does not match to command") {
t.Fatal("Unexpected error:", err)
}
}
func TestTargetNotFoundInTarGz(t *testing.T) {
for _, f := range []string{
"testdata/empty.tar.gz",
"testdata/bar-not-found.tar.gz",
} {
t.Run(f, func(t *testing.T) {
f, err := os.Open(f)
if err != nil {
t.Fatal(err)
}
_, err = UncompressCommand(f, "https://github.com/foo/bar/releases/download/v1.2.3/bar.tar.gz", "bar")
if err == nil {
t.Fatal("Error should be raised for")
}
if !strings.Contains(err.Error(), "command is not found") {
if !strings.Contains(err.Error(), tc.msg) {
t.Fatal("Unexpected error:", err)
}
})

View File

@ -35,6 +35,8 @@ func TestUpdateCommand(t *testing.T) {
"rhysd-test/test-release-zip",
"rhysd-test/test-release-tar",
"rhysd-test/test-release-gzip",
"rhysd-test/test-release-tar-xz",
"rhysd-test/test-release-xz",
} {
t.Run(slug, func(t *testing.T) {
setupTestBinary()
@ -101,6 +103,10 @@ func TestCurrentIsTheLatest(t *testing.T) {
}
func TestBrokenBinaryUpdate(t *testing.T) {
if testing.Short() {
t.Skip("skip tests in short mode.")
}
_, err := UpdateCommand("foo", semver.MustParse("1.2.2"), "rhysd-test/test-incorrect-release")
if err == nil {
t.Fatal("Error should occur for broken package")