From 9ce128fa75e99ef44f98eab0ca6b27f90615c631 Mon Sep 17 00:00:00 2001 From: JayChen Date: Wed, 18 Dec 2019 18:08:07 +0800 Subject: [PATCH] first --- go.mod | 8 ++ go.sum | 14 ++++ main.go | 244 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 266 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..732f929 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module koa-init + +go 1.13 + +require ( + git.trj.tw/golang/utils v0.0.0-20190225142552-b019626f0349 + github.com/sirupsen/logrus v1.4.2 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..ffbe24f --- /dev/null +++ b/go.sum @@ -0,0 +1,14 @@ +git.trj.tw/golang/utils v0.0.0-20190225142552-b019626f0349 h1:V6ifeiJ3ExnjaUylTOz37n6z5uLwm6fjKjnztbTCaQI= +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/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= +github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4= +github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/main.go b/main.go new file mode 100644 index 0000000..70386d0 --- /dev/null +++ b/main.go @@ -0,0 +1,244 @@ +package main + +import ( + "archive/tar" + "compress/gzip" + "encoding/json" + "errors" + "flag" + "fmt" + "io" + "io/ioutil" + "log" + "net/http" + "net/url" + "os" + "path" + "strings" + + "git.trj.tw/golang/utils" + "github.com/sirupsen/logrus" +) + +var version string +var workdir string +var downloadDir = os.TempDir() +var fileName = "koatmpl.tar" + +func init() { + flag.StringVar(&version, "version", "", "template version") + flag.StringVar(&workdir, "wd", "", "working directory") + flag.Parse() +} + +const apiHost = "https://git.trj.tw" +const apiPath = "/api/v1/repos/nodejs/koa-template/tags" + +type Commit struct { + URL string `json:"url"` + SHA string `json:"sha"` +} +type Tag struct { + Name string `json:"name"` + ID string `json:"id"` + Commit Commit `json:"commit"` + ZipballURL string `json:"zipball_url"` + TarballURL string `json:"tarball_url"` +} + +func main() { + args := flag.Args() + + if len(workdir) > 0 { + workdir = utils.ParsePath(workdir) + if !utils.IsDir(workdir) { + log.Fatalf("target directory not exists") + } + } + + if len(args) == 0 { + // download + err := downloadTarball() + if err != nil { + log.Fatal(err) + } + err = extractTarball() + if err != nil { + log.Fatal(err) + } + fmt.Println("Success") + return + } + + switch strings.ToLower(args[0]) { + case "list": + list, err := getTagList() + if err != nil { + log.Fatal(err) + } + + fmt.Println(list) + break + default: + log.Fatalf("argv not match") + } + +} + +func extractTarball() (err error) { + wd, err := os.Getwd() + if err != nil { + return err + } + if len(workdir) > 0 { + wd = workdir + } + + f, err := os.Open(path.Join(downloadDir, fileName)) + if err != nil { + return err + } + defer f.Close() + + gzf, err := gzip.NewReader(f) + if err != nil { + return err + } + defer gzf.Close() + + reader := tar.NewReader(gzf) + + for { + header, err := reader.Next() + if err == io.EOF { + break + } + if err != nil { + fmt.Println("err :::: ", err) + 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) + } + } + + return +} + +func downloadTarball() (err error) { + latest := true + if len(version) > 0 { + latest = false + } + + tags, err := getList() + if err != nil { + return + } + if len(tags) == 0 { + return errors.New("no release tag") + } + + link := "" + if latest == false { + for _, v := range tags { + if v.Name == version { + link = v.TarballURL + } + } + if len(link) == 0 { + return errors.New("version not match") + } + } else { + link = tags[0].TarballURL + } + + resp, err := http.DefaultClient.Get(link) + if err != nil { + return + } + defer resp.Body.Close() + + f, err := os.Create(path.Join(downloadDir, fileName)) + if err != nil { + return + } + + _, err = io.Copy(f, resp.Body) + if err != nil { + return + } + + return +} + +func getList() (tags []Tag, err error) { + u, err := url.Parse(apiHost) + if err != nil { + return + } + u, err = u.Parse(apiPath) + if err != nil { + return + } + + req, err := http.NewRequest("GET", u.String(), nil) + if err != nil { + return + } + + logrus.Debugf("Try send request to api: %s\n", u.String()) + resp, err := http.DefaultClient.Do(req) + if err != nil { + return + } + defer resp.Body.Close() + + body := make([]Tag, 0) + + b, err := ioutil.ReadAll(resp.Body) + if err != nil { + return + } + fmt.Println(string(b)) + + err = json.Unmarshal(b, &body) + if err != nil { + return + } + return body, nil +} + +func getTagList() (tags []string, err error) { + body, err := getList() + + tags = make([]string, 0, len(body)) + for _, v := range body { + tags = append(tags, v.Name) + } + + return +}