diff options
author | Vidhu Kant Sharma <vidhukant@vidhukant.xyz> | 2022-09-30 15:28:27 +0530 |
---|---|---|
committer | Vidhu Kant Sharma <vidhukant@vidhukant.xyz> | 2022-09-30 15:28:27 +0530 |
commit | e1bd26b9ad73cd0758ba77373c1cd07f72f47a65 (patch) | |
tree | bfcba01cdf2b5e0d3f659b097d3683eb83fb89d0 | |
parent | d67758bdbeb162adadb6b19954e8e22cf04ed388 (diff) |
reading both flags and config for list commands
-rw-r--r-- | cmd/list.go | 29 | ||||
-rw-r--r-- | cmd/seasonals.go | 24 | ||||
-rw-r--r-- | macli.yaml | 2 | ||||
-rw-r--r-- | util/bind_config.go | 43 |
4 files changed, 71 insertions, 27 deletions
diff --git a/cmd/list.go b/cmd/list.go index e4dbcf1..9cc0559 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -22,11 +22,10 @@ import ( "os" "fmt" "github.com/MikunoNaka/macli/ui" + "github.com/MikunoNaka/macli/util" "github.com/MikunoNaka/macli/mal" - // m "github.com/MikunoNaka/MAL2Go/v4/manga" "github.com/spf13/cobra" - "github.com/spf13/viper" ) // statusCmd represents the status command @@ -41,6 +40,14 @@ var listCmd = &cobra.Command{ // " - \x1b[33m`macli chapters -s +1 <anime-name>`\x1b[0m to increment the chapters by 1\n" + // " - \x1b[33m`macli chapters -s -2 <anime-name>`\x1b[0m to decrement the chapters by 2\n", Run: func(cmd *cobra.Command, args []string) { + conf, err := util.BindListConfig(cmd.Flags()) + if err != nil { + fmt.Println("Error while parsing flags.", err.Error()) + os.Exit(1) + } + mal.SearchLength = conf.ResultsLength + mal.SearchOffset = conf.ResultsOffset + mal.SearchNSFW = conf.IncludeNSFW mal.Init() status, err := cmd.Flags().GetString("status") @@ -61,16 +68,10 @@ var listCmd = &cobra.Command{ os.Exit(1) } - nsfw, err := cmd.Flags().GetBool("include-nsfw") - if err != nil { - fmt.Println("error while reading \x1b[33m--include-nsfw\x1b[0m flag:", err) - os.Exit(1) - } - if mangaMode { - ui.MangaList(mal.MangaList(user, status, sort, nsfw)) + ui.MangaList(mal.MangaList(user, status, sort, conf.IncludeNSFW)) } else { - ui.AnimeList(mal.AnimeList(user, status, sort, nsfw)) + ui.AnimeList(mal.AnimeList(user, status, sort, conf.IncludeNSFW)) } }, @@ -81,13 +82,11 @@ func init() { listCmd.Flags().StringP("status", "", "", "Status (leave blank for all)") listCmd.Flags().StringP("user", "", "@me", "User (@me or blank for self)") listCmd.Flags().StringP("sort", "", "list_score", "Sort the list") - listCmd.Flags().BoolP("include-nsfw", "", false, "Include NSFW results") listCmd.Flags().BoolVarP(&mangaMode, "manga", "m", false, "Use manga mode") listCmd.Flags().StringVarP(&mal.Secret, "authentication-token", "t", "", "MyAnimeList authentication token to use (overrides system keyring if any)") listCmd.Flags().IntVarP(&mal.SearchLength, "list-length", "n", 15, "Amount of list items to load (default: all)") - listCmd.Flags().IntVarP(&mal.SearchOffset, "list-offset", "o", 0, "Offset for the list") - viper.BindPFlag("lists.list_offset", listCmd.Flags().Lookup("list-offset")) - viper.BindPFlag("lists.list_length", listCmd.Flags().Lookup("list-length")) - viper.BindPFlag("lists.include_nsfw_results", listCmd.Flags().Lookup("include-nsfw")) + listCmd.Flags().IntP("results-length", "n", 10, "Amount of results to load") + listCmd.Flags().IntP("results-offset", "o", 0, "Offset for the results") + listCmd.Flags().BoolP("include-nsfw", "", false, "Include NSFW-rated items in search results") } diff --git a/cmd/seasonals.go b/cmd/seasonals.go index b78876a..3ea5012 100644 --- a/cmd/seasonals.go +++ b/cmd/seasonals.go @@ -19,7 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. package cmd import ( - // "os" + "os" "fmt" // "strings" // "github.com/MikunoNaka/macli/ui" @@ -33,13 +33,22 @@ import ( // statusCmd represents the status command var seasonalsCmd = &cobra.Command{ Use: "seasonals", - Short: "Get seasonal animes", - Long: "" + + Short: "Get seasonal animes (under construction)", + Long: "under construction" + "" + "", Run: func(cmd *cobra.Command, args []string) { + conf, err := util.BindListConfig(cmd.Flags()) + if err != nil { + fmt.Println("Error while parsing flags.", err.Error()) + os.Exit(1) + } + mal.SearchLength = conf.ResultsLength + mal.SearchOffset = conf.ResultsOffset + mal.SearchNSFW = conf.IncludeNSFW mal.Init() + season := util.GetCurrentSeason() sort, _ := cmd.Flags().GetString("sort") @@ -59,15 +68,12 @@ var seasonalsCmd = &cobra.Command{ func init() { rootCmd.AddCommand(seasonalsCmd) - seasonalsCmd.Flags().IntVarP(&mal.SearchLength, "results-length", "n", 15, "Amount of results to load") - seasonalsCmd.Flags().BoolVarP(&mal.SearchNSFW, "include-nsfw", "", false, "Include NSFW-rated items in results") - seasonalsCmd.Flags().IntVarP(&mal.SearchOffset, "results-offset", "o", 0, "Offset for the results") seasonalsCmd.Flags().StringP("sort", "", "anime_num_list_users", "sort") seasonalsCmd.Flags().StringP("season", "", "", "") seasonalsCmd.Flags().IntP("year", "", 0, "") seasonalsCmd.Flags().StringVarP(&mal.Secret, "authentication-token", "t", "", "MyAnimeList authentication token to use (overrides system keyring if any)") - viper.BindPFlag("lists.list_length", seasonalsCmd.Flags().Lookup("results-length")) - viper.BindPFlag("lists.list_offset", seasonalsCmd.Flags().Lookup("results-offset")) - viper.BindPFlag("lists.include_nsfw_results", seasonalsCmd.Flags().Lookup("include-nsfw")) + seasonalsCmd.Flags().IntP("results-length", "n", 10, "Amount of results to load") + seasonalsCmd.Flags().IntP("results-offset", "o", 0, "Offset for the results") + seasonalsCmd.Flags().BoolP("include-nsfw", "", false, "Include NSFW-rated items in search results") } @@ -34,6 +34,6 @@ searching: lists: # TODO: add default sort types, etc - list_offset: 0 # offset for the results (default is 0, changing it here is not recommended as it may cause confusion) list_length: 15 # amount of titles to show from user's list + list_offset: 0 # offset for the results (default is 0, changing it here is not recommended as it may cause confusion) include_nsfw_results: false # show NSFW rated items diff --git a/util/bind_config.go b/util/bind_config.go index f849398..2ab6cc9 100644 --- a/util/bind_config.go +++ b/util/bind_config.go @@ -36,10 +36,18 @@ type SearchConfig struct { SearchNSFW bool } +type ListConfig struct { + ResultsLength int + ResultsOffset int + IncludeNSFW bool +} + // handles prompt-length, search-length, search-offset and search-nsfw func BindSearchConfig(flags *pflag.FlagSet) (SearchConfig, error) { - var conf SearchConfig - var err error + var ( + conf SearchConfig + err error + ) if flags.Lookup("prompt-length").Changed { conf.PromptLength, err = flags.GetInt("prompt-length") @@ -71,3 +79,34 @@ func BindSearchConfig(flags *pflag.FlagSet) (SearchConfig, error) { return conf, nil } + +// handles results-length, results-offset, include-nsfw +func BindListConfig(flags *pflag.FlagSet) (ListConfig, error) { + var ( + conf ListConfig + err error + ) + + if flags.Lookup("results-length").Changed { + conf.ResultsLength, err = flags.GetInt("results-length") + if err != nil {return conf, err} + } else { + conf.ResultsLength = viper.GetInt("lists.list_length") + } + + if flags.Lookup("results-offset").Changed { + conf.ResultsOffset, err = flags.GetInt("results-offset") + if err != nil {return conf, err} + } else { + conf.ResultsOffset = viper.GetInt("lists.list_offset") + } + + if flags.Lookup("include-nsfw").Changed { + conf.IncludeNSFW, err = flags.GetBool("include-nsfw") + if err != nil {return conf, err} + } else { + conf.IncludeNSFW = viper.GetBool("lists.include_nsfw_results") + } + + return conf, nil +} |