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

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

No files matched your search

+2 -12
View File
@@ -5,7 +5,6 @@ package activity
import (
"github.com/eat-pray-ai/yutu/cmd"
"github.com/eat-pray-ai/yutu/pkg/utils"
"github.com/spf13/cobra"
)
@@ -13,8 +12,7 @@ const (
short = "Manage activities on YouTube"
long = "Manage activities on YouTube. Use this tool to list channel activities."
ciUsage = "ID of the channel"
homeUsage = "true|false|null"
mineUsage = "true|false|null"
forUsage = "home|mine"
paUsage = "Filter on activities published after this date"
pbUsage = "Filter on activities published before this date"
rcUsage = "Display the content as seen by viewers in this country"
@@ -22,9 +20,8 @@ const (
var (
channelId string
home = new(false)
activityFor string
maxResults int64
mine = new(false)
publishedAfter string
publishedBefore string
regionCode string
@@ -35,13 +32,6 @@ var activityCmd = &cobra.Command{
Use: "activity",
Short: short,
Long: long,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
boolMap := map[string]**bool{
"home": &home,
"mine": &mine,
}
utils.ResetBool(boolMap, cmd.Flags())
},
Run: func(cmd *cobra.Command, args []string) {
_ = cmd.Help()
},
+6 -6
View File
@@ -34,12 +34,14 @@ var listInSchema = &jsonschema.Schema{
Required: []string{},
Properties: map[string]*jsonschema.Schema{
"channel_id": {Type: "string", Description: ciUsage},
"home": {Type: "boolean", Description: homeUsage},
"for": {
Type: "string", Description: forUsage,
Enum: []any{"home", "mine"},
},
"max_results": {
Type: "number", Description: pkg.MRUsage,
Default: json.RawMessage("5"), Minimum: new(float64(0)),
},
"mine": {Type: "boolean", Description: mineUsage},
"published_after": {Type: "string", Description: paUsage},
"published_before": {Type: "string", Description: pbUsage},
"region_code": {Type: "string", Description: rcUsage},
@@ -73,9 +75,8 @@ func init() {
)
activityCmd.AddCommand(listCmd)
listCmd.Flags().StringVarP(&channelId, "channelId", "c", "", ciUsage)
listCmd.Flags().BoolVarP(home, "home", "H", true, homeUsage)
listCmd.Flags().StringVarP(&activityFor, "for", "F", "", forUsage)
listCmd.Flags().Int64VarP(&maxResults, "maxResults", "n", 5, pkg.MRUsage)
listCmd.Flags().BoolVarP(mine, "mine", "M", true, mineUsage)
listCmd.Flags().StringVarP(
&publishedAfter, "publishedAfter", "a", "", paUsage,
)
@@ -99,9 +100,8 @@ var listCmd = &cobra.Command{
output, _ := cmd.Flags().GetString("output")
input := activity.NewActivity(
activity.WithChannelId(channelId),
activity.WithHome(home),
activity.WithFor(activityFor),
activity.WithMaxResults(maxResults),
activity.WithMine(mine),
activity.WithPublishedAfter(publishedAfter),
activity.WithPublishedBefore(publishedBefore),
activity.WithRegionCode(regionCode),
+10 -22
View File
@@ -18,11 +18,10 @@ var (
type Activity struct {
common.Fields
Home *bool `yaml:"home" json:"home,omitempty"`
Mine *bool `yaml:"mine" json:"mine,omitempty"`
PublishedAfter string `yaml:"published_after" json:"published_after,omitempty"`
For string `yaml:"for" json:"for,omitempty"`
PublishedAfter string `yaml:"published_after" json:"published_after,omitempty"`
PublishedBefore string `yaml:"published_before" json:"published_before,omitempty"`
RegionCode string `yaml:"region_code" json:"region_code,omitempty"`
RegionCode string `yaml:"region_code" json:"region_code,omitempty"`
}
type IActivity[T any] interface {
@@ -49,12 +48,11 @@ func (a *Activity) Get() ([]*youtube.Activity, error) {
call = call.ChannelId(a.ChannelId)
}
if a.Home != nil {
call = call.Home(*a.Home)
}
if a.Mine != nil {
call = call.Mine(*a.Mine)
switch a.For {
case "home":
call = call.Home(true)
case "mine":
call = call.Mine(true)
}
if a.PublishedAfter != "" {
@@ -92,19 +90,9 @@ func (a *Activity) List(writer io.Writer) error {
return err
}
func WithHome(home *bool) Option {
func WithFor(f string) Option {
return func(a *Activity) {
if home != nil {
a.Home = home
}
}
}
func WithMine(mine *bool) Option {
return func(a *Activity) {
if mine != nil {
a.Mine = mine
}
a.For = f
}
}
+11 -23
View File
@@ -20,10 +20,6 @@ func TestNewActivity(t *testing.T) {
}
svc := &youtube.Service{}
homeTrue := true
homeFalse := false
mineTrue := true
mineFalse := false
tests := []struct {
name string
@@ -35,9 +31,8 @@ func TestNewActivity(t *testing.T) {
args: args{
opts: []Option{
WithChannelId("test-channel-123"),
WithHome(&homeTrue),
WithFor("mine"),
WithMaxResults(50),
WithMine(&mineTrue),
WithPublishedAfter("2024-01-01T00:00:00Z"),
WithPublishedBefore("2024-12-31T23:59:59Z"),
WithRegionCode("US"),
@@ -50,11 +45,10 @@ func TestNewActivity(t *testing.T) {
ChannelId: "test-channel-123",
MaxResults: 50,
},
Home: &homeTrue,
Mine: &mineTrue,
PublishedAfter: "2024-01-01T00:00:00Z",
For: "mine",
PublishedAfter: "2024-01-01T00:00:00Z",
PublishedBefore: "2024-12-31T23:59:59Z",
RegionCode: "US",
RegionCode: "US",
},
},
{
@@ -65,27 +59,24 @@ func TestNewActivity(t *testing.T) {
want: &Activity{Fields: common.Fields{}},
},
{
name: "with nil boolean options",
name: "with empty for option",
args: args{
opts: []Option{
WithHome(nil),
WithMine(nil),
WithFor(""),
},
},
want: &Activity{Fields: common.Fields{}},
},
{
name: "with false boolean options",
name: "with for home",
args: args{
opts: []Option{
WithHome(&homeFalse),
WithMine(&mineFalse),
WithFor("home"),
},
},
want: &Activity{
Fields: common.Fields{},
Home: &homeFalse,
Mine: &mineFalse,
For: "home",
},
},
{
@@ -160,9 +151,6 @@ func TestNewActivity(t *testing.T) {
}
func TestActivity_Get(t *testing.T) {
homeTrue := true
mineTrue := true
tests := []struct {
name string
opts []Option
@@ -190,7 +178,7 @@ func TestActivity_Get(t *testing.T) {
{
name: "get activities with home",
opts: []Option{
WithHome(&homeTrue),
WithFor("home"),
WithMaxResults(2),
},
verify: func(r *http.Request) {
@@ -204,7 +192,7 @@ func TestActivity_Get(t *testing.T) {
{
name: "get activities with mine",
opts: []Option{
WithMine(&mineTrue),
WithFor("mine"),
WithMaxResults(2),
},
verify: func(r *http.Request) {