version 1
This commit is contained in:
parent
9ce128fa75
commit
cf4e037ea3
1
go.sum
1
go.sum
@ -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=
|
||||
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/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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
|
133
main.go
133
main.go
@ -1,8 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"compress/gzip"
|
||||
"archive/zip"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
@ -14,6 +13,7 @@ import (
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"git.trj.tw/golang/utils"
|
||||
@ -23,7 +23,7 @@ import (
|
||||
var version string
|
||||
var workdir string
|
||||
var downloadDir = os.TempDir()
|
||||
var fileName = "koatmpl.tar"
|
||||
var fileName = "koatmpl.zip"
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&version, "version", "", "template version")
|
||||
@ -32,7 +32,7 @@ func init() {
|
||||
}
|
||||
|
||||
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 {
|
||||
URL string `json:"url"`
|
||||
@ -46,6 +46,31 @@ type Tag struct {
|
||||
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() {
|
||||
args := flag.Args()
|
||||
|
||||
@ -58,11 +83,11 @@ func main() {
|
||||
|
||||
if len(args) == 0 {
|
||||
// download
|
||||
err := downloadTarball()
|
||||
err := downloadZipball()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = extractTarball()
|
||||
err = Unzip()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@ -77,7 +102,10 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println(list)
|
||||
fmt.Println("Release:")
|
||||
for _, v := range list {
|
||||
fmt.Println(v)
|
||||
}
|
||||
break
|
||||
default:
|
||||
log.Fatalf("argv not match")
|
||||
@ -85,7 +113,7 @@ func main() {
|
||||
|
||||
}
|
||||
|
||||
func extractTarball() (err error) {
|
||||
func Unzip() (err error) {
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return err
|
||||
@ -94,61 +122,53 @@ func extractTarball() (err error) {
|
||||
wd = workdir
|
||||
}
|
||||
|
||||
f, err := os.Open(path.Join(downloadDir, fileName))
|
||||
r, err := zip.OpenReader(path.Join(downloadDir, fileName))
|
||||
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
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
gzf, err := gzip.NewReader(f)
|
||||
if err != nil {
|
||||
return err
|
||||
continue
|
||||
}
|
||||
defer gzf.Close()
|
||||
|
||||
reader := tar.NewReader(gzf)
|
||||
|
||||
for {
|
||||
header, err := reader.Next()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Println("err :::: ", err)
|
||||
if err := os.MkdirAll(filepath.Dir(fpath), 0775); err != nil && err != os.ErrExist {
|
||||
return err
|
||||
}
|
||||
|
||||
name := header.Name
|
||||
outFile, err := os.Create(fpath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer outFile.Close()
|
||||
|
||||
switch header.Typeflag {
|
||||
case tar.TypeDir:
|
||||
fmt.Println("Directory: ", name)
|
||||
err = os.Mkdir(path.Join(wd, name), 0775)
|
||||
rc, err := f.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
break
|
||||
case tar.TypeReg:
|
||||
fmt.Println("Regular FIle: ", name)
|
||||
data := make([]byte, header.Size)
|
||||
_, err = reader.Read(data)
|
||||
defer rc.Close()
|
||||
|
||||
_, err = io.Copy(outFile, rc)
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
func downloadTarball() (err error) {
|
||||
func downloadZipball() (err error) {
|
||||
latest := true
|
||||
if len(version) > 0 {
|
||||
latest = false
|
||||
@ -165,15 +185,23 @@ func downloadTarball() (err error) {
|
||||
link := ""
|
||||
if latest == false {
|
||||
for _, v := range tags {
|
||||
if v.Name == version {
|
||||
link = v.TarballURL
|
||||
if v.TagName == version {
|
||||
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 {
|
||||
return errors.New("version not match")
|
||||
}
|
||||
} else {
|
||||
link = tags[0].TarballURL
|
||||
return errors.New("get download link fail")
|
||||
}
|
||||
|
||||
resp, err := http.DefaultClient.Get(link)
|
||||
@ -195,7 +223,7 @@ func downloadTarball() (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func getList() (tags []Tag, err error) {
|
||||
func getList() (tags []Release, err error) {
|
||||
u, err := url.Parse(apiHost)
|
||||
if err != nil {
|
||||
return
|
||||
@ -217,13 +245,12 @@ func getList() (tags []Tag, err error) {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body := make([]Tag, 0)
|
||||
body := make([]Release, 0)
|
||||
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
fmt.Println(string(b))
|
||||
|
||||
err = json.Unmarshal(b, &body)
|
||||
if err != nil {
|
||||
@ -237,7 +264,7 @@ func getTagList() (tags []string, err error) {
|
||||
|
||||
tags = make([]string, 0, len(body))
|
||||
for _, v := range body {
|
||||
tags = append(tags, v.Name)
|
||||
tags = append(tags, v.TagName)
|
||||
}
|
||||
|
||||
return
|
||||
|
Loading…
Reference in New Issue
Block a user