Skip to content

Commit

Permalink
Add update subcommand for check new version
Browse files Browse the repository at this point in the history
  • Loading branch information
arimatakao committed Jun 18, 2024
1 parent 573c6ec commit 4f0cb9d
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ mdx ping

- [X] Remove Doujinshi from list in `find` subcommand and add `doujinshi` flag for show Doujinshi in list.
- [X] Add metadata for cbz downloaded archive.
- [ ] Add check update subcommand.
- [X] Add check update subcommand.
- [ ] Add self update mechanism.
- [ ] Add search filter for `find` subcommand.
- [ ] Add flag `random` in `info` subcommand for get information about random manga.
- [ ] Add flag to `download`:
Expand Down
2 changes: 1 addition & 1 deletion cmd/consts.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cmd

const (
MDX_APP_VERSION = "v1.5.1"
MDX_APP_VERSION = "v1.6.0"
MANGADEX_API_VERSION = "v5.10.2"

MDX_USER_AGENT = "mdx-cli " + MDX_APP_VERSION
Expand Down
88 changes: 88 additions & 0 deletions cmd/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package cmd

import (
"fmt"
"os"
"strconv"
"strings"
"text/tabwriter"

"github.com/go-resty/resty/v2"
"github.com/spf13/cobra"
)

var (
updateCmd = &cobra.Command{
Use: "update",
Short: "Check for updates to application",
Run: checkUpdate,
}
)

func init() {
rootCmd.AddCommand(updateCmd)
}

func checkUpdate(cmd *cobra.Command, args []string) {

result := struct {
TagName string `json:"tag_name"`
Description string `json:"body"`
}{}

resp, err := resty.New().R().
SetHeader("User-Agent", MDX_USER_AGENT).
SetHeader("Accept", "application/vnd.github+json").
SetHeader("X-GitHub-Api-Version", "2022-11-28").
SetResult(&result).
Get("https://api.github.com/repos/arimatakao/mdx/releases/latest")
if err != nil {
fmt.Println("error while connecting to github api")
os.Exit(1)
}

if resp.IsError() {
fmt.Println("wrong response from github api")
os.Exit(1)
}

isShouldUpdate := false

parsedLatest := strings.Split(result.TagName, ".")
if len(parsedLatest) == 3 {
parsedCurrent := strings.Split(MDX_APP_VERSION, ".")
mainCurrent := parsedCurrent[0]
secondCurrent, _ := strconv.Atoi(parsedCurrent[1])
thirdCurrent, _ := strconv.Atoi(parsedCurrent[2])

isNotMainVersion := !strings.Contains(parsedLatest[0], mainCurrent)
if isNotMainVersion {
isShouldUpdate = true
}
if secondLast, err := strconv.Atoi(parsedLatest[1]); err != nil {
isShouldUpdate = true
} else if secondLast > secondCurrent {
isShouldUpdate = true
}
if thirdLast, err := strconv.Atoi(parsedLatest[2]); err != nil {
isShouldUpdate = true
} else if thirdLast > thirdCurrent {
isShouldUpdate = true
}
} else if result.TagName != MDX_APP_VERSION {
isShouldUpdate = true
}

w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
fmt.Fprintf(w, "Your version\t: %s\n", MDX_APP_VERSION)
fmt.Fprintf(w, "Latest version\t: %s\n", result.TagName)
w.Flush()
if isShouldUpdate {
fmt.Printf("Download new version here: %s\n",
"https://github.com/arimatakao/mdx/releases")
fmt.Printf("Release description:\n---\n%s\n---\n",
result.Description)
} else {
fmt.Print("You have latest version.\n")
}
}

0 comments on commit 4f0cb9d

Please sign in to comment.