diff options
-rw-r--r-- | cmd/chapters.go | 25 | ||||
-rw-r--r-- | cmd/episodes.go | 25 | ||||
-rw-r--r-- | cmd/list.go | 12 | ||||
-rw-r--r-- | cmd/login.go | 2 | ||||
-rw-r--r-- | cmd/root.go | 41 | ||||
-rw-r--r-- | cmd/score.go | 19 | ||||
-rw-r--r-- | cmd/search.go | 19 | ||||
-rw-r--r-- | cmd/seasonals.go | 7 | ||||
-rw-r--r-- | cmd/status.go | 20 | ||||
-rw-r--r-- | cmd/volumes.go | 25 | ||||
-rw-r--r-- | mal/mal.go | 36 | ||||
-rw-r--r-- | ui/ui.go | 18 | ||||
-rw-r--r-- | util/bind_config.go | 73 |
13 files changed, 215 insertions, 107 deletions
diff --git a/cmd/chapters.go b/cmd/chapters.go index 4e5be08..f8c3414 100644 --- a/cmd/chapters.go +++ b/cmd/chapters.go @@ -23,6 +23,7 @@ import ( "fmt" "strings" "github.com/MikunoNaka/macli/ui" + "github.com/MikunoNaka/macli/util" "github.com/MikunoNaka/macli/mal" m "github.com/MikunoNaka/MAL2Go/v4/manga" @@ -41,7 +42,17 @@ var chaptersCmd = &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.BindSearchConfig(cmd.Flags()) + if err != nil { + fmt.Println("Error while parsing flags.", err.Error()) + os.Exit(1) + } + mal.SearchLength = conf.SearchLength + mal.SearchOffset = conf.SearchOffset + mal.SearchNSFW = conf.SearchNSFW + ui.PromptLength = conf.PromptLength mal.Init() + var selectedManga m.Manga if entryId > 0 { selectedManga = mal.GetMangaData(entryId, []string{"my_list_status", "num_chapters"}) @@ -58,10 +69,7 @@ var chaptersCmd = &cobra.Command{ searchInput = ui.TextInput(promptText, "Search can't be blank.") } - var ( - chInput string - err error - ) + var chInput string if !queryOnlyMode { chInput, err = cmd.Flags().GetString("set-value") if err != nil { @@ -92,11 +100,12 @@ 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", 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)") + + chaptersCmd.Flags().IntP("prompt-length", "l", 5, "Length of select prompt") + chaptersCmd.Flags().IntP("search-length", "n", 10, "Amount of search results to load") + chaptersCmd.Flags().IntP("search-offset", "o", 0, "Offset for the search results") + chaptersCmd.Flags().BoolP("search-nsfw", "", false, "Include NSFW-rated items in search results") } diff --git a/cmd/episodes.go b/cmd/episodes.go index 0423679..fce4b2a 100644 --- a/cmd/episodes.go +++ b/cmd/episodes.go @@ -23,6 +23,7 @@ import ( "fmt" "strings" "github.com/MikunoNaka/macli/ui" + "github.com/MikunoNaka/macli/util" "github.com/MikunoNaka/macli/mal" a "github.com/MikunoNaka/MAL2Go/v4/anime" @@ -41,7 +42,17 @@ var episodesCmd = &cobra.Command{ " - \x1b[33m`macli episodes -s +1 <anime-name>`\x1b[0m to increment the episodes by 1\n" + " - \x1b[33m`macli episodes -s -2 <anime-name>`\x1b[0m to decrement the episodes by 2\n", Run: func(cmd *cobra.Command, args []string) { + conf, err := util.BindSearchConfig(cmd.Flags()) + if err != nil { + fmt.Println("Error while parsing flags.", err.Error()) + os.Exit(1) + } + mal.SearchLength = conf.SearchLength + mal.SearchOffset = conf.SearchOffset + mal.SearchNSFW = conf.SearchNSFW + ui.PromptLength = conf.PromptLength mal.Init() + var selectedAnime a.Anime if entryId > 0 { selectedAnime = mal.GetAnimeData(entryId, []string{"my_list_status", "num_episodes"}) @@ -58,10 +69,7 @@ var episodesCmd = &cobra.Command{ searchInput = ui.TextInput(promptText, "Search can't be blank.") } - var ( - epInput string - err error - ) + var epInput string if !queryOnlyMode { epInput, err = cmd.Flags().GetString("set-value") if err != nil { @@ -92,11 +100,12 @@ 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", 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", 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)") + + episodesCmd.Flags().IntP("prompt-length", "l", 5, "Length of select prompt") + episodesCmd.Flags().IntP("search-length", "n", 10, "Amount of search results to load") + episodesCmd.Flags().IntP("search-offset", "o", 0, "Offset for the search results") + episodesCmd.Flags().BoolP("search-nsfw", "", false, "Include NSFW-rated items in search results") } diff --git a/cmd/list.go b/cmd/list.go index e6f7f8c..e4dbcf1 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -26,6 +26,7 @@ import ( // m "github.com/MikunoNaka/MAL2Go/v4/manga" "github.com/spf13/cobra" + "github.com/spf13/viper" ) // statusCmd represents the status command @@ -80,10 +81,13 @@ 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", "", listIncludeNsfw, "Include NSFW results") + 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)") - // 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") + 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")) } diff --git a/cmd/login.go b/cmd/login.go index 7127c47..39bc7d4 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -64,5 +64,5 @@ func init() { 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", saveClientId, "Save Client ID to keyring (yes/no) (Default: yes)") + loginCmd.Flags().StringP("store-client-id", "s", "yes", "Save Client ID to keyring") } diff --git a/cmd/root.go b/cmd/root.go index 02d27a1..7b379a3 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -30,14 +30,14 @@ var ( queryOnlyMode, mangaMode bool entryId int - // auth - saveClientId string = "yes" - // searching - promptLength, searchLength, searchOffset int = 5, 10, 0 - searchNsfw bool = false - // lists - listOffset, listLength int = 0, 15 - listIncludeNsfw bool = false + // // 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{ @@ -46,12 +46,16 @@ var rootCmd = &cobra.Command{ Long: "macli is an unofficial MyAnimeList Client for use inside the terminal.", } -func Execute() { +func init() { + cobra.OnInitialize(initConfig) +} + +func initConfig() { viper.SetConfigName("macli") - viper.SetConfigType("yaml") + //viper.SetConfigType("yaml") viper.AddConfigPath(".") - viper.AddConfigPath("$HOME/.config/macli") - viper.AddConfigPath("/etc/macli") + // viper.AddConfigPath("$HOME/.config/macli") + // viper.AddConfigPath("/etc/macli") // dont show error if file not found // macli doesnt need a config file to work properly @@ -64,6 +68,19 @@ func Execute() { } } + viper.SetDefault("searching.prompt_length", 5) + viper.SetDefault("searching.search_length", 10) + viper.SetDefault("searching.search_offset", 0) + viper.SetDefault("searching.search_nsfw", false) + + viper.SetDefault("lists.list_offset", 0) + viper.SetDefault("lists.list_length", 15) + viper.SetDefault("lists.include_nsfw_results", false) + + viper.SetDefault("auth.save_client_id", "yes") +} + +func Execute() { err := rootCmd.Execute() if err != nil { os.Exit(1) diff --git a/cmd/score.go b/cmd/score.go index ec26252..a7a5aaa 100644 --- a/cmd/score.go +++ b/cmd/score.go @@ -43,7 +43,17 @@ var scoreCmd = &cobra.Command{ " - \x1b[33m`macli status <anime-name>`\x1b[0m For interactive prompt (anime-name can be omitted)\n" + " - \x1b[33m`macli status -s \x1b[34mwatching|plan_to_watch|dropped|on_hold|completed\x1b[33m <anime-name>`\x1b[0m to specify status from command\n", Run: func(cmd *cobra.Command, args []string) { + conf, err := util.BindSearchConfig(cmd.Flags()) + if err != nil { + fmt.Println("Error while parsing flags.", err.Error()) + os.Exit(1) + } + mal.SearchLength = conf.SearchLength + mal.SearchOffset = conf.SearchOffset + mal.SearchNSFW = conf.SearchNSFW + ui.PromptLength = conf.PromptLength mal.Init() + searchInput := strings.Join(args, " ") scoreInput, err := cmd.Flags().GetString("set-value") @@ -159,12 +169,13 @@ 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", 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)") scoreCmd.Flags().StringVarP(&mal.Secret, "authentication-token", "t", "", "MyAnimeList authentication token to use (overrides system keyring if any)") + + scoreCmd.Flags().IntP("prompt-length", "l", 5, "Length of select prompt") + scoreCmd.Flags().IntP("search-length", "n", 10, "Amount of search results to load") + scoreCmd.Flags().IntP("search-offset", "o", 0, "Offset for the search results") + scoreCmd.Flags().BoolP("search-nsfw", "", false, "Include NSFW-rated items in search results") } diff --git a/cmd/search.go b/cmd/search.go index 57f1980..0961ffe 100644 --- a/cmd/search.go +++ b/cmd/search.go @@ -41,7 +41,17 @@ var searchCmd = &cobra.Command { "\t\x1b[33m`macli search -m <manga-name>`\x1b[0m searches for a manga\n" + "\t\x1b[33m`macli search`\x1b[0m interactively asks for an anime to search for (same for manga with -m/--manga flag)\n", Run: func(cmd *cobra.Command, args []string) { + conf, err := util.BindSearchConfig(cmd.Flags()) + if err != nil { + fmt.Println("Error while parsing flags.", err.Error()) + os.Exit(1) + } + mal.SearchLength = conf.SearchLength + mal.SearchOffset = conf.SearchOffset + mal.SearchNSFW = conf.SearchNSFW + ui.PromptLength = conf.PromptLength mal.Init() + // read searchInput from command searchInput := strings.Join(args, " ") mangaMode, err := cmd.Flags().GetBool("manga") @@ -114,12 +124,13 @@ func searchAnime(searchInput string) { func init() { rootCmd.AddCommand(searchCmd) - 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", 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)") + + searchCmd.Flags().IntP("prompt-length", "l", 5, "Length of select prompt") + searchCmd.Flags().IntP("search-length", "n", 10, "Amount of search results to load") + searchCmd.Flags().IntP("search-offset", "o", 0, "Offset for the search results") + searchCmd.Flags().BoolP("search-nsfw", "", false, "Include NSFW-rated items in search results") } diff --git a/cmd/seasonals.go b/cmd/seasonals.go index 5efe544..b78876a 100644 --- a/cmd/seasonals.go +++ b/cmd/seasonals.go @@ -27,6 +27,7 @@ import ( "github.com/MikunoNaka/macli/mal" "github.com/spf13/cobra" + "github.com/spf13/viper" ) // statusCmd represents the status command @@ -58,11 +59,15 @@ var seasonalsCmd = &cobra.Command{ func init() { rootCmd.AddCommand(seasonalsCmd) - seasonalsCmd.Flags().IntVarP(&mal.SearchLength, "results-length", "n", 10, "Amount of results to load") + 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")) } diff --git a/cmd/status.go b/cmd/status.go index 2dc6fee..7c641e6 100644 --- a/cmd/status.go +++ b/cmd/status.go @@ -23,6 +23,7 @@ import ( "os" "strings" "github.com/MikunoNaka/macli/ui" + "github.com/MikunoNaka/macli/util" "github.com/MikunoNaka/macli/mal" a "github.com/MikunoNaka/MAL2Go/v4/anime" m "github.com/MikunoNaka/MAL2Go/v4/manga" @@ -40,7 +41,17 @@ var statusCmd = &cobra.Command{ " - \x1b[33m`macli status <anime-name>`\x1b[0m For interactive prompt (anime-name can be omitted)\n" + " - \x1b[33m`macli status -s \x1b[34mwatching|plan_to_watch|dropped|on_hold|completed\x1b[33m <anime-name>`\x1b[0m to specify status from command\n", Run: func(cmd *cobra.Command, args []string) { + conf, err := util.BindSearchConfig(cmd.Flags()) + if err != nil { + fmt.Println("Error while parsing flags.", err.Error()) + os.Exit(1) + } + mal.SearchLength = conf.SearchLength + mal.SearchOffset = conf.SearchOffset + mal.SearchNSFW = conf.SearchNSFW + ui.PromptLength = conf.PromptLength mal.Init() + searchInput := strings.Join(args, " ") statusInput, err := cmd.Flags().GetString("set-value") @@ -135,12 +146,13 @@ func setMangaStatus(statusInput, searchInput string) { func init() { 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") - statusCmd.Flags().IntVarP(&mal.SearchLength, "search-length", "n", 10, "Amount of search results to load") - statusCmd.Flags().IntVarP(&mal.SearchOffset, "search-offset", "o", 0, "Offset for the search results") - statusCmd.Flags().BoolVarP(&mal.SearchNSFW, "search-nsfw", "", false, "Include NSFW-rated items in search results") statusCmd.Flags().BoolVarP(&mangaMode, "manga", "m", false, "Use manga mode") statusCmd.Flags().BoolVarP(&queryOnlyMode, "query", "q", false, "Query only (don't update data)") statusCmd.Flags().IntVarP(&entryId, "id", "i", -1, "Manually specify the ID of anime/manga (overrides search)") statusCmd.Flags().StringVarP(&mal.Secret, "authentication-token", "t", "", "MyAnimeList authentication token to use (overrides system keyring if any)") + + statusCmd.Flags().IntP("prompt-length", "l", 5, "Length of select prompt") + statusCmd.Flags().IntP("search-length", "n", 10, "Amount of search results to load") + statusCmd.Flags().IntP("search-offset", "o", 0, "Offset for the search results") + statusCmd.Flags().BoolP("search-nsfw", "", false, "Include NSFW-rated items in search results") } diff --git a/cmd/volumes.go b/cmd/volumes.go index 92b588b..6df1b69 100644 --- a/cmd/volumes.go +++ b/cmd/volumes.go @@ -23,6 +23,7 @@ import ( "fmt" "strings" "github.com/MikunoNaka/macli/ui" + "github.com/MikunoNaka/macli/util" "github.com/MikunoNaka/macli/mal" m "github.com/MikunoNaka/MAL2Go/v4/manga" @@ -41,7 +42,17 @@ var volumesCmd = &cobra.Command{ " - \x1b[33m`macli volumes -s +1 <manga-name>`\x1b[0m to increment the volumes by 1\n" + " - \x1b[33m`macli volumes -s -2 <manga-name>`\x1b[0m to decrement the volumes by 2\n", Run: func(cmd *cobra.Command, args []string) { + conf, err := util.BindSearchConfig(cmd.Flags()) + if err != nil { + fmt.Println("Error while parsing flags.", err.Error()) + os.Exit(1) + } + mal.SearchLength = conf.SearchLength + mal.SearchOffset = conf.SearchOffset + mal.SearchNSFW = conf.SearchNSFW + ui.PromptLength = conf.PromptLength mal.Init() + var selectedManga m.Manga if entryId > 0 { selectedManga = mal.GetMangaData(entryId, []string{"my_list_status", "num_volumes"}) @@ -58,10 +69,7 @@ var volumesCmd = &cobra.Command{ searchInput = ui.TextInput(promptText, "Search can't be blank.") } - var ( - volInput string - err error - ) + var volInput string if !queryOnlyMode { volInput, err = cmd.Flags().GetString("set-value") if err != nil { @@ -92,11 +100,12 @@ var volumesCmd = &cobra.Command{ func init() { rootCmd.AddCommand(volumesCmd) volumesCmd.Flags().StringP("set-value", "s", "", "Number of voulmes") - volumesCmd.Flags().IntVarP(&ui.PromptLength, "prompt-length", "l", 5, "Length of select prompt") - volumesCmd.Flags().IntVarP(&mal.SearchLength, "search-length", "n", 10, "Amount of search results to load") - volumesCmd.Flags().IntVarP(&mal.SearchOffset, "search-offset", "o", 0, "Offset for the search results") - volumesCmd.Flags().BoolVarP(&mal.SearchNSFW, "search-nsfw", "", false, "Include NSFW-rated items in search results") volumesCmd.Flags().BoolVarP(&queryOnlyMode, "query", "q", false, "Query only (don't update data)") volumesCmd.Flags().IntVarP(&entryId, "id", "i", -1, "Manually specify the ID of manga (overrides search)") volumesCmd.Flags().StringVarP(&mal.Secret, "authentication-token", "t", "", "MyAnimeList authentication token to use (overrides system keyring if any)") + + volumesCmd.Flags().IntP("prompt-length", "l", 5, "Length of select prompt") + volumesCmd.Flags().IntP("search-length", "n", 10, "Amount of search results to load") + volumesCmd.Flags().IntP("search-offset", "o", 0, "Offset for the search results") + volumesCmd.Flags().BoolP("search-nsfw", "", false, "Include NSFW-rated items in search results") } @@ -20,7 +20,6 @@ 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" @@ -47,41 +46,6 @@ 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") - confSecret := viper.Get("auth.token") - - // 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) - } - - /* the secret stored in the config file - * takes precedence on the system keyring */ - if confSecret != nil && confSecret != "" { - Secret = confSecret.(string) - } - // initialise MAL2Go Client(s) animeClient.AuthToken = tk mangaClient.AuthToken = tk @@ -24,22 +24,6 @@ import ( 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) - } + PromptLength = viper.GetInt("searching.prompt_length") } diff --git a/util/bind_config.go b/util/bind_config.go new file mode 100644 index 0000000..f849398 --- /dev/null +++ b/util/bind_config.go @@ -0,0 +1,73 @@ +/* +macli - Unofficial CLI-Based MyAnimeList Client +Copyright © 2022 Vidhu Kant Sharma <vidhukant@vidhukant.xyz> + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +/* viper.BindPFlag won't work if + * multiple commands have the same + * flags. so this is my hacky + * way to do that stuff myself + */ + +package util + +import ( + "github.com/spf13/viper" + "github.com/spf13/pflag" +) + +type SearchConfig struct { + PromptLength int + SearchLength int + SearchOffset int + SearchNSFW bool +} + +// handles prompt-length, search-length, search-offset and search-nsfw +func BindSearchConfig(flags *pflag.FlagSet) (SearchConfig, error) { + var conf SearchConfig + var err error + + if flags.Lookup("prompt-length").Changed { + conf.PromptLength, err = flags.GetInt("prompt-length") + if err != nil {return conf, err} + } else { + conf.PromptLength = viper.GetInt("searching.prompt_length") + } + + if flags.Lookup("search-length").Changed { + conf.SearchLength, err = flags.GetInt("search-length") + if err != nil {return conf, err} + } else { + conf.SearchLength = viper.GetInt("searching.search_length") + } + + if flags.Lookup("search-offset").Changed { + conf.SearchOffset, err = flags.GetInt("search-offset") + if err != nil {return conf, err} + } else { + conf.SearchOffset = viper.GetInt("searching.search_offset") + } + + if flags.Lookup("search-nsfw").Changed { + conf.SearchNSFW, err = flags.GetBool("search-nsfw") + if err != nil {return conf, err} + } else { + conf.SearchNSFW = viper.GetBool("searching.search_nsfw") + } + + return conf, nil +} |