|
| 1 | +package command |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/AlecAivazis/survey/v2" |
| 7 | + "github.com/google/go-github/v39/github" |
| 8 | + "net/http" |
| 9 | + "net/url" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/gohade/hade/framework/cobra" |
| 13 | + "github.com/gohade/hade/framework/contract" |
| 14 | +) |
| 15 | + |
| 16 | +// initEnvCommand 获取env相关的命令 |
| 17 | +func initVersionCommand() *cobra.Command { |
| 18 | + versionCommand.AddCommand(versionListCommand) |
| 19 | + return versionCommand |
| 20 | +} |
| 21 | + |
| 22 | +var versionCommand = &cobra.Command{ |
| 23 | + Use: "version", |
| 24 | + Short: "当前hade的版本", |
| 25 | + Run: func(c *cobra.Command, args []string) { |
| 26 | + container := c.GetContainer() |
| 27 | + appService := container.MustMake(contract.AppKey).(contract.App) |
| 28 | + fmt.Println("hade version:", appService.Version()) |
| 29 | + }, |
| 30 | +} |
| 31 | + |
| 32 | +var versionListCommand = &cobra.Command{ |
| 33 | + Use: "list", |
| 34 | + Short: "获取最新的hade的版本", |
| 35 | + RunE: func(c *cobra.Command, args []string) error { |
| 36 | + // 检测到github的连接 |
| 37 | + fmt.Println("===============前置条件检测===============") |
| 38 | + fmt.Println("hade源码从github.com中下载,正在检测到github.com的连接") |
| 39 | + var client *github.Client |
| 40 | + client = github.NewClient(nil) |
| 41 | + perPage := 10 |
| 42 | + opts := &github.ListOptions{Page: 1, PerPage: perPage} |
| 43 | + releases, rsp, err := client.Repositories.ListReleases(context.Background(), "gohade", "hade", opts) |
| 44 | + fmt.Println(rsp.Rate.String()) |
| 45 | + if err != nil { |
| 46 | + if _, ok := err.(*github.RateLimitError); ok { |
| 47 | + fmt.Println("错误提示:" + err.Error()) |
| 48 | + fmt.Println("说明你的出口ip遇到github的调用限制,可以使用github.com帐号登录方式来增加调用次数") |
| 49 | + githubUserName := "" |
| 50 | + prompt := &survey.Input{ |
| 51 | + Message: "请输入github帐号用户名:", |
| 52 | + } |
| 53 | + if err := survey.AskOne(prompt, &githubUserName); err != nil { |
| 54 | + fmt.Println("任务终止:" + err.Error()) |
| 55 | + return nil |
| 56 | + } |
| 57 | + githubPassword := "" |
| 58 | + promptPwd := &survey.Password{ |
| 59 | + Message: "请输入github帐号密码:", |
| 60 | + } |
| 61 | + if err := survey.AskOne(promptPwd, &githubPassword); err != nil { |
| 62 | + fmt.Println("任务终止:" + err.Error()) |
| 63 | + return nil |
| 64 | + } |
| 65 | + |
| 66 | + httpClient := &http.Client{ |
| 67 | + Transport: &http.Transport{ |
| 68 | + Proxy: func(req *http.Request) (*url.URL, error) { |
| 69 | + req.SetBasicAuth(githubUserName, githubPassword) |
| 70 | + return nil, nil |
| 71 | + }, |
| 72 | + }, |
| 73 | + } |
| 74 | + client = github.NewClient(httpClient) |
| 75 | + releases, rsp, err = client.Repositories.ListReleases(context.Background(), "gohade", "hade", opts) |
| 76 | + if err != nil { |
| 77 | + fmt.Println("错误提示:" + err.Error()) |
| 78 | + fmt.Println("用户名密码错误,请重新开始") |
| 79 | + return nil |
| 80 | + } |
| 81 | + if len(releases) == 0 { |
| 82 | + fmt.Println("用户名密码错误,请重新开始") |
| 83 | + return nil |
| 84 | + } |
| 85 | + fmt.Println(rsp.Rate.String()) |
| 86 | + } else { |
| 87 | + fmt.Println("github.com的连接异常:" + err.Error()) |
| 88 | + return nil |
| 89 | + } |
| 90 | + } |
| 91 | + fmt.Println("hade源码从github.com中下载,github.com的连接正常") |
| 92 | + // 这里下面的client都是可用的了 |
| 93 | + if rsp.LastPage != 0 { |
| 94 | + opts = &github.ListOptions{Page: rsp.LastPage, PerPage: perPage} |
| 95 | + releases, rsp, err = client.Repositories.ListReleases(context.Background(), "gohade", "hade", opts) |
| 96 | + if err != nil { |
| 97 | + fmt.Println("任务终止:" + err.Error()) |
| 98 | + return nil |
| 99 | + } |
| 100 | + fmt.Println(rsp.Rate.String()) |
| 101 | + } |
| 102 | + fmt.Println("===============前置条件检测结束===============") |
| 103 | + fmt.Printf("\n") |
| 104 | + fmt.Printf("最新的%v个版本\n", len(releases)) |
| 105 | + for _, releaseTmp := range releases { |
| 106 | + fmt.Println("-" + releaseTmp.GetTagName()) |
| 107 | + fmt.Println(" 发布时间:" + releaseTmp.GetPublishedAt().Format("2006-01-02 15:04:05")) |
| 108 | + fmt.Println(" 修改说明:") |
| 109 | + fmt.Println(" " + strings.ReplaceAll(releaseTmp.GetBody(), "\n", "\n ")) |
| 110 | + } |
| 111 | + fmt.Printf("\n") |
| 112 | + |
| 113 | + fmt.Printf("更多历史版本请参考 https://github.com/gohade/hade/releases\n") |
| 114 | + return nil |
| 115 | + }, |
| 116 | +} |
0 commit comments