Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Tests

on:
push:
branches: [master, main, enhancements]
pull_request:
branches: [master, main]

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: ['1.22', '1.23']

steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}

- name: Download dependencies
run: go mod download

- name: Build
run: go build -v ./...

- name: Run tests
run: go test -v -race -coverprofile=coverage.out ./...

- name: Upload coverage
uses: actions/upload-artifact@v4
with:
name: coverage-${{ matrix.go-version }}
path: coverage.out
6 changes: 5 additions & 1 deletion example/hello/hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ func main() {
fmt.Printf("Hello to hello go version: %s\n", version)

u := updater.NewUpdater(version, "ao-data", "go-githubupdate", "update-")
if err := u.BackgroundUpdater(); err != nil {
updated, err := u.BackgroundUpdater()
if err != nil {
fmt.Println(err)
}
if updated {
fmt.Println("Application was updated, please restart.")
}
}
16 changes: 10 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
module github.com/ao-data/go-githubupdate

go 1.16
go 1.22

require (
github.com/blang/semver v3.5.1+incompatible
github.com/google/go-github v17.0.0+incompatible
github.com/blang/semver/v4 v4.0.0
github.com/google/go-github/v68 v68.0.0
github.com/minio/selfupdate v0.6.0
)

require (
aead.dev/minisign v0.2.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 // indirect
github.com/kr/binarydist v0.1.0 // indirect
gopkg.in/inconshreveable/go-update.v0 v0.0.0-20150814200126-d8b0b1d421aa
golang.org/x/crypto v0.0.0-20211209193657-4570a0811e8b // indirect
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
)
39 changes: 28 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ=
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
aead.dev/minisign v0.2.0 h1:kAWrq/hBRu4AARY6AlciO83xhNnW9UaC8YipS2uhLPk=
aead.dev/minisign v0.2.0/go.mod h1:zdq6LdSd9TbuSxchxwhpA9zEb9YXcVGoE8JakuiGaIQ=
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY=
github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-github/v68 v68.0.0 h1:ZW57zeNZiXTdQ16qrDiZ0k6XucrxZ2CGmoTvcCyQG6s=
github.com/google/go-github/v68 v68.0.0/go.mod h1:K9HAUBovM2sLwM408A18h+wd9vqdLOEqTUCbnRIcx68=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 h1:iQTw/8FWTuc7uiaSepXwyf3o52HaUYcV+Tu66S3F5GA=
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0/go.mod h1:1NbS8ALrpOvjt0rHPNLyCIeMtbizbir8U//inJ+zuB8=
github.com/kr/binarydist v0.1.0 h1:6kAoLA9FMMnNGSehX0s1PdjbEaACznAv/W219j2uvyo=
github.com/kr/binarydist v0.1.0/go.mod h1:DY7S//GCoz1BCd0B0EVrinCKAZN3pXe+MDaIZbXQVgM=
github.com/minio/selfupdate v0.6.0 h1:i76PgT0K5xO9+hjzKcacQtO7+MjJ4JKA8Ak8XQ9DDwU=
github.com/minio/selfupdate v0.6.0/go.mod h1:bO02GTIPCMQFTEvE5h4DjYB58bCoZ35XLeBf0buTDdM=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/crypto v0.0.0-20211209193657-4570a0811e8b h1:QAqMVf3pSa6eeTsuklijukjXBlj7Es2QQplab+/RbQ4=
golang.org/x/crypto v0.0.0-20211209193657-4570a0811e8b/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210228012217-479acdf4ea46/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/inconshreveable/go-update.v0 v0.0.0-20150814200126-d8b0b1d421aa h1:drvf2JoUL1fz3ttkGNkw+rf3kZa2//7XkYGpSO4NHNA=
gopkg.in/inconshreveable/go-update.v0 v0.0.0-20150814200126-d8b0b1d421aa/go.mod h1:tuNm0ntQ7IH9VSA39XxzLMpee5c2DwgIbjD4x3ydo8Y=
38 changes: 19 additions & 19 deletions updater/githubupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ import (
"io"
"runtime"

update "gopkg.in/inconshreveable/go-update.v0"

"github.com/blang/semver"
"github.com/google/go-github/github"
"github.com/blang/semver/v4"
"github.com/google/go-github/v68/github"
"github.com/minio/selfupdate"
)

const (
Expand All @@ -27,7 +26,6 @@ const (
var (
ErrorNoBinary = errors.New("No binary for the update found")
defaultHTTPRequester = HTTPRequester{}
up = update.New()
)

// Updater is the configuration and runtime data for doing an update.
Expand All @@ -52,21 +50,23 @@ func NewUpdater(currentVersion, githubOwner, githubRepo, filePrefix string) *Upd
}

// BackgroundUpdater is the all in one update solution for ya. :)
func (u *Updater) BackgroundUpdater() error {
// Returns true if an update was successfully applied, false otherwise.
func (u *Updater) BackgroundUpdater() (bool, error) {
available, err := u.CheckUpdateAvailable()
if err != nil {
return err
return false, err
}

if available != "" {
fmt.Printf("Version %s available, installing now.\n", available)
err := u.Update()
if err != nil {
return err
return false, err
}
return true, nil
}

return nil
return false, nil
}

// CheckUpdateAvailable fetches the latest releases from github and
Expand Down Expand Up @@ -96,34 +96,34 @@ func (u *Updater) Update() error {
if runtime.GOOS == "windows" {
reqFilename = u.FilePrefix + platform + ".exe.gz"
}
var foundAsset github.ReleaseAsset
var foundAsset *github.ReleaseAsset
for _, asset := range u.latestReleasesResp.Assets {
if *asset.Name == reqFilename {
if asset.GetName() == reqFilename {
foundAsset = asset
break
}
}

// Not found
if foundAsset.Name == nil {
if foundAsset == nil {
return ErrorNoBinary
}

dlURL := *foundAsset.BrowserDownloadURL
dlURL := foundAsset.GetBrowserDownloadURL()

bin, err := u.fetchGZ(dlURL)
if err != nil {
return err
}

err, errRecover := up.FromStream(bytes.NewReader(bin))
if errRecover != nil {
return fmt.Errorf("Update and recovery errors: %q %q", err, errRecover)
}
err = selfupdate.Apply(bytes.NewReader(bin), selfupdate.Options{})
if err != nil {
return err
if rerr := selfupdate.RollbackError(err); rerr != nil {
return fmt.Errorf("update failed and rollback also failed: %v", rerr)
}
return fmt.Errorf("update failed: %v", err)
}
fmt.Println("Update installed, please restart the program.")
fmt.Println("Update installed successfully.")
Comment thread
phendryx marked this conversation as resolved.
return nil
}

Expand Down
Loading