From adffba663dbe8daebe311dc8f3ae5d40109cb2dd Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Wed, 17 Aug 2022 22:33:40 +0530 Subject: reading config file's defaults while searching --- cmd/chapters.go | 8 ++++---- cmd/episodes.go | 8 ++++---- cmd/list.go | 5 ++++- cmd/login.go | 6 +++--- cmd/root.go | 24 ++++++++++-------------- cmd/score.go | 8 ++++---- cmd/search.go | 9 +++++---- cmd/seasonals.go | 4 +--- cmd/status.go | 1 + macli.yaml | 15 ++++++++++++--- mal/mal.go | 29 +++++++++++++++++++++++++++++ ui/ui.go | 26 ++++++++++++++++++++++++-- 12 files changed, 101 insertions(+), 42 deletions(-) diff --git a/cmd/chapters.go b/cmd/chapters.go index 2728beb..4e5be08 100644 --- a/cmd/chapters.go +++ b/cmd/chapters.go @@ -92,10 +92,10 @@ var chaptersCmd = &cobra.Command{ func init() { rootCmd.AddCommand(chaptersCmd) chaptersCmd.Flags().StringP("set-value", "s", "", "Number of chapters") - chaptersCmd.Flags().IntVarP(&ui.PromptLength, "prompt-length", "l", 5, "Length of select prompt") - chaptersCmd.Flags().IntVarP(&mal.SearchLength, "search-length", "n", 10, "Amount of search results to load") - chaptersCmd.Flags().IntVarP(&mal.SearchOffset, "search-offset", "o", 0, "Offset for the search results") - chaptersCmd.Flags().BoolVarP(&mal.SearchNSFW, "search-nsfw", "", false, "Include NSFW-rated items in search results") + chaptersCmd.Flags().IntVarP(&ui.PromptLength, "prompt-length", "l", promptLength, "Length of select prompt") + chaptersCmd.Flags().IntVarP(&mal.SearchLength, "search-length", "n", searchLength, "Amount of search results to load") + chaptersCmd.Flags().IntVarP(&mal.SearchOffset, "search-offset", "o", searchOffset, "Offset for the search results") + chaptersCmd.Flags().BoolVarP(&mal.SearchNSFW, "search-nsfw", "", searchNsfw, "Include NSFW-rated items in search results") chaptersCmd.Flags().BoolVarP(&queryOnlyMode, "query", "q", false, "Query only (don't update data)") chaptersCmd.Flags().IntVarP(&entryId, "id", "i", -1, "Manually specify the ID of anime/manga (overrides search)") chaptersCmd.Flags().StringVarP(&mal.Secret, "authentication-token", "t", "", "MyAnimeList authentication token to use (overrides system keyring if any)") diff --git a/cmd/episodes.go b/cmd/episodes.go index e760eff..0423679 100644 --- a/cmd/episodes.go +++ b/cmd/episodes.go @@ -92,11 +92,11 @@ var episodesCmd = &cobra.Command{ func init() { rootCmd.AddCommand(episodesCmd) episodesCmd.Flags().StringP("set-value", "s", "", "Number of episodes") - episodesCmd.Flags().IntVarP(&ui.PromptLength, "prompt-length", "l", 5, "Length of select prompt") + episodesCmd.Flags().IntVarP(&ui.PromptLength, "prompt-length", "l", promptLength, "Length of select prompt") episodesCmd.Flags().BoolVarP(&queryOnlyMode, "query", "q", false, "Query only (don't update data)") - episodesCmd.Flags().IntVarP(&mal.SearchLength, "search-length", "n", 10, "Amount of search results to load") - episodesCmd.Flags().BoolVarP(&mal.SearchNSFW, "search-nsfw", "", false, "Include NSFW-rated items in search results") - episodesCmd.Flags().IntVarP(&mal.SearchOffset, "search-offset", "o", 0, "Offset for the search results") + episodesCmd.Flags().IntVarP(&mal.SearchLength, "search-length", "n", searchLength, "Amount of search results to load") + episodesCmd.Flags().BoolVarP(&mal.SearchNSFW, "search-nsfw", "", searchNsfw, "Include NSFW-rated items in search results") + episodesCmd.Flags().IntVarP(&mal.SearchOffset, "search-offset", "o", searchOffset, "Offset for the search results") episodesCmd.Flags().IntVarP(&entryId, "id", "i", -1, "Manually specify the ID of anime/manga (overrides search)") episodesCmd.Flags().StringVarP(&mal.Secret, "authentication-token", "t", "", "MyAnimeList authentication token to use (overrides system keyring if any)") } diff --git a/cmd/list.go b/cmd/list.go index 2b9cc57..6cb95fb 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -80,7 +80,10 @@ 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().BoolP("include-nsfw", "", listIncludeNsfw, "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)") + // TODO: implement below 2 flags + // listCmd.Flags().IntVarP(&mal.SearchLength, "list-length", "n", listLength, "Amount of list items to load (default: all)") + // listCmd.Flags().IntVarP(&mal.SearchOffset, "list-offset", "o", listOffset, "Offset for the list") } diff --git a/cmd/login.go b/cmd/login.go index 71e4b5a..7127c47 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -19,8 +19,8 @@ along with this program. If not, see . package cmd import ( - "fmt" "os" + "fmt" "github.com/spf13/cobra" "github.com/MikunoNaka/macli/auth" ) @@ -61,8 +61,8 @@ var loginCmd = &cobra.Command { } func init() { - rootCmd.AddCommand(loginCmd) + rootCmd.AddCommand(loginCmd) loginCmd.Flags().StringP("authentication-token", "t", "", "MyAnimeList authentication token to use (overrides system keyring if any)") loginCmd.Flags().StringP("client-id", "c", "", "MyAnimeList Client ID") - loginCmd.Flags().StringP("store-client-id", "s", "yes", "Save Client ID to keyring (yes/no) (Default: yes)") + loginCmd.Flags().StringP("store-client-id", "s", saveClientId, "Save Client ID to keyring (yes/no) (Default: yes)") } diff --git a/cmd/root.go b/cmd/root.go index 1b956cf..02d27a1 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -29,22 +29,24 @@ import ( var ( queryOnlyMode, mangaMode bool entryId int - authConfig, defConfig map[string]interface{} - // config vars - /* TODO: load config vars here - * then set config file's values or default values - * conditionally after loading the config file - */ + // auth + saveClientId string = "yes" + // searching + promptLength, searchLength, searchOffset int = 5, 10, 0 + searchNsfw bool = false + // lists + listOffset, listLength int = 0, 15 + listIncludeNsfw bool = false ) var rootCmd = &cobra.Command{ - Use: "macli", + Use: "macli", Short: "macli - Unofficial CLI-Based MyAnimeList Client.", Long: "macli is an unofficial MyAnimeList Client for use inside the terminal.", } -func init() { +func Execute() { viper.SetConfigName("macli") viper.SetConfigType("yaml") viper.AddConfigPath(".") @@ -62,12 +64,6 @@ func init() { } } - // load config file contents - authConfig = viper.Get("auth").(map[string]interface{}) - defConfig = viper.Get("defaults").(map[string]interface{}) -} - -func Execute() { err := rootCmd.Execute() if err != nil { os.Exit(1) diff --git a/cmd/score.go b/cmd/score.go index 6a2df9c..ec26252 100644 --- a/cmd/score.go +++ b/cmd/score.go @@ -159,10 +159,10 @@ func setMangaScore(scoreInput, searchInput string) { func init() { rootCmd.AddCommand(scoreCmd) scoreCmd.Flags().StringP("set-value", "s", "", "Score to be set") - scoreCmd.Flags().IntVarP(&ui.PromptLength, "prompt-length", "l", 5, "Length of select prompt") - scoreCmd.Flags().IntVarP(&mal.SearchLength, "search-length", "n", 10, "Amount of search results to load") - scoreCmd.Flags().IntVarP(&mal.SearchOffset, "search-offset", "o", 0, "Offset for the search results") - scoreCmd.Flags().BoolVarP(&mal.SearchNSFW, "search-nsfw", "", false, "Include NSFW-rated items in search results") + scoreCmd.Flags().IntVarP(&ui.PromptLength, "prompt-length", "l", promptLength, "Length of select prompt") + scoreCmd.Flags().IntVarP(&mal.SearchLength, "search-length", "n", searchLength, "Amount of search results to load") + scoreCmd.Flags().IntVarP(&mal.SearchOffset, "search-offset", "o", searchOffset, "Offset for the search results") + scoreCmd.Flags().BoolVarP(&mal.SearchNSFW, "search-nsfw", "", searchNsfw, "Include NSFW-rated items in search results") scoreCmd.Flags().BoolVarP(&mangaMode, "manga", "m", false, "Use manga mode") scoreCmd.Flags().BoolVarP(&queryOnlyMode, "query", "q", false, "Query only (don't update data)") scoreCmd.Flags().IntVarP(&entryId, "id", "i", -1, "Manually specify the ID of anime/manga (overrides search)") diff --git a/cmd/search.go b/cmd/search.go index b6aaf3e..76babf4 100644 --- a/cmd/search.go +++ b/cmd/search.go @@ -113,12 +113,13 @@ func searchAnime(searchInput string) { } func init() { + fmt.Println("searchInit") rootCmd.AddCommand(searchCmd) - searchCmd.Flags().IntVarP(&ui.PromptLength, "prompt-length", "l", 5, "Length of select prompt") + searchCmd.Flags().IntVarP(&ui.PromptLength, "prompt-length", "l", promptLength, "Length of select prompt") searchCmd.Flags().BoolVarP(&mangaMode, "manga", "m", false, "Use manga mode") - searchCmd.Flags().IntVarP(&mal.SearchLength, "search-length", "n", 10, "Amount of search results to load") - searchCmd.Flags().IntVarP(&mal.SearchOffset, "search-offset", "o", 0, "Offset for the search results") - searchCmd.Flags().BoolVarP(&mal.SearchNSFW, "search-nsfw", "", false, "Include NSFW-rated items in search results") + searchCmd.Flags().IntVarP(&mal.SearchLength, "search-length", "n", searchLength, "Amount of search results to load") + searchCmd.Flags().IntVarP(&mal.SearchOffset, "search-offset", "o", searchOffset, "Offset for the search results") + searchCmd.Flags().BoolVarP(&mal.SearchNSFW, "search-nsfw", "", searchNsfw, "Include NSFW-rated items in search results") searchCmd.Flags().IntVarP(&entryId, "id", "i", -1, "Manually specify the ID of anime/manga (overrides search)") searchCmd.Flags().BoolVarP(&queryOnlyMode, "query", "q", false, "Query only (don't update data)") searchCmd.Flags().StringVarP(&mal.Secret, "authentication-token", "t", "", "MyAnimeList authentication token to use (overrides system keyring if any)") diff --git a/cmd/seasonals.go b/cmd/seasonals.go index 7fd4cde..5efe544 100644 --- a/cmd/seasonals.go +++ b/cmd/seasonals.go @@ -22,7 +22,7 @@ import ( // "os" "fmt" // "strings" - "github.com/MikunoNaka/macli/ui" + // "github.com/MikunoNaka/macli/ui" "github.com/MikunoNaka/macli/util" "github.com/MikunoNaka/macli/mal" @@ -58,8 +58,6 @@ var seasonalsCmd = &cobra.Command{ func init() { rootCmd.AddCommand(seasonalsCmd) - seasonalsCmd.Flags().IntVarP(&ui.PromptLength, "prompt-length", "l", 5, "Length of select prompt") - seasonalsCmd.Flags().BoolVarP(&queryOnlyMode, "query", "q", false, "Query only (don't update data)") seasonalsCmd.Flags().IntVarP(&mal.SearchLength, "results-length", "n", 10, "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") diff --git a/cmd/status.go b/cmd/status.go index 2dc6fee..53e93c6 100644 --- a/cmd/status.go +++ b/cmd/status.go @@ -133,6 +133,7 @@ func setMangaStatus(statusInput, searchInput string) { } func init() { + fmt.Println("statusINit") rootCmd.AddCommand(statusCmd) statusCmd.Flags().StringP("set-value", "s", "", "status to be set") statusCmd.Flags().IntVarP(&ui.PromptLength, "prompt-length", "l", 5, "Length of select prompt") diff --git a/macli.yaml b/macli.yaml index dd5cc79..ac4f999 100644 --- a/macli.yaml +++ b/macli.yaml @@ -5,15 +5,19 @@ # https://vidhukant.xyz/docs/macli # https://github.com/MikunoNaka/macli # +# macli v1.12 onwards support reading this file but +# currently this file may cause unexpected behaviour +# so it is not currently recommended to use it # AUTHENTICAITON INFO # macli defaults to the system keyring on windows or mac, -# or gnome-keyring on linux to store the login information +# or gnome-keyring (optional) on linux to store the login information # if none are available it can be set here # but it is not recommended unless it's the only option # # if you're using termux, this is probably the only option auth: + # TODO: add option to ignore system keyring # ~~can leave blank, macli will set this~~ TODO: make macli set this optionally token: "" # run `macli help` for instructions for creating one @@ -22,9 +26,14 @@ auth: save_client_id: "yes" # default settings (can be overridden by corresponding flags, `macli --help` for more details) -defaults: +searching: prompt_length: 5 # length of anime/manga selection prompt when searching (default: 5) search_length: 10 # amount of search results to get (default: 10) - search_offset: 0 # offset for the search results (default is 0, changing it is not recommended) + search_offset: 0 # offset for the search results (default is 0, changing it is here not recommended as it may cause confusion) search_nsfw: false # include NSFW rated search results (true/false) +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 + include_nsfw_results: false # show NSFW rated items diff --git a/mal/mal.go b/mal/mal.go index 77940df..670be07 100644 --- a/mal/mal.go +++ b/mal/mal.go @@ -20,6 +20,7 @@ package mal import ( "github.com/MikunoNaka/macli/auth" + "github.com/spf13/viper" a "github.com/MikunoNaka/MAL2Go/v4/anime" m "github.com/MikunoNaka/MAL2Go/v4/manga" u "github.com/MikunoNaka/MAL2Go/v4/user" @@ -46,6 +47,34 @@ func Init() { } tk := "Bearer " + Secret + /* NOTE: currently, macli is checking wether the specified + * search length, etc is the default value (5) or not. if it is not + * then it wont do anything. if it is, then if a config file + * exists the value in the config file will be used + * this works but flags won't be able to take precedence + * + * i.e if the value in config file is 6 but I want to set it to 5 through + * flags, it will see that the value is the default value so it'll use + * the value in the macli.yaml file which is 6. in this case the + * flags aren't taking precedence. fix that! */ + // load config file vars (if any) + confSearchLength := viper.Get("searching.search_length") + confSearchOffset := viper.Get("searching.search_offset") + confSearchNsfw := viper.Get("searching.search_nsfw") + + // if SearchLength is the default value just use the one in config file if any + if confSearchLength != nil && SearchLength == 10 { + SearchLength = confSearchLength.(int) + } + // if SearchOffset is the default value just use the one in config file if any + if confSearchOffset != nil && SearchOffset == 0 { + SearchOffset = confSearchOffset.(int) + } + // if SearchNsfw is the default value just use the one in config file if any + if confSearchNsfw != nil && SearchNSFW == false { + SearchNSFW = confSearchNsfw.(bool) + } + // initialise MAL2Go Client(s) animeClient.AuthToken = tk mangaClient.AuthToken = tk diff --git a/ui/ui.go b/ui/ui.go index a54818e..96cc3b4 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -18,6 +18,28 @@ along with this program. If not, see . package ui -var ( - PromptLength, SynopsisLength int +import ( + "github.com/spf13/viper" ) + +var PromptLength int + +/* NOTE: currently, macli is checking wether the specified + * prompt length is the default value (5) or not. if it is not + * then it wont do anything. if it is, then if a config file + * exists the value in the config file will be used + * this works but flags won't be able to take precedence + * + * i.e if the value in config file is 6 but I want to set it to 5 through + * flags, it will see that the value is the default value so it'll use + * the value in the macli.yaml file which is 6. in this case the + * flags aren't taking precedence. fix that! */ +func init() { + // read prompt length from config file + confPromptLength := viper.Get("searching.prompt_length") + + // if PromptLength is the default value just use the one in config file if any + if confPromptLength != nil && PromptLength == 5 { + PromptLength = confPromptLength.(int) + } +} -- cgit v1.2.3