This commit is contained in:
Jingwen Owen Ou 2013-12-18 12:22:59 -08:00
Родитель 6d79e4792f
Коммит 3f4670d707
1 изменённых файлов: 120 добавлений и 0 удалений

Просмотреть файл

@ -1,7 +1,17 @@
package commands
import (
"archive/zip"
"fmt"
"github.com/jingweno/gh/github"
"github.com/jingweno/gh/utils"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
)
var cmdUpdate = &Command{
@ -16,5 +26,115 @@ Examples:
}
func update(cmd *Command, args *Args) {
err := doUpdate()
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")
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 %s to %s", downloadURL, path)
return
}
exec, err := unzipExecutable(path)
if err != nil {
err = fmt.Errorf("Can't unzip gh executable: %s", err)
return
}
fmt.Println(exec)
//https://github.com/jingweno/gh/releases/download/v0.26.0/gh_0.26.0-snapshot_darwin_amd64.zip
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()
dir := filepath.Dir(path)
for _, file := range rc.File {
frc, e := file.Open()
if e != nil {
err = fmt.Errorf("Can't open zip entry %s when reading: %s", file.Name, err)
return
}
defer frc.Close()
if !strings.HasPrefix(file.Name, "gh") {
continue
}
dest := filepath.Join(dir, filepath.Base(file.Name))
f, e := os.Create(dest)
if e != nil {
err = e
return
}
defer f.Close()
copied, e := io.Copy(f, frc)
if e != nil {
err = e
return
}
if uint32(copied) != file.UncompressedSize {
err = fmt.Errorf("Zip entry %s is corrupted", file.Name)
return
}
exec = f.Name()
break
}
if exec == "" {
err = fmt.Errorf("No gh executable is found")
}
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)
defer resp.Body.Close()
_, err = io.Copy(file, resp.Body)
if err != nil {
return
}
path = file.Name()
return
}