diff options
author | Vidhu Kant Sharma <vidhukant@vidhukant.xyz> | 2022-07-02 16:46:14 +0530 |
---|---|---|
committer | Vidhu Kant Sharma <vidhukant@vidhukant.xyz> | 2022-07-02 16:46:14 +0530 |
commit | fb7324295cf3f32c530135a0456f0afd14324d29 (patch) | |
tree | 1118db78fdf54f63fc8596bc41afee9b26ff4935 /cmd | |
parent | 720045d9ea2e612ca0f0b7333ee62a73b2955a15 (diff) |
incrementing/decrementing score now
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/score.go | 40 | ||||
-rw-r--r-- | cmd/version.go | 2 |
2 files changed, 33 insertions, 9 deletions
diff --git a/cmd/score.go b/cmd/score.go index f97de87..27106e5 100644 --- a/cmd/score.go +++ b/cmd/score.go @@ -22,8 +22,11 @@ import ( "fmt" "os" "strings" + "strconv" + "errors" "github.com/MikunoNaka/macli/ui" "github.com/MikunoNaka/macli/mal" + "github.com/MikunoNaka/macli/util" "github.com/spf13/cobra" ) @@ -41,7 +44,7 @@ var scoreCmd = &cobra.Command{ mal.Init() searchInput := strings.Join(args, " ") - scoreInput, err := cmd.Flags().GetInt("set-value") + scoreInput, err := cmd.Flags().GetString("set-value") if err != nil { fmt.Println("Error while reading \x1b[33m--set-value\x1b[0m flag.", err.Error()) os.Exit(1) @@ -62,7 +65,18 @@ var scoreCmd = &cobra.Command{ }, } -func setAnimeScore(scoreInput int, searchInput string) { +func validateScore(input string, currentScore int) (int, error) { + parsedScore := util.ParseNumeric(input, currentScore) + if parsedScore > 10 { + return currentScore, errors.New("\x1b[31mScore out of range (" + strconv.Itoa(parsedScore) + " > 10)\x1b[0m") + } + if parsedScore < 0 { + return currentScore, errors.New("\x1b[31mScore out of range (" + strconv.Itoa(parsedScore) + " < 0)\x1b[0m") + } + return parsedScore, nil +} + +func setAnimeScore(scoreInput, searchInput string) { if searchInput == "" { var promptText string if queryOnlyMode { @@ -82,15 +96,20 @@ func setAnimeScore(scoreInput int, searchInput string) { os.Exit(0) } - if scoreInput < 0 { + if scoreInput == "" { ui.ScoreInput(anime.Id, selectedAnime.MyListStatus.Score, anime.Title, false) } else { - resp := mal.SetAnimeScore(anime.Id, scoreInput) + score, err := validateScore(scoreInput, selectedAnime.MyListStatus.Score) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + resp := mal.SetAnimeScore(anime.Id, score) fmt.Println(ui.CreateScoreUpdateConfirmationMessage(anime.Title, currentScore, resp.Score)) } } -func setMangaScore(scoreInput int, searchInput string) { +func setMangaScore(scoreInput, searchInput string) { if searchInput == "" { var promptText string if queryOnlyMode { @@ -110,15 +129,20 @@ func setMangaScore(scoreInput int, searchInput string) { os.Exit(0) } - if scoreInput < 0 { + if scoreInput == "" { ui.ScoreInput(manga.Id, selectedManga.MyListStatus.Score, manga.Title, true) } else { - resp := mal.SetMangaScore(manga.Id, scoreInput) + score, err := validateScore(scoreInput, selectedManga.MyListStatus.Score) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + resp := mal.SetMangaScore(manga.Id, score) fmt.Println(ui.CreateScoreUpdateConfirmationMessage(manga.Title, currentScore, resp.Score)) } } func init() { rootCmd.AddCommand(scoreCmd) - scoreCmd.Flags().IntP("set-value", "s", -1, "Score to be set") + scoreCmd.Flags().StringP("set-value", "s", "", "Score to be set") } diff --git a/cmd/version.go b/cmd/version.go index 0901980..174b7e3 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -24,7 +24,7 @@ import ( "github.com/spf13/cobra" ) -var version string = "v1.5.1" +var version string = "v1.5.3" var versionCmd = &cobra.Command { Use: "version", |