From cf4e037ea325bd9456a1d1c2a4d141ca42014b57 Mon Sep 17 00:00:00 2001 From: JayChen Date: Thu, 19 Dec 2019 11:28:26 +0800 Subject: [PATCH] version 1 --- go.sum | 1 + main.go | 145 +++++++++++++++++++++++++++++++++----------------------- 2 files changed, 87 insertions(+), 59 deletions(-) diff --git a/go.sum b/go.sum index ffbe24f..fa9f1b1 100644 --- a/go.sum +++ b/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= diff --git a/main.go b/main.go index 70386d0..e947d15 100644 --- a/main.go +++ b/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 err + return } - defer f.Close() + defer r.Close() - gzf, err := gzip.NewReader(f) - if err != nil { - return err - } - defer gzf.Close() + for _, f := range r.File { + fpath := filepath.Join(wd, f.Name) - reader := tar.NewReader(gzf) - - for { - header, err := reader.Next() - if err == io.EOF { - break + if !strings.HasPrefix(fpath, filepath.Clean(wd)+string(os.PathSeparator)) { + return fmt.Errorf("%s: illegal file path", fpath) } - if err != nil { - fmt.Println("err :::: ", err) + + if f.FileInfo().IsDir() { + if err := os.MkdirAll(fpath, 0775); err != nil { + return err + } + continue + } + + if err := os.MkdirAll(filepath.Dir(fpath), 0775); err != nil && err != os.ErrExist { return err } - name := header.Name - - switch header.Typeflag { - case tar.TypeDir: - fmt.Println("Directory: ", name) - err = os.Mkdir(path.Join(wd, name), 0775) - if err != nil { - return err - } - break - case tar.TypeReg: - fmt.Println("Regular FIle: ", name) - data := make([]byte, header.Size) - _, err = reader.Read(data) - 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) + outFile, err := os.Create(fpath) + if err != nil { + return err } + defer outFile.Close() + + rc, err := f.Open() + if err != nil { + return err + } + defer rc.Close() + + _, err = io.Copy(outFile, rc) + if err != nil { + return err + } + } 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 + } + } } } - if len(link) == 0 { - return errors.New("version not match") - } } else { - link = tags[0].TarballURL + for _, v := range tags[0].Assets { + if v.Name == "release.zip" { + link = v.BrowserDownloadURL + } + } + } + if len(link) == 0 { + 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