version 1

This commit is contained in:
Jay 2019-12-19 11:28:26 +08:00
parent 9ce128fa75
commit cf4e037ea3
2 changed files with 87 additions and 59 deletions

1
go.sum
View File

@ -2,6 +2,7 @@ git.trj.tw/golang/utils v0.0.0-20190225142552-b019626f0349 h1:V6ifeiJ3ExnjaUylTO
git.trj.tw/golang/utils v0.0.0-20190225142552-b019626f0349/go.mod h1:yE+qbsUsijCTdwsaQRkPT1CXYk7ftMzXsCaaYx/0QI0= git.trj.tw/golang/utils v0.0.0-20190225142552-b019626f0349/go.mod h1:yE+qbsUsijCTdwsaQRkPT1CXYk7ftMzXsCaaYx/0QI0=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=

133
main.go
View File

@ -1,8 +1,7 @@
package main package main
import ( import (
"archive/tar" "archive/zip"
"compress/gzip"
"encoding/json" "encoding/json"
"errors" "errors"
"flag" "flag"
@ -14,6 +13,7 @@ import (
"net/url" "net/url"
"os" "os"
"path" "path"
"path/filepath"
"strings" "strings"
"git.trj.tw/golang/utils" "git.trj.tw/golang/utils"
@ -23,7 +23,7 @@ import (
var version string var version string
var workdir string var workdir string
var downloadDir = os.TempDir() var downloadDir = os.TempDir()
var fileName = "koatmpl.tar" var fileName = "koatmpl.zip"
func init() { func init() {
flag.StringVar(&version, "version", "", "template version") flag.StringVar(&version, "version", "", "template version")
@ -32,7 +32,7 @@ func init() {
} }
const apiHost = "https://git.trj.tw" const apiHost = "https://git.trj.tw"
const apiPath = "/api/v1/repos/nodejs/koa-template/tags" const apiPath = "/api/v1/repos/nodejs/koa-template/releases"
type Commit struct { type Commit struct {
URL string `json:"url"` URL string `json:"url"`
@ -46,6 +46,31 @@ type Tag struct {
TarballURL string `json:"tarball_url"` TarballURL string `json:"tarball_url"`
} }
type Asset struct {
ID int `json:"id"`
Name string `json:"name"`
Size int64 `json:"size"`
DownloadCount int `json:"download_count"`
CreatedAt string `json:"created_at"`
UUID string `json:"uuid"`
BrowserDownloadURL string `json:"browser_download_url"`
}
type Release struct {
ID int `json:"id"`
TagName string `json:"tag_name"`
TargetCommitish string `json:"target_commitish"`
Name string `json:"name"`
Body string `json:"body"`
URL string `json:"url"`
TarballURL string `json:"tarball_url"`
ZipballURL string `json:"zipball_url"`
Draft bool `json:"draft"`
Prerelease bool `json:"prerelease"`
CreatedAt string `json:"created_at"`
PublishedAt string `json:"published_at"`
Assets []Asset `json:"assets"`
}
func main() { func main() {
args := flag.Args() args := flag.Args()
@ -58,11 +83,11 @@ func main() {
if len(args) == 0 { if len(args) == 0 {
// download // download
err := downloadTarball() err := downloadZipball()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
err = extractTarball() err = Unzip()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -77,7 +102,10 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
fmt.Println(list) fmt.Println("Release:")
for _, v := range list {
fmt.Println(v)
}
break break
default: default:
log.Fatalf("argv not match") log.Fatalf("argv not match")
@ -85,7 +113,7 @@ func main() {
} }
func extractTarball() (err error) { func Unzip() (err error) {
wd, err := os.Getwd() wd, err := os.Getwd()
if err != nil { if err != nil {
return err return err
@ -94,61 +122,53 @@ func extractTarball() (err error) {
wd = workdir wd = workdir
} }
f, err := os.Open(path.Join(downloadDir, fileName)) r, err := zip.OpenReader(path.Join(downloadDir, fileName))
if err != nil { if err != nil {
return
}
defer r.Close()
for _, f := range r.File {
fpath := filepath.Join(wd, f.Name)
if !strings.HasPrefix(fpath, filepath.Clean(wd)+string(os.PathSeparator)) {
return fmt.Errorf("%s: illegal file path", fpath)
}
if f.FileInfo().IsDir() {
if err := os.MkdirAll(fpath, 0775); err != nil {
return err return err
} }
defer f.Close() continue
gzf, err := gzip.NewReader(f)
if err != nil {
return err
} }
defer gzf.Close()
reader := tar.NewReader(gzf) if err := os.MkdirAll(filepath.Dir(fpath), 0775); err != nil && err != os.ErrExist {
for {
header, err := reader.Next()
if err == io.EOF {
break
}
if err != nil {
fmt.Println("err :::: ", err)
return err return err
} }
name := header.Name outFile, err := os.Create(fpath)
if err != nil {
return err
}
defer outFile.Close()
switch header.Typeflag { rc, err := f.Open()
case tar.TypeDir:
fmt.Println("Directory: ", name)
err = os.Mkdir(path.Join(wd, name), 0775)
if err != nil { if err != nil {
return err return err
} }
break defer rc.Close()
case tar.TypeReg:
fmt.Println("Regular FIle: ", name) _, err = io.Copy(outFile, rc)
data := make([]byte, header.Size)
_, err = reader.Read(data)
if err != nil { if err != nil {
return err return err
} }
err = ioutil.WriteFile(path.Join(wd, name), data, 664)
if err != nil {
return err
}
break
default:
fmt.Printf("%s : %c %s %s\n", "Unable to figure out type", header.Typeflag, "in file", name)
}
} }
return return
} }
func downloadTarball() (err error) { func downloadZipball() (err error) {
latest := true latest := true
if len(version) > 0 { if len(version) > 0 {
latest = false latest = false
@ -165,15 +185,23 @@ func downloadTarball() (err error) {
link := "" link := ""
if latest == false { if latest == false {
for _, v := range tags { for _, v := range tags {
if v.Name == version { if v.TagName == version {
link = v.TarballURL for _, it := range v.Assets {
if it.Name == "release.zip" {
link = it.BrowserDownloadURL
}
}
}
}
} else {
for _, v := range tags[0].Assets {
if v.Name == "release.zip" {
link = v.BrowserDownloadURL
}
} }
} }
if len(link) == 0 { if len(link) == 0 {
return errors.New("version not match") return errors.New("get download link fail")
}
} else {
link = tags[0].TarballURL
} }
resp, err := http.DefaultClient.Get(link) resp, err := http.DefaultClient.Get(link)
@ -195,7 +223,7 @@ func downloadTarball() (err error) {
return return
} }
func getList() (tags []Tag, err error) { func getList() (tags []Release, err error) {
u, err := url.Parse(apiHost) u, err := url.Parse(apiHost)
if err != nil { if err != nil {
return return
@ -217,13 +245,12 @@ func getList() (tags []Tag, err error) {
} }
defer resp.Body.Close() defer resp.Body.Close()
body := make([]Tag, 0) body := make([]Release, 0)
b, err := ioutil.ReadAll(resp.Body) b, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return return
} }
fmt.Println(string(b))
err = json.Unmarshal(b, &body) err = json.Unmarshal(b, &body)
if err != nil { if err != nil {
@ -237,7 +264,7 @@ func getTagList() (tags []string, err error) {
tags = make([]string, 0, len(body)) tags = make([]string, 0, len(body))
for _, v := range body { for _, v := range body {
tags = append(tags, v.Name) tags = append(tags, v.TagName)
} }
return return