From f2ebd7d68cfbd82d1241d1fe04b2e056892b5315 Mon Sep 17 00:00:00 2001 From: chaoqun <27287694+OpenWaygate@users.noreply.github.com> Date: Sat, 11 May 2024 00:20:51 +0800 Subject: [PATCH] :sparkles: Support hl flag when list i18n staff --- cmd/i18nLanguage/i18nLanguage.go | 1 + cmd/i18nLanguage/list.go | 3 ++- cmd/i18nRegion/i18nRegion.go | 1 + cmd/i18nRegion/list.go | 3 ++- cmd/version.go | 10 +++++----- pkg/auth/auth.go | 20 ++++++++++---------- pkg/yutuber/i18nLanguage.go | 14 +++++++++++++- pkg/yutuber/i18nRegion.go | 14 +++++++++++++- 8 files changed, 47 insertions(+), 19 deletions(-) diff --git a/cmd/i18nLanguage/i18nLanguage.go b/cmd/i18nLanguage/i18nLanguage.go index f4c47ca..74b76de 100644 --- a/cmd/i18nLanguage/i18nLanguage.go +++ b/cmd/i18nLanguage/i18nLanguage.go @@ -6,6 +6,7 @@ import ( ) var ( + hl string parts []string output string ) diff --git a/cmd/i18nLanguage/list.go b/cmd/i18nLanguage/list.go index 623f5a9..54dae45 100644 --- a/cmd/i18nLanguage/list.go +++ b/cmd/i18nLanguage/list.go @@ -10,13 +10,14 @@ var listCmd = &cobra.Command{ Short: "list i18nLanguages", Long: "list i18nLanguages' id, hl, and name", Run: func(cmd *cobra.Command, args []string) { - i := yutuber.NewI18nLanguage() + i := yutuber.NewI18nLanguage(yutuber.WithI18nLanguageHl(hl)) i.List(parts, output) }, } func init() { i18nLanguageCmd.AddCommand(listCmd) + listCmd.Flags().StringVarP(&hl, "hl", "l", "", "host language") listCmd.Flags().StringSliceVarP( &parts, "parts", "p", []string{"id", "snippet"}, "Comma separated parts", ) diff --git a/cmd/i18nRegion/i18nRegion.go b/cmd/i18nRegion/i18nRegion.go index d66f1c9..c9e2ef3 100644 --- a/cmd/i18nRegion/i18nRegion.go +++ b/cmd/i18nRegion/i18nRegion.go @@ -6,6 +6,7 @@ import ( ) var ( + hl string parts []string output string ) diff --git a/cmd/i18nRegion/list.go b/cmd/i18nRegion/list.go index 0ef5f67..c4378f9 100644 --- a/cmd/i18nRegion/list.go +++ b/cmd/i18nRegion/list.go @@ -10,13 +10,14 @@ var listCmd = &cobra.Command{ Short: "list i18nRegions", Long: "list i18nRegions' id, hl, and name", Run: func(cmd *cobra.Command, args []string) { - i := yutuber.NewI18nRegion() + i := yutuber.NewI18nRegion(yutuber.WithI18nRegionHl(hl)) i.List(parts, output) }, } func init() { i18nRegionCmd.AddCommand(listCmd) + listCmd.Flags().StringVarP(&hl, "hl", "l", "", "host language") listCmd.Flags().StringSliceVarP( &parts, "parts", "p", []string{"id", "snippet"}, "Comma separated parts", ) diff --git a/cmd/version.go b/cmd/version.go index 78ce17a..4d1b659 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -3,11 +3,11 @@ package cmd import "github.com/spf13/cobra" var ( - Version string = "" - Commit string = "" - CommitDate string = "" - Os string = "" - Arch string = "" + Version = "" + Commit = "" + CommitDate = "" + Os = "" + Arch = "" ) var versionCmd = &cobra.Command{ diff --git a/pkg/auth/auth.go b/pkg/auth/auth.go index 58be4f5..b7b8e87 100644 --- a/pkg/auth/auth.go +++ b/pkg/auth/auth.go @@ -20,16 +20,16 @@ import ( ) var ( - credential string = "client_secret.json" - cacheFile string = "youtube.token.json" - errGetUser error = errors.New("unable to get current user") - errCreateSvc error = errors.New("unable to create YouTube service") - errReadPrompt error = errors.New("unable to read prompt") - errExchange error = errors.New("unable retrieve token from web or prompt") - errStartWeb error = errors.New("unable to start web server") - errCacheToken error = errors.New("unable to cache token") - errReadSecret error = errors.New("unable to read client secret file") - errParseSecret error = errors.New("unable to parse client secret to config") + credential = "client_secret.json" + cacheFile = "youtube.token.json" + errGetUser = errors.New("unable to get current user") + errCreateSvc = errors.New("unable to create YouTube service") + errReadPrompt = errors.New("unable to read prompt") + errExchange = errors.New("unable retrieve token from web or prompt") + errStartWeb = errors.New("unable to start web server") + errCacheToken = errors.New("unable to cache token") + errReadSecret = errors.New("unable to read client secret file") + errParseSecret = errors.New("unable to parse client secret to config") ) const missingClientSecretsMessage string = ` diff --git a/pkg/yutuber/i18nLanguage.go b/pkg/yutuber/i18nLanguage.go index 66375ec..40ba36c 100644 --- a/pkg/yutuber/i18nLanguage.go +++ b/pkg/yutuber/i18nLanguage.go @@ -13,7 +13,9 @@ var ( errGetI18nLanguage = errors.New("failed to get i18n language") ) -type i18nLanguage struct{} +type i18nLanguage struct { + hl string +} type I18nLanguage interface { get(parts []string) []*youtube.I18nLanguage @@ -35,6 +37,10 @@ func NewI18nLanguage(opts ...I18nLanguageOption) I18nLanguage { func (i *i18nLanguage) get(parts []string) []*youtube.I18nLanguage { call := service.I18nLanguages.List(parts) + if i.hl != "" { + call = call.Hl(i.hl) + } + response, err := call.Do() if err != nil { log.Fatalln(errors.Join(errGetI18nLanguage, err)) @@ -60,3 +66,9 @@ func (i *i18nLanguage) List(parts []string, output string) { } } } + +func WithI18nLanguageHl(hl string) I18nLanguageOption { + return func(i *i18nLanguage) { + i.hl = hl + } +} diff --git a/pkg/yutuber/i18nRegion.go b/pkg/yutuber/i18nRegion.go index b49d88a..7532b09 100644 --- a/pkg/yutuber/i18nRegion.go +++ b/pkg/yutuber/i18nRegion.go @@ -13,7 +13,9 @@ var ( errGetI18nRegion = errors.New("failed to get i18n region") ) -type i18nRegion struct{} +type i18nRegion struct { + hl string +} type I18nRegion interface { get(parts []string) []*youtube.I18nRegion @@ -35,6 +37,10 @@ func NewI18nRegion(opts ...I18nRegionOption) I18nRegion { func (i *i18nRegion) get(parts []string) []*youtube.I18nRegion { call := service.I18nRegions.List(parts) + if i.hl != "" { + call = call.Hl(i.hl) + } + response, err := call.Do() if err != nil { log.Fatalln(errors.Join(errGetI18nRegion, err)) @@ -60,3 +66,9 @@ func (i *i18nRegion) List(parts []string, output string) { } } } + +func WithI18nRegionHl(hl string) I18nRegionOption { + return func(i *i18nRegion) { + i.hl = hl + } +}