Skip to content

Commit 3e3692b

Browse files
committed
Merge branch 'autoupdate'
2 parents ed7dd8b + 1186d6f commit 3e3692b

File tree

7 files changed

+255
-138
lines changed

7 files changed

+255
-138
lines changed

.goxc.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"ResourcesExclude": "*.go",
77
"MainDirsExclude": "Godeps",
88
"PackageVersion": "0.26.0",
9-
"PrereleaseInfo": "snapshot",
109
"Verbosity": "v",
1110
"TaskSettings": {
1211
"downloads-page": {

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,26 @@ To compile gh from source, you need to have a [Go development environment](http:
6161
Note that `go get` will pull down sources from various VCS.
6262
Please make sure you have git and hg installed.
6363

64-
## Upgrade
64+
## Update
6565

6666
`gh` comes with a command to self update:
6767

6868
$ gh update
6969

70+
### Autoupdate
71+
72+
`gh` checks every two weeks for newer versions and prompts you for update if there's one.
73+
A timestamp is stored in `~/.config/gh-update` for the next update time.
74+
7075
### Homebrew
7176

72-
If you installed `gh` with `brew tap jingweno/gh`, you can upgrade it with:
77+
If you installed `gh` with `brew tap jingweno/gh`, you can update it with:
7378

7479
$ brew upgrade gh
7580

7681
### Source
7782

78-
To upgrade gh from source, run:
83+
To update gh from source, run:
7984

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

commands/runner.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"github.com/jingweno/gh/cmd"
77
"github.com/jingweno/gh/git"
8+
"github.com/jingweno/gh/utils"
89
"github.com/kballard/go-shellquote"
910
"os/exec"
1011
"syscall"
@@ -44,6 +45,10 @@ func (r *Runner) Execute() ExecError {
4445
return newExecError(nil)
4546
}
4647

48+
updater := NewUpdater()
49+
err := updater.PromptForUpdate()
50+
utils.Check(err)
51+
4752
expandAlias(args)
4853
slurpGlobalFlags(args)
4954

@@ -78,7 +83,7 @@ func (r *Runner) Execute() ExecError {
7883
}
7984
}
8085

81-
err := git.Spawn(args.Command, args.Params...)
86+
err = git.Spawn(args.Command, args.Params...)
8287
return newExecError(err)
8388
}
8489

commands/update.go

Lines changed: 2 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
package commands
22

33
import (
4-
"archive/zip"
5-
"fmt"
6-
updater "github.com/inconshreveable/go-update"
7-
"github.com/jingweno/gh/github"
84
"github.com/jingweno/gh/utils"
9-
"io"
10-
"io/ioutil"
11-
"net/http"
125
"os"
13-
"path/filepath"
14-
"runtime"
15-
"strings"
166
)
177

188
var cmdUpdate = &Command{
@@ -27,128 +17,8 @@ Examples:
2717
}
2818

2919
func update(cmd *Command, args *Args) {
30-
err := doUpdate()
20+
updater := NewUpdater()
21+
err := updater.Update()
3122
utils.Check(err)
3223
os.Exit(0)
3324
}
34-
35-
func doUpdate() (err error) {
36-
client := github.NewClient(github.GitHubHost)
37-
releases, err := client.Releases(github.NewProject("jingweno", "gh", github.GitHubHost))
38-
if err != nil {
39-
err = fmt.Errorf("Error fetching releases: %s", err)
40-
return
41-
}
42-
43-
latestRelease := releases[0]
44-
tagName := latestRelease.TagName
45-
version := strings.TrimPrefix(tagName, "v")
46-
if version == Version {
47-
err = fmt.Errorf("You're already on the latest version: %s", Version)
48-
return
49-
}
50-
51-
fmt.Printf("Updating gh to release %s...\n", version)
52-
downloadURL := fmt.Sprintf("https://github.com/jingweno/gh/releases/download/%s/gh_%s-snapshot_%s_%s.zip", tagName, version, runtime.GOOS, runtime.GOARCH)
53-
path, err := downloadFile(downloadURL)
54-
if err != nil {
55-
err = fmt.Errorf("Can't download update from %s to %s", downloadURL, path)
56-
return
57-
}
58-
59-
exec, err := unzipExecutable(path)
60-
if err != nil {
61-
err = fmt.Errorf("Can't unzip gh executable: %s", err)
62-
return
63-
}
64-
65-
err, _ = updater.FromFile(exec)
66-
if err == nil {
67-
fmt.Println("Done!")
68-
}
69-
70-
return
71-
}
72-
73-
func unzipExecutable(path string) (exec string, err error) {
74-
rc, err := zip.OpenReader(path)
75-
if err != nil {
76-
err = fmt.Errorf("Can't open zip file %s: %s", path, err)
77-
return
78-
}
79-
defer rc.Close()
80-
81-
for _, file := range rc.File {
82-
if !strings.HasPrefix(file.Name, "gh") {
83-
continue
84-
}
85-
86-
dir := filepath.Dir(path)
87-
exec, err = unzipFile(file, dir)
88-
break
89-
}
90-
91-
if exec == "" && err == nil {
92-
err = fmt.Errorf("No gh executable is found in %s", path)
93-
}
94-
95-
return
96-
}
97-
98-
func unzipFile(file *zip.File, to string) (exec string, err error) {
99-
frc, err := file.Open()
100-
if err != nil {
101-
err = fmt.Errorf("Can't open zip entry %s when reading: %s", file.Name, err)
102-
return
103-
}
104-
defer frc.Close()
105-
106-
dest := filepath.Join(to, filepath.Base(file.Name))
107-
f, err := os.Create(dest)
108-
if err != nil {
109-
return
110-
}
111-
defer f.Close()
112-
113-
copied, err := io.Copy(f, frc)
114-
if err != nil {
115-
return
116-
}
117-
118-
if uint32(copied) != file.UncompressedSize {
119-
err = fmt.Errorf("Zip entry %s is corrupted", file.Name)
120-
return
121-
}
122-
123-
exec = f.Name()
124-
125-
return
126-
}
127-
128-
func downloadFile(url string) (path string, err error) {
129-
dir, err := ioutil.TempDir("", "gh-update")
130-
if err != nil {
131-
return
132-
}
133-
134-
file, err := os.Create(filepath.Join(dir, filepath.Base(url)))
135-
if err != nil {
136-
return
137-
}
138-
defer file.Close()
139-
140-
resp, err := http.Get(url)
141-
if err != nil {
142-
return
143-
}
144-
defer resp.Body.Close()
145-
146-
_, err = io.Copy(file, resp.Body)
147-
if err != nil {
148-
return
149-
}
150-
151-
path = file.Name()
152-
153-
return
154-
}

0 commit comments

Comments
 (0)