Skip to content

Commit 4f0cb9d

Browse files
committed
Add update subcommand for check new version
1 parent 573c6ec commit 4f0cb9d

File tree

3 files changed

+91
-2
lines changed

3 files changed

+91
-2
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ mdx ping
116116

117117
- [X] Remove Doujinshi from list in `find` subcommand and add `doujinshi` flag for show Doujinshi in list.
118118
- [X] Add metadata for cbz downloaded archive.
119-
- [ ] Add check update subcommand.
119+
- [X] Add check update subcommand.
120+
- [ ] Add self update mechanism.
120121
- [ ] Add search filter for `find` subcommand.
121122
- [ ] Add flag `random` in `info` subcommand for get information about random manga.
122123
- [ ] Add flag to `download`:

cmd/consts.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package cmd
22

33
const (
4-
MDX_APP_VERSION = "v1.5.1"
4+
MDX_APP_VERSION = "v1.6.0"
55
MANGADEX_API_VERSION = "v5.10.2"
66

77
MDX_USER_AGENT = "mdx-cli " + MDX_APP_VERSION

cmd/update.go

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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

Comments
 (0)