|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + "text/tabwriter" |
| 9 | + |
| 10 | + "github.com/go-resty/resty/v2" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + updateCmd = &cobra.Command{ |
| 16 | + Use: "update", |
| 17 | + Short: "Check for updates to application", |
| 18 | + Run: checkUpdate, |
| 19 | + } |
| 20 | +) |
| 21 | + |
| 22 | +func init() { |
| 23 | + rootCmd.AddCommand(updateCmd) |
| 24 | +} |
| 25 | + |
| 26 | +func checkUpdate(cmd *cobra.Command, args []string) { |
| 27 | + |
| 28 | + result := struct { |
| 29 | + TagName string `json:"tag_name"` |
| 30 | + Description string `json:"body"` |
| 31 | + }{} |
| 32 | + |
| 33 | + resp, err := resty.New().R(). |
| 34 | + SetHeader("User-Agent", MDX_USER_AGENT). |
| 35 | + SetHeader("Accept", "application/vnd.github+json"). |
| 36 | + SetHeader("X-GitHub-Api-Version", "2022-11-28"). |
| 37 | + SetResult(&result). |
| 38 | + Get("https://api.github.com/repos/arimatakao/mdx/releases/latest") |
| 39 | + if err != nil { |
| 40 | + fmt.Println("error while connecting to github api") |
| 41 | + os.Exit(1) |
| 42 | + } |
| 43 | + |
| 44 | + if resp.IsError() { |
| 45 | + fmt.Println("wrong response from github api") |
| 46 | + os.Exit(1) |
| 47 | + } |
| 48 | + |
| 49 | + isShouldUpdate := false |
| 50 | + |
| 51 | + parsedLatest := strings.Split(result.TagName, ".") |
| 52 | + if len(parsedLatest) == 3 { |
| 53 | + parsedCurrent := strings.Split(MDX_APP_VERSION, ".") |
| 54 | + mainCurrent := parsedCurrent[0] |
| 55 | + secondCurrent, _ := strconv.Atoi(parsedCurrent[1]) |
| 56 | + thirdCurrent, _ := strconv.Atoi(parsedCurrent[2]) |
| 57 | + |
| 58 | + isNotMainVersion := !strings.Contains(parsedLatest[0], mainCurrent) |
| 59 | + if isNotMainVersion { |
| 60 | + isShouldUpdate = true |
| 61 | + } |
| 62 | + if secondLast, err := strconv.Atoi(parsedLatest[1]); err != nil { |
| 63 | + isShouldUpdate = true |
| 64 | + } else if secondLast > secondCurrent { |
| 65 | + isShouldUpdate = true |
| 66 | + } |
| 67 | + if thirdLast, err := strconv.Atoi(parsedLatest[2]); err != nil { |
| 68 | + isShouldUpdate = true |
| 69 | + } else if thirdLast > thirdCurrent { |
| 70 | + isShouldUpdate = true |
| 71 | + } |
| 72 | + } else if result.TagName != MDX_APP_VERSION { |
| 73 | + isShouldUpdate = true |
| 74 | + } |
| 75 | + |
| 76 | + w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0) |
| 77 | + fmt.Fprintf(w, "Your version\t: %s\n", MDX_APP_VERSION) |
| 78 | + fmt.Fprintf(w, "Latest version\t: %s\n", result.TagName) |
| 79 | + w.Flush() |
| 80 | + if isShouldUpdate { |
| 81 | + fmt.Printf("Download new version here: %s\n", |
| 82 | + "https://github.com/arimatakao/mdx/releases") |
| 83 | + fmt.Printf("Release description:\n---\n%s\n---\n", |
| 84 | + result.Description) |
| 85 | + } else { |
| 86 | + fmt.Print("You have latest version.\n") |
| 87 | + } |
| 88 | +} |
0 commit comments