Skip to content

Commit

Permalink
Merge branch 'autoupdate'
Browse files Browse the repository at this point in the history
  • Loading branch information
owenthereal committed Dec 19, 2013
2 parents ed7dd8b + 1186d6f commit 3e3692b
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 138 deletions.
1 change: 0 additions & 1 deletion .goxc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"ResourcesExclude": "*.go",
"MainDirsExclude": "Godeps",
"PackageVersion": "0.26.0",
"PrereleaseInfo": "snapshot",
"Verbosity": "v",
"TaskSettings": {
"downloads-page": {
Expand Down
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,26 @@ To compile gh from source, you need to have a [Go development environment](http:
Note that `go get` will pull down sources from various VCS.
Please make sure you have git and hg installed.

## Upgrade
## Update

`gh` comes with a command to self update:

$ gh update

### Autoupdate

`gh` checks every two weeks for newer versions and prompts you for update if there's one.
A timestamp is stored in `~/.config/gh-update` for the next update time.

### Homebrew

If you installed `gh` with `brew tap jingweno/gh`, you can upgrade it with:
If you installed `gh` with `brew tap jingweno/gh`, you can update it with:

$ brew upgrade gh

### Source

To upgrade gh from source, run:
To update gh from source, run:

$ go get -u github.com/jingweno/gh

Expand Down
7 changes: 6 additions & 1 deletion commands/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/jingweno/gh/cmd"
"github.com/jingweno/gh/git"
"github.com/jingweno/gh/utils"
"github.com/kballard/go-shellquote"
"os/exec"
"syscall"
Expand Down Expand Up @@ -44,6 +45,10 @@ func (r *Runner) Execute() ExecError {
return newExecError(nil)
}

updater := NewUpdater()
err := updater.PromptForUpdate()
utils.Check(err)

expandAlias(args)
slurpGlobalFlags(args)

Expand Down Expand Up @@ -78,7 +83,7 @@ func (r *Runner) Execute() ExecError {
}
}

err := git.Spawn(args.Command, args.Params...)
err = git.Spawn(args.Command, args.Params...)
return newExecError(err)
}

Expand Down
134 changes: 2 additions & 132 deletions commands/update.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
package commands

import (
"archive/zip"
"fmt"
updater "github.com/inconshreveable/go-update"
"github.com/jingweno/gh/github"
"github.com/jingweno/gh/utils"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
)

var cmdUpdate = &Command{
Expand All @@ -27,128 +17,8 @@ Examples:
}

func update(cmd *Command, args *Args) {
err := doUpdate()
updater := NewUpdater()
err := updater.Update()
utils.Check(err)
os.Exit(0)
}

func doUpdate() (err error) {
client := github.NewClient(github.GitHubHost)
releases, err := client.Releases(github.NewProject("jingweno", "gh", github.GitHubHost))
if err != nil {
err = fmt.Errorf("Error fetching releases: %s", err)
return
}

latestRelease := releases[0]
tagName := latestRelease.TagName
version := strings.TrimPrefix(tagName, "v")
if version == Version {
err = fmt.Errorf("You're already on the latest version: %s", Version)
return
}

fmt.Printf("Updating gh to release %s...\n", version)
downloadURL := fmt.Sprintf("https://github.com/jingweno/gh/releases/download/%s/gh_%s-snapshot_%s_%s.zip", tagName, version, runtime.GOOS, runtime.GOARCH)
path, err := downloadFile(downloadURL)
if err != nil {
err = fmt.Errorf("Can't download update from %s to %s", downloadURL, path)
return
}

exec, err := unzipExecutable(path)
if err != nil {
err = fmt.Errorf("Can't unzip gh executable: %s", err)
return
}

err, _ = updater.FromFile(exec)
if err == nil {
fmt.Println("Done!")
}

return
}

func unzipExecutable(path string) (exec string, err error) {
rc, err := zip.OpenReader(path)
if err != nil {
err = fmt.Errorf("Can't open zip file %s: %s", path, err)
return
}
defer rc.Close()

for _, file := range rc.File {
if !strings.HasPrefix(file.Name, "gh") {
continue
}

dir := filepath.Dir(path)
exec, err = unzipFile(file, dir)
break
}

if exec == "" && err == nil {
err = fmt.Errorf("No gh executable is found in %s", path)
}

return
}

func unzipFile(file *zip.File, to string) (exec string, err error) {
frc, err := file.Open()
if err != nil {
err = fmt.Errorf("Can't open zip entry %s when reading: %s", file.Name, err)
return
}
defer frc.Close()

dest := filepath.Join(to, filepath.Base(file.Name))
f, err := os.Create(dest)
if err != nil {
return
}
defer f.Close()

copied, err := io.Copy(f, frc)
if err != nil {
return
}

if uint32(copied) != file.UncompressedSize {
err = fmt.Errorf("Zip entry %s is corrupted", file.Name)
return
}

exec = f.Name()

return
}

func downloadFile(url string) (path string, err error) {
dir, err := ioutil.TempDir("", "gh-update")
if err != nil {
return
}

file, err := os.Create(filepath.Join(dir, filepath.Base(url)))
if err != nil {
return
}
defer file.Close()

resp, err := http.Get(url)
if err != nil {
return
}
defer resp.Body.Close()

_, err = io.Copy(file, resp.Body)
if err != nil {
return
}

path = file.Name()

return
}
Loading

0 comments on commit 3e3692b

Please sign in to comment.