🐛 Replace mutually exclusive bool flags with single --for enum in channel

related issue #215
This commit is contained in:
OpenWaygate committed 2026-06-27 18:36:26 +08:00
1 parent 9cf757851c
commit 8ea872ed84
4 files changed
+29 -101

No files matched your search

+2 -21
View File
@@ -5,9 +5,6 @@ package channel
import (
"github.com/eat-pray-ai/yutu/cmd"
"github.com/eat-pray-ai/yutu/pkg/utils"
"github.com/spf13/pflag"
"github.com/spf13/cobra"
)
@@ -18,9 +15,7 @@ const (
fhUsage = "Return the channel associated with a YouTube handle"
fuUsage = "Return the channel associated with a YouTube username"
hlUsage = "Specifies the localization language of the metadata"
mbmUsage = "Return the channels managed by the authenticated user"
mineUsage = "Return the ids of channels owned by the authenticated user"
msUsage = "Return the channels subscribed to the authenticated user"
forUsage = "managedByMe|mine|mySubscribers"
countryUsage = "Country of the channel"
curlUsage = "Custom URL of the channel"
dlUsage = "The language of the channel's default title and description"
@@ -34,10 +29,8 @@ var (
forUsername string
hl string
ids []string
managedByMe = new(false)
channelFor string
maxResults int64
mine = new(false)
mySubscribers = new(false)
country string
customUrl string
defaultLanguage string
@@ -52,9 +45,6 @@ var channelCmd = &cobra.Command{
Use: "channel",
Short: short,
Long: long,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
resetFlags(cmd.Flags())
},
Run: func(cmd *cobra.Command, args []string) {
_ = cmd.Help()
},
@@ -64,12 +54,3 @@ func init() {
cmd.RootCmd.AddCommand(channelCmd)
}
func resetFlags(flagSet *pflag.FlagSet) {
boolMap := map[string]**bool{
"managedByMe": &managedByMe,
"mine": &mine,
"mySubscribers": &mySubscribers,
}
utils.ResetBool(boolMap, flagSet)
}
+6 -13
View File
@@ -45,14 +45,15 @@ var listInSchema = &jsonschema.Schema{
Description: listIdsUsage,
Default: json.RawMessage(`[]`),
},
"managed_by_me": {Type: "boolean", Description: mbmUsage},
"for": {
Type: "string", Description: forUsage,
Enum: []any{"managedByMe", "mine", "mySubscribers"},
},
"max_results": {
Type: "number", Description: pkg.MRUsage,
Default: json.RawMessage("5"),
Minimum: new(float64(0)),
},
"mine": {Type: "boolean", Description: mineUsage},
"my_subscribers": {Type: "boolean", Description: msUsage},
"on_behalf_of_content_owner": {Type: "string", Description: pkg.OBOCOUsage},
"parts": {
Type: "array", Description: pkg.PartsUsage,
@@ -95,16 +96,10 @@ func init() {
)
listCmd.Flags().StringVarP(&hl, "hl", "l", "", hlUsage)
listCmd.Flags().StringSliceVarP(&ids, "ids", "i", []string{}, listIdsUsage)
listCmd.Flags().BoolVarP(
managedByMe, "managedByMe", "E", false, mbmUsage,
)
listCmd.Flags().StringVarP(&channelFor, "for", "F", "", forUsage)
listCmd.Flags().Int64VarP(
&maxResults, "maxResults", "n", 5, pkg.MRUsage,
)
listCmd.Flags().BoolVarP(mine, "mine", "M", true, mineUsage)
listCmd.Flags().BoolVarP(
mySubscribers, "mySubscribers", "S", false, msUsage,
)
listCmd.Flags().StringVarP(
&onBehalfOfContentOwner, "onBehalfOfContentOwner", "b", "", pkg.OBOCOUsage,
)
@@ -127,10 +122,8 @@ var listCmd = &cobra.Command{
channel.WithForUsername(forUsername),
channel.WithHl(hl),
channel.WithIds(ids),
channel.WithChannelManagedByMe(managedByMe),
channel.WithFor(channelFor),
channel.WithMaxResults(maxResults),
channel.WithMine(mine),
channel.WithMySubscribers(mySubscribers),
channel.WithOnBehalfOfContentOwner(onBehalfOfContentOwner),
channel.WithParts(parts),
channel.WithOutput(output),
+10 -31
View File
@@ -23,9 +23,7 @@ type Channel struct {
CategoryId string `yaml:"category_id" json:"category_id,omitempty"`
ForHandle string `yaml:"for_handle" json:"for_handle,omitempty"`
ForUsername string `yaml:"for_username" json:"for_username,omitempty"`
ManagedByMe *bool `yaml:"managed_by_me" json:"managed_by_me,omitempty"`
Mine *bool `yaml:"mine" json:"mine,omitempty"`
MySubscribers *bool `yaml:"my_subscribers" json:"my_subscribers,omitempty"`
For string `yaml:"for" json:"for,omitempty"`
Country string `yaml:"country" json:"country,omitempty"`
CustomUrl string `yaml:"custom_url" json:"custom_url,omitempty"`
DefaultLanguage string `yaml:"default_language" json:"default_language,omitempty"`
@@ -69,14 +67,13 @@ func (c *Channel) Get() ([]*youtube.Channel, error) {
if len(c.Ids) > 0 {
call = call.Id(c.Ids...)
}
if c.ManagedByMe != nil {
call = call.ManagedByMe(*c.ManagedByMe)
}
if c.Mine != nil {
call = call.Mine(*c.Mine)
}
if c.MySubscribers != nil {
call = call.MySubscribers(*c.MySubscribers)
switch c.For {
case "managedByMe":
call = call.ManagedByMe(true)
case "mine":
call = call.Mine(true)
case "mySubscribers":
call = call.MySubscribers(true)
}
if c.OnBehalfOfContentOwner != "" {
call = call.OnBehalfOfContentOwner(c.OnBehalfOfContentOwner)
@@ -166,27 +163,9 @@ func WithForUsername(username string) Option {
}
}
func WithChannelManagedByMe(managedByMe *bool) Option {
func WithFor(f string) Option {
return func(c *Channel) {
if managedByMe != nil {
c.ManagedByMe = managedByMe
}
}
}
func WithMine(mine *bool) Option {
return func(c *Channel) {
if mine != nil {
c.Mine = mine
}
}
}
func WithMySubscribers(mySubscribers *bool) Option {
return func(c *Channel) {
if mySubscribers != nil {
c.MySubscribers = mySubscribers
}
c.For = f
}
}
+11 -36
View File
@@ -21,12 +21,6 @@ func TestNewChannel(t *testing.T) {
}
svc := &youtube.Service{}
managedByMeTrue := true
managedByMeFalse := false
mineTrue := true
mineFalse := false
mySubscribersTrue := true
mySubscribersFalse := false
tests := []struct {
name string
@@ -42,10 +36,8 @@ func TestNewChannel(t *testing.T) {
WithForUsername("testuser"),
WithHl("en"),
WithIds([]string{"channel1", "channel2"}),
WithChannelManagedByMe(&managedByMeTrue),
WithFor("managedByMe"),
WithMaxResults(100),
WithMine(&mineTrue),
WithMySubscribers(&mySubscribersTrue),
WithOnBehalfOfContentOwner("owner123"),
WithCountry("US"),
WithCustomUrl("testchannel"),
@@ -70,9 +62,7 @@ func TestNewChannel(t *testing.T) {
CategoryId: "category123",
ForHandle: "@testhandle",
ForUsername: "testuser",
ManagedByMe: &managedByMeTrue,
Mine: &mineTrue,
MySubscribers: &mySubscribersTrue,
For: "managedByMe",
Country: "US",
CustomUrl: "testchannel",
DefaultLanguage: "en",
@@ -92,12 +82,10 @@ func TestNewChannel(t *testing.T) {
},
},
{
name: "with nil boolean options",
name: "with empty for option",
args: args{
opts: []Option{
WithChannelManagedByMe(nil),
WithMine(nil),
WithMySubscribers(nil),
WithFor(""),
WithService(svc),
},
},
@@ -106,20 +94,16 @@ func TestNewChannel(t *testing.T) {
},
},
{
name: "with false boolean options",
name: "with for mine",
args: args{
opts: []Option{
WithChannelManagedByMe(&managedByMeFalse),
WithMine(&mineFalse),
WithMySubscribers(&mySubscribersFalse),
WithFor("mine"),
WithService(svc),
},
},
want: &Channel{
Fields: common.Fields{Service: svc},
ManagedByMe: &managedByMeFalse,
Mine: &mineFalse,
MySubscribers: &mySubscribersFalse,
Fields: common.Fields{Service: svc},
For: "mine",
},
},
{
@@ -285,10 +269,7 @@ func TestChannel_Get(t *testing.T) {
{
name: "get channels managedByMe",
opts: []Option{
func(c *Channel) {
b := true
c.ManagedByMe = &b
},
WithFor("managedByMe"),
WithMaxResults(1),
},
verify: func(r *http.Request) {
@@ -319,10 +300,7 @@ func TestChannel_Get(t *testing.T) {
{
name: "get channels mine",
opts: []Option{
func(c *Channel) {
b := true
c.Mine = &b
},
WithFor("mine"),
WithMaxResults(1),
},
verify: func(r *http.Request) {
@@ -336,10 +314,7 @@ func TestChannel_Get(t *testing.T) {
{
name: "get channels mySubscribers",
opts: []Option{
func(c *Channel) {
b := true
c.MySubscribers = &b
},
WithFor("mySubscribers"),
WithMaxResults(1),
},
verify: func(r *http.Request) {