commit 426c60c7a42567ebadb4cd49effeb77df2869b01 Author: suguo.yao Date: Fri Sep 3 12:14:46 2021 +0800 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c089143 --- /dev/null +++ b/.gitignore @@ -0,0 +1,20 @@ +# ---> Go +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ +go.sum +myschools +myschools-dll +.vscode/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..2071b23 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..04b8a24 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# 标准组件下载工具 + +#### 使用方法 +1. 安装 +go install myschools.me/suguo/myschools-dl +2. 使用 +myschools-dl -dl mysql diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..76858c2 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module myschools.me/suguo/myschools-dl + +go 1.17 diff --git a/main.go b/main.go new file mode 100644 index 0000000..6f72fff --- /dev/null +++ b/main.go @@ -0,0 +1,121 @@ +/* +Author: suguo.yao(ysg@myschools.me) +Description: 用于从github.com中下载标准组件 +*/ +package main + +import ( + "flag" + "fmt" + "io" + "io/ioutil" + "net/http" + "os" + "path/filepath" + "regexp" + "strings" + "sync" + "time" +) + +const GITEA = "https://myschools.me" +const PATH = "/suguo/norm/src/branch/master" + +var path string + +func init() { + flag.StringVar(&path, "dl", "", "下载组件名") +} + +func main() { + flag.Parse() + if path == "" { + panic("下载组件名指定") + } + var client http.Client + var wg sync.WaitGroup + start := time.Now() + dl(client, path, &wg) + wg.Wait() + fmt.Printf("total time: %.2f s\n", float64(time.Since(start))/float64(time.Second)) +} + +// get all file link and download it +func dl(client http.Client, path string, wg *sync.WaitGroup) { + if !isExist(path) { + os.MkdirAll(path, 0775) + } + + url := fmt.Sprintf("%s%s/%s", GITEA, PATH, path) + html, err := getHtml(client, url) + if err != nil { + fmt.Printf("get html error: %s", err.Error()) + return + } + urlPattern := regexp.MustCompile(fmt.Sprintf(`%s/%s/\S*go`, PATH, path)) + links := urlPattern.FindAllSubmatch(html, -1) + for _, link := range links { + tmp := strings.Split(string(link[0]), "/") + filename := tmp[len(tmp)-1] + wg.Add(1) + go downloadFile(client, path, filename, wg) + + } +} + +// download file +func downloadFile(client http.Client, path, filename string, wg *sync.WaitGroup) { + defer wg.Done() + fmt.Println("start to download: ", filename) + fileURL := fmt.Sprintf("%s/suguo/norm/raw/branch/master/%s/%s", GITEA, path, filename) + resp, err := client.Get(fileURL) + if err != nil { + fmt.Printf("download file %s failed due to: %s\n", filename, err.Error()) + return + } + defer resp.Body.Close() + var buff [1024]byte + // 创建文件 + file, err := os.Create(filepath.Join(path, filename)) + if err != nil { + fmt.Printf("create file: %s error\n", filename) + return + } + defer file.Close() + // 写入文件 + for { + n, err := resp.Body.Read(buff[:]) + if err != nil { + if err == io.EOF { + file.Write(buff[:n]) + break + } + fmt.Println("error: ", err) + // if failed delete this file + os.Remove(filepath.Join(path, filename)) + return + } + file.Write(buff[:n]) + } + fmt.Println("finish download:", filename) +} + +// get html source +func getHtml(client http.Client, url string) ([]byte, error) { + resp, err := client.Get(url) + if err != nil { + return nil, err + } + defer resp.Body.Close() + data, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + return data, nil +} + +// if file or directory exits +func isExist(path string) bool { + _, err := os.Stat(path) + return !os.IsNotExist(err) +}