diff --git a/cmd/search/list.go b/cmd/search/list.go index 1f02c11..9adbd79 100644 --- a/cmd/search/list.go +++ b/cmd/search/list.go @@ -44,11 +44,12 @@ var listInSchema = &jsonschema.Schema{ Type: "string", Enum: []any{"none", "upcoming", "live", "completed"}, Description: etUsage, Default: json.RawMessage(`"none"`), }, - "for_content_owner": {Type: "boolean", Description: fcoUsage}, - "for_developer": {Type: "boolean", Description: fdUsage}, - "for_mine": {Type: "boolean", Description: fmUsage}, - "location": {Type: "string", Description: locationUsage}, - "location_radius": {Type: "string", Description: lrUsage}, + "for": { + Type: "string", Description: forUsage, + Enum: []any{"mine", "developer", "contentOwner"}, + }, + "location": {Type: "string", Description: locationUsage}, + "location_radius": {Type: "string", Description: lrUsage}, "max_results": { Type: "number", Description: pkg.MRUsage, Default: json.RawMessage("5"), @@ -153,11 +154,7 @@ func init() { &channelType, "channelType", "channelTypeUnspecified", ctUsage, ) listCmd.Flags().StringVar(&eventType, "eventType", "none", etUsage) - listCmd.Flags().BoolVar( - forContentOwner, "forContentOwner", false, fcoUsage, - ) - listCmd.Flags().BoolVar(forDeveloper, "forDeveloper", false, fdUsage) - listCmd.Flags().BoolVar(forMine, "forMine", false, fmUsage) + listCmd.Flags().StringVar(&searchFor, "for", "", forUsage) listCmd.Flags().StringVar(&location, "location", "", locationUsage) listCmd.Flags().StringVar(&locationRadius, "locationRadius", "", lrUsage) listCmd.Flags().Int64Var(&maxResults, "maxResults", 5, pkg.MRUsage) @@ -202,9 +199,7 @@ var listCmd = &cobra.Command{ search.WithChannelId(channelId), search.WithChannelType(channelType), search.WithEventType(eventType), - search.WithForContentOwner(forContentOwner), - search.WithForDeveloper(forDeveloper), - search.WithForMine(forMine), + search.WithFor(searchFor), search.WithLocation(location), search.WithLocationRadius(locationRadius), search.WithMaxResults(maxResults), diff --git a/cmd/search/search.go b/cmd/search/search.go index 41d7d13..15b244a 100644 --- a/cmd/search/search.go +++ b/cmd/search/search.go @@ -5,7 +5,6 @@ package search import ( "github.com/eat-pray-ai/yutu/cmd" - "github.com/eat-pray-ai/yutu/pkg/utils" "github.com/spf13/cobra" ) @@ -15,9 +14,7 @@ const ( cidUsage = "Filter on resources belonging to this channelId" ctUsage = "channelTypeUnspecified|any|show" etUsage = "none|upcoming|live|completed" - fcoUsage = "Search owned by content owner" - fdUsage = "Only retrieve videos uploaded using the project id of the authenticated user" - fmUsage = "Search for the private videos of the authenticated user" + forUsage = "mine|developer|contentOwner" locationUsage = "Filter on location of the video" lrUsage = "Filter on distance from the location" orderUsage = "searchSortUnspecified, date, rating, viewCount, relevance, title, videoCount" @@ -45,9 +42,7 @@ var ( channelId string channelType string eventType string - forContentOwner = new(false) - forDeveloper = new(false) - forMine = new(false) + searchFor string location string locationRadius string maxResults int64 @@ -78,14 +73,6 @@ var searchCmd = &cobra.Command{ Use: "search", Short: short, Long: long, - PersistentPreRun: func(cmd *cobra.Command, args []string) { - boolMap := map[string]**bool{ - "forContentOwner": &forContentOwner, - "forDeveloper": &forDeveloper, - "forMine": &forMine, - } - utils.ResetBool(boolMap, cmd.Flags()) - }, Run: func(cmd *cobra.Command, args []string) { _ = cmd.Help() }, diff --git a/pkg/search/search.go b/pkg/search/search.go index 27f5cfb..b468bc8 100644 --- a/pkg/search/search.go +++ b/pkg/search/search.go @@ -20,9 +20,7 @@ type Search struct { common.Fields ChannelType string `yaml:"channel_type" json:"channel_type,omitempty"` EventType string `yaml:"event_type" json:"event_type,omitempty"` - ForContentOwner *bool `yaml:"for_content_owner" json:"for_content_owner,omitempty"` - ForDeveloper *bool `yaml:"for_developer" json:"for_developer,omitempty"` - ForMine *bool `yaml:"for_mine" json:"for_mine,omitempty"` + For string `yaml:"for" json:"for,omitempty"` Location string `yaml:"location" json:"location,omitempty"` LocationRadius string `yaml:"location_radius" json:"location_radius,omitempty"` Order string `yaml:"order" json:"order,omitempty"` @@ -75,14 +73,13 @@ func (s *Search) Get() ([]*youtube.SearchResult, error) { if s.EventType != "" { call = call.EventType(s.EventType) } - if s.ForContentOwner != nil { - call = call.ForContentOwner(*s.ForContentOwner) - } - if s.ForDeveloper != nil { - call = call.ForDeveloper(*s.ForDeveloper) - } - if s.ForMine != nil { - call = call.ForMine(*s.ForMine) + switch s.For { + case "mine": + call = call.ForMine(true) + case "developer": + call = call.ForDeveloper(true) + case "contentOwner": + call = call.ForContentOwner(true) } if s.Location != "" { call = call.Location(s.Location) @@ -195,27 +192,9 @@ func WithEventType(eventType string) Option { } } -func WithForContentOwner(forContentOwner *bool) Option { +func WithFor(f string) Option { return func(s *Search) { - if forContentOwner != nil { - s.ForContentOwner = forContentOwner - } - } -} - -func WithForDeveloper(forDeveloper *bool) Option { - return func(s *Search) { - if forDeveloper != nil { - s.ForDeveloper = forDeveloper - } - } -} - -func WithForMine(forMine *bool) Option { - return func(s *Search) { - if forMine != nil { - s.ForMine = forMine - } + s.For = f } } diff --git a/pkg/search/search_test.go b/pkg/search/search_test.go index 4021e8e..4de9ffb 100644 --- a/pkg/search/search_test.go +++ b/pkg/search/search_test.go @@ -21,12 +21,6 @@ func TestNewSearch(t *testing.T) { opts []Option } - forContentOwnerTrue := true - forContentOwnerFalse := false - forDeveloperTrue := true - forDeveloperFalse := false - forMineTrue := true - forMineFalse := false svc := &youtube.Service{} tests := []struct { @@ -41,9 +35,7 @@ func TestNewSearch(t *testing.T) { WithChannelId("channel123"), WithChannelType("any"), WithEventType("live"), - WithForContentOwner(&forContentOwnerTrue), - WithForDeveloper(&forDeveloperTrue), - WithForMine(&forMineTrue), + WithFor("mine"), WithLocation("37.42307,-122.08427"), WithLocationRadius("50km"), WithMaxResults(50), @@ -83,9 +75,7 @@ func TestNewSearch(t *testing.T) { }, ChannelType: "any", EventType: "live", - ForContentOwner: &forContentOwnerTrue, - ForDeveloper: &forDeveloperTrue, - ForMine: &forMineTrue, + For: "mine", Location: "37.42307,-122.08427", LocationRadius: "50km", Order: "relevance", @@ -117,30 +107,24 @@ func TestNewSearch(t *testing.T) { want: &Search{Fields: common.Fields{}}, }, { - name: "with nil boolean options", + name: "with empty for option", args: args{ opts: []Option{ - WithForContentOwner(nil), - WithForDeveloper(nil), - WithForMine(nil), + WithFor(""), }, }, want: &Search{Fields: common.Fields{}}, }, { - name: "with false boolean options", + name: "with for developer", args: args{ opts: []Option{ - WithForContentOwner(&forContentOwnerFalse), - WithForDeveloper(&forDeveloperFalse), - WithForMine(&forMineFalse), + WithFor("developer"), }, }, want: &Search{ - Fields: common.Fields{}, - ForContentOwner: &forContentOwnerFalse, - ForDeveloper: &forDeveloperFalse, - ForMine: &forMineFalse, + Fields: common.Fields{}, + For: "developer", }, }, { @@ -257,10 +241,6 @@ func TestNewSearch(t *testing.T) { } func TestSearch_Get(t *testing.T) { - forMineTrue := true - forDeveloperTrue := true - forContentOwnerTrue := true - tests := []struct { name string opts []Option @@ -383,11 +363,9 @@ func TestSearch_Get(t *testing.T) { wantErr: false, }, { - name: "get search with boolean flags", + name: "get search with for mine", opts: []Option{ - WithForMine(&forMineTrue), - WithForDeveloper(&forDeveloperTrue), - WithForContentOwner(&forContentOwnerTrue), + WithFor("mine"), WithMaxResults(1), }, verify: func(r *http.Request) { @@ -395,13 +373,35 @@ func TestSearch_Get(t *testing.T) { if q.Get("forMine") != "true" { t.Errorf("expected forMine=true, got %s", q.Get("forMine")) } + }, + wantLen: 1, + wantErr: false, + }, + { + name: "get search with for developer", + opts: []Option{ + WithFor("developer"), + WithMaxResults(1), + }, + verify: func(r *http.Request) { + q := r.URL.Query() if q.Get("forDeveloper") != "true" { t.Errorf("expected forDeveloper=true, got %s", q.Get("forDeveloper")) } + }, + wantLen: 1, + wantErr: false, + }, + { + name: "get search with for contentOwner", + opts: []Option{ + WithFor("contentOwner"), + WithMaxResults(1), + }, + verify: func(r *http.Request) { + q := r.URL.Query() if q.Get("forContentOwner") != "true" { - t.Errorf( - "expected forContentOwner=true, got %s", q.Get("forContentOwner"), - ) + t.Errorf("expected forContentOwner=true, got %s", q.Get("forContentOwner")) } }, wantLen: 1,