|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "archive/zip" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "github.com/samber/lo" |
| 9 | + "github.com/schollz/progressbar/v3" |
| 10 | + "github.com/spf13/cobra" |
| 11 | + "io" |
| 12 | + "net/http" |
| 13 | + "os" |
| 14 | + "path/filepath" |
| 15 | + "runtime" |
| 16 | + "strings" |
| 17 | + "time" |
| 18 | +) |
| 19 | + |
| 20 | +var upgradeCmd = &cobra.Command{ |
| 21 | + Use: "upgrade", |
| 22 | + Short: "Upgrade to the latest version or specified version", |
| 23 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 24 | + if repoUrl != "" { |
| 25 | + return downloadInstall(repoUrl) |
| 26 | + } |
| 27 | + |
| 28 | + //查询 github |
| 29 | + assets, err := downloadCheck(repoVersion) |
| 30 | + if err != nil { |
| 31 | + return err |
| 32 | + } |
| 33 | + return downloadInstall(assets.Url) |
| 34 | + }, |
| 35 | +} |
| 36 | + |
| 37 | +func init() { |
| 38 | + upgradeCmd.Flags().StringVarP(&repoUrl, "download url", "d", repoUrl, "repo download url") |
| 39 | + upgradeCmd.Flags().StringVarP(&repoVersion, "repo version", "v", repoVersion, "repo version") |
| 40 | +} |
| 41 | + |
| 42 | +const repo = "https://api.github.com/repos/otk-final/openapi-codegen/releases" |
| 43 | + |
| 44 | +var repoUrl = "" |
| 45 | +var repoVersion = "latest" |
| 46 | +var repoName = "openapi" |
| 47 | + |
| 48 | +type Assets struct { |
| 49 | + Name string `json:"name"` |
| 50 | + Url string `json:"browser_download_url"` |
| 51 | +} |
| 52 | +type RepoInfo struct { |
| 53 | + Tag string `json:"tag"` |
| 54 | + Name string `json:"name"` |
| 55 | + CreatedAt string `json:"created_at"` |
| 56 | + PublishedAt string `json:"published_at"` |
| 57 | + Assets []*Assets `json:"assets"` |
| 58 | +} |
| 59 | + |
| 60 | +func downloadCheck(version string) (*Assets, error) { |
| 61 | + |
| 62 | + //查询 |
| 63 | + resp, err := http.Get(repo + "/" + version) |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + defer func() { |
| 68 | + _ = resp.Body.Close() |
| 69 | + }() |
| 70 | + |
| 71 | + body, _ := io.ReadAll(resp.Body) |
| 72 | + |
| 73 | + //解码 |
| 74 | + var info RepoInfo |
| 75 | + _ = json.Unmarshal(body, &info) |
| 76 | + |
| 77 | + //获取当前os 安装包 |
| 78 | + assets, ok := lo.Find(info.Assets, func(item *Assets) bool { |
| 79 | + return strings.Contains(item.Name, runtime.GOOS) |
| 80 | + }) |
| 81 | + if ok { |
| 82 | + return assets, nil |
| 83 | + } |
| 84 | + return nil, errors.New("not found assets") |
| 85 | +} |
| 86 | + |
| 87 | +func downloadInstall(url string) error { |
| 88 | + fmt.Printf("start download %s\n", url) |
| 89 | + |
| 90 | + //下载 |
| 91 | + resp, err := http.Get(url) |
| 92 | + if err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + |
| 96 | + defer func() { |
| 97 | + _ = resp.Body.Close() |
| 98 | + }() |
| 99 | + |
| 100 | + //临时目录 文件 |
| 101 | + tempDir, _ := os.UserCacheDir() |
| 102 | + tempDir = filepath.Join(tempDir, repoName) |
| 103 | + _ = os.MkdirAll(tempDir, os.ModePerm) |
| 104 | + |
| 105 | + tempFile, err := os.OpenFile(filepath.Join(tempDir, fmt.Sprintf("%d.zip", time.Now().UnixMilli())), os.O_CREATE|os.O_WRONLY, 0644) |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + |
| 110 | + //写入文件 |
| 111 | + bar := progressbar.DefaultBytes(resp.ContentLength, "downloading") |
| 112 | + _, err = io.Copy(io.MultiWriter(tempFile, bar), resp.Body) |
| 113 | + if err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + fmt.Printf("download completed\n") |
| 117 | + |
| 118 | + //解压文件 |
| 119 | + tempZip, _ := zip.OpenReader(tempFile.Name()) |
| 120 | + defer func() { |
| 121 | + _ = tempZip.Close() |
| 122 | + _ = os.Remove(tempFile.Name()) |
| 123 | + }() |
| 124 | + |
| 125 | + //获取指定文件 |
| 126 | + newFile, ok := lo.Find(tempZip.File, func(item *zip.File) bool { |
| 127 | + return strings.Contains(item.Name, repoName) |
| 128 | + }) |
| 129 | + if !ok { |
| 130 | + return errors.New("not found install file from zip") |
| 131 | + } |
| 132 | + |
| 133 | + newReader, _ := newFile.Open() |
| 134 | + newBytes, _ := io.ReadAll(newReader) |
| 135 | + |
| 136 | + //获取真实安装路径 |
| 137 | + exePath, _ := os.Executable() |
| 138 | + realPath, _ := filepath.EvalSymlinks(exePath) |
| 139 | + |
| 140 | + //替换文件 覆盖 |
| 141 | + return os.WriteFile(realPath, newBytes, os.ModePerm) |
| 142 | +} |
0 commit comments