This commit is contained in:
Medzik 2021-07-24 19:39:58 +00:00
parent 04a545f8ea
commit f29763a683
5 changed files with 14 additions and 19 deletions

View File

@ -118,7 +118,7 @@ func main() {
os.Exit(1)
}
} else {
if err := selfupdate.UpdateTo(latest.AssetURL, cmdPath); err != nil {
if err := selfupdate.UpdateTo(latest.AssetURL, cmdPath, "" /* TODO */); err != nil {
fmt.Fprintf(os.Stderr, "Error while replacing the binary with %s: %s\n", latest.AssetURL, err)
os.Exit(1)
}

2
go.mod
View File

@ -1,4 +1,4 @@
module github.com/rhysd/go-github-selfupdate
module github.com/MedzikUser/go-github-selfupdate
require (
github.com/blang/semver v3.5.1+incompatible

5
go.sum
View File

@ -2,7 +2,6 @@ github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdn
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
@ -27,13 +26,11 @@ github.com/tcnksm/go-gitconfig v0.1.2 h1:iiDhRitByXAEyjgBqsKi9QU4o2TNtv9kPP3RgPg
github.com/tcnksm/go-gitconfig v0.1.2/go.mod h1:/8EhP4H7oJZdIPyT+/UIsG87kTzrzM4UsLGSItWYCpE=
github.com/ulikunitz/xz v0.5.9 h1:RsKRIA2MO8x56wkkcd3LbtcE/uMszhb6DpRf+3uwa3I=
github.com/ulikunitz/xz v0.5.9/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad h1:DN0cp81fZ3njFcrLCytUHRSUkqBjfTo4Tx9RJTWs0EY=
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
@ -42,12 +39,10 @@ golang.org/x/oauth2 v0.0.0-20181106182150-f42d05182288 h1:JIqe8uIcRBHXDQVvZtHwp8
golang.org/x/oauth2 v0.0.0-20181106182150-f42d05182288/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=

View File

@ -15,9 +15,8 @@ import (
"github.com/inconshreveable/go-update"
)
func uncompressAndUpdate(src io.Reader, assetURL, cmdPath string) error {
_, cmd := filepath.Split(cmdPath)
asset, err := UncompressCommand(src, assetURL, cmd)
func uncompressAndUpdate(src io.Reader, assetURL, cmdPath, cmdName string) error {
asset, err := UncompressCommand(src, assetURL, cmdName)
if err != nil {
return err
}
@ -55,7 +54,7 @@ func (up *Updater) downloadDirectlyFromURL(assetURL string) (io.ReadCloser, erro
// UpdateTo downloads an executable from GitHub Releases API and replace current binary with the downloaded one.
// It downloads a release asset via GitHub Releases API so this function is available for update releases on private repository.
// If a redirect occurs, it fallbacks into directly downloading from the redirect URL.
func (up *Updater) UpdateTo(rel *Release, cmdPath string) error {
func (up *Updater) UpdateTo(rel *Release, cmdPath, cmdName string) error {
var client http.Client
src, redirectURL, err := up.api.Repositories.DownloadReleaseAsset(up.apiCtx, rel.RepoOwner, rel.RepoName, rel.AssetID, &client)
if err != nil {
@ -76,7 +75,7 @@ func (up *Updater) UpdateTo(rel *Release, cmdPath string) error {
}
if up.validator == nil {
return uncompressAndUpdate(bytes.NewReader(data), rel.AssetURL, cmdPath)
return uncompressAndUpdate(bytes.NewReader(data), rel.AssetURL, cmdPath, cmdName)
}
validationSrc, validationRedirectURL, err := up.api.Repositories.DownloadReleaseAsset(up.apiCtx, rel.RepoOwner, rel.RepoName, rel.ValidationAssetID, &client)
@ -102,7 +101,7 @@ func (up *Updater) UpdateTo(rel *Release, cmdPath string) error {
return fmt.Errorf("Failed validating asset content: %v", err)
}
return uncompressAndUpdate(bytes.NewReader(data), rel.AssetURL, cmdPath)
return uncompressAndUpdate(bytes.NewReader(data), rel.AssetURL, cmdPath, cmdName)
}
// UpdateCommand updates a given command binary to the latest version.
@ -138,7 +137,8 @@ func (up *Updater) UpdateCommand(cmdPath string, current semver.Version, slug st
return rel, nil
}
log.Println("Will update", cmdPath, "to the latest version", rel.Version)
if err := up.UpdateTo(rel, cmdPath); err != nil {
_, cmdName := filepath.Split(cmdPath)
if err := up.UpdateTo(rel, cmdPath, cmdName); err != nil {
return nil, err
}
return rel, nil
@ -158,14 +158,14 @@ func (up *Updater) UpdateSelf(current semver.Version, slug string) (*Release, er
// This function is low-level API to update the binary. Because it does not use GitHub API and downloads asset directly from the URL via HTTP,
// this function is not available to update a release for private repositories.
// cmdPath is a file path to command executable.
func UpdateTo(assetURL, cmdPath string) error {
func UpdateTo(assetURL, cmdPath, cmdName string) error {
up := DefaultUpdater()
src, err := up.downloadDirectlyFromURL(assetURL)
if err != nil {
return err
}
defer src.Close()
return uncompressAndUpdate(src, assetURL, cmdPath)
return uncompressAndUpdate(src, assetURL, cmdPath, cmdName)
}
// UpdateCommand updates a given command binary to the latest version.

View File

@ -238,7 +238,7 @@ func TestInvalidSlugForUpdate(t *testing.T) {
}
func TestInvalidAssetURL(t *testing.T) {
err := UpdateTo("https://github.com/rhysd/non-existing-repo/releases/download/v1.2.3/foo.zip", "foo")
err := UpdateTo("https://github.com/rhysd/non-existing-repo/releases/download/v1.2.3/foo.zip", "foo", "bar")
if err == nil {
t.Fatal("Error should occur for URL not found")
}
@ -249,7 +249,7 @@ func TestInvalidAssetURL(t *testing.T) {
func TestBrokenAsset(t *testing.T) {
asset := "https://github.com/rhysd-test/test-incorrect-release/releases/download/invalid/broken-zip.zip"
err := UpdateTo(asset, "foo")
err := UpdateTo(asset, "foo", "bar")
if err == nil {
t.Fatal("Error should occur for URL not found")
}
@ -263,7 +263,7 @@ func TestBrokenGitHubEnterpriseURL(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = up.UpdateTo(&Release{AssetURL: "https://example.com"}, "foo")
err = up.UpdateTo(&Release{AssetURL: "https://example.com"}, "foo", "")
if err == nil {
t.Fatal("Invalid GitHub Enterprise base URL should raise an error")
}