Skip to content

Commit 0264b55

Browse files
committed
♻️ Refactor bool as pointer
1 parent 6b67d3a commit 0264b55

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+490
-283
lines changed

MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use_repo(
1919
"com_github_mark3labs_mcp_go",
2020
"com_github_savioxavier_termlink",
2121
"com_github_spf13_cobra",
22+
"com_github_spf13_pflag",
2223
"com_github_spf13_viper",
2324
"in_gopkg_yaml_v3",
2425
"org_golang_google_api",

cmd/activity/activity.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,25 @@ import (
66
"github.com/spf13/cobra"
77
)
88

9+
const (
10+
shortUsage = "List YouTube activities"
11+
longUsage = "List YouTube activities"
12+
channelIdUsage = "ID of the channel"
13+
homeUsage = "true or false or empty"
14+
maxResultsUsage = "The maximum number of items that should be returned"
15+
mineUsage = "true or false or empty"
16+
publishedAfterUsage = "Filter on activities published after this date"
17+
publishedBeforeUsage = "Filter on activities published before this date"
18+
regionCodeUsage = ""
19+
partsUsage = "Comma separated parts"
20+
outputUsage = "json or yaml"
21+
)
22+
923
var (
1024
channelId string
11-
home = utils.BoolPtr("false")
25+
home = utils.BoolPtr("")
1226
maxResults int64
13-
mine = utils.BoolPtr("true")
27+
mine = utils.BoolPtr("")
1428
publishedAfter string
1529
publishedBefore string
1630
regionCode string
@@ -20,8 +34,15 @@ var (
2034

2135
var activityCmd = &cobra.Command{
2236
Use: "activity",
23-
Short: "List YouTube activities",
24-
Long: "List YouTube activities",
37+
Short: shortUsage,
38+
Long: longUsage,
39+
PersistentPreRun: func(cmd *cobra.Command, args []string) {
40+
boolMap := map[string]*bool{
41+
"home": home,
42+
"mine": mine,
43+
}
44+
utils.ResetBool(boolMap, cmd.Flags())
45+
},
2546
Run: func(cmd *cobra.Command, args []string) {
2647
_ = cmd.Help()
2748
},

cmd/activity/list.go

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,51 +12,44 @@ import (
1212
)
1313

1414
const (
15-
shortDesc = "List YouTube activities"
16-
longDesc = "List YouTube activities, such as likes, favorites, uploads, etc."
17-
channelIdDesc = "ID of the channel"
18-
homeDesc = "true or false"
19-
maxResultsDesc = "The maximum number of items that should be returned"
20-
mineDesc = "true or false"
21-
publishedAfterDesc = "Filter on activities published after this date"
22-
publishedBeforeDesc = "Filter on activities published before this date"
23-
regionCodeDesc = ""
24-
partsDesc = "Comma separated parts"
25-
outputDesc = "json or yaml"
15+
listShortUsage = "List YouTube activities"
16+
listLongUsage = "List YouTube activities, such as likes, favorites, uploads, etc."
2617
)
2718

19+
var defaultParts = []string{"id", "snippet", "contentDetails"}
20+
2821
var listTool = mcp.NewTool(
2922
"activity.list",
30-
mcp.WithDescription(longDesc),
23+
mcp.WithDescription(listLongUsage),
3124
mcp.WithString(
32-
"channelId", mcp.DefaultString(""), mcp.Description(channelIdDesc),
25+
"channelId", mcp.DefaultString(""), mcp.Description(channelIdUsage),
3326
),
3427
mcp.WithString(
3528
"home", mcp.Enum("true", "false", ""),
36-
mcp.DefaultString(""), mcp.Description(homeDesc),
29+
mcp.DefaultString(""), mcp.Description(homeUsage),
3730
),
3831
mcp.WithNumber(
39-
"maxResults", mcp.DefaultNumber(5), mcp.Description(maxResultsDesc),
32+
"maxResults", mcp.DefaultNumber(5), mcp.Description(maxResultsUsage),
4033
),
4134
mcp.WithString(
4235
"mine", mcp.Enum("true", "false", ""),
43-
mcp.DefaultString("true"), mcp.Description(mineDesc),
36+
mcp.DefaultString("true"), mcp.Description(mineUsage),
4437
),
4538
mcp.WithString(
46-
"publishedAfter", mcp.DefaultString(""), mcp.Description(publishedAfterDesc),
39+
"publishedAfter", mcp.DefaultString(""),
40+
mcp.Description(publishedAfterUsage),
4741
),
4842
mcp.WithString(
4943
"publishedBefore", mcp.DefaultString(""),
50-
mcp.Description(publishedBeforeDesc),
44+
mcp.Description(publishedBeforeUsage),
5145
),
5246
mcp.WithString(
53-
"regionCode", mcp.DefaultString(""), mcp.Description(regionCodeDesc),
47+
"regionCode", mcp.DefaultString(""), mcp.Description(regionCodeUsage),
5448
),
5549
mcp.WithArray(
56-
"parts", mcp.DefaultArray([]string{"id", "snippet", "contentDetails"}),
57-
mcp.Description(partsDesc),
50+
"parts", mcp.DefaultArray(defaultParts), mcp.Description(partsUsage),
5851
),
59-
mcp.WithString("output", mcp.DefaultString(""), mcp.Description(outputDesc)),
52+
mcp.WithString("output", mcp.DefaultString(""), mcp.Description(outputUsage)),
6053
)
6154

6255
func run(writer io.Writer) error {
@@ -76,16 +69,8 @@ func run(writer io.Writer) error {
7669

7770
var listCmd = &cobra.Command{
7871
Use: "list",
79-
Short: shortDesc,
80-
Long: longDesc,
81-
PreRun: func(cmd *cobra.Command, args []string) {
82-
if !cmd.Flags().Lookup("home").Changed {
83-
home = nil
84-
}
85-
if !cmd.Flags().Lookup("mine").Changed {
86-
mine = nil
87-
}
88-
},
72+
Short: listShortUsage,
73+
Long: listLongUsage,
8974
Run: func(cmd *cobra.Command, args []string) {
9075
err := run(cmd.OutOrStdout())
9176
if err != nil {
@@ -99,25 +84,25 @@ func init() {
9984
cmd.MCP.AddTool(listTool, listHandler)
10085
activityCmd.AddCommand(listCmd)
10186
listCmd.Flags().StringVarP(
102-
&channelId, "channelId", "c", "", channelIdDesc,
87+
&channelId, "channelId", "c", "", channelIdUsage,
10388
)
104-
listCmd.Flags().BoolVarP(home, "home", "H", true, homeDesc)
89+
listCmd.Flags().BoolVarP(home, "home", "H", true, homeUsage)
10590
listCmd.Flags().Int64VarP(
106-
&maxResults, "maxResults", "n", 5, maxResultsDesc,
91+
&maxResults, "maxResults", "n", 5, maxResultsUsage,
10792
)
108-
listCmd.Flags().BoolVarP(mine, "mine", "M", true, mineDesc)
93+
listCmd.Flags().BoolVarP(mine, "mine", "M", true, mineUsage)
10994
listCmd.Flags().StringVarP(
110-
&publishedAfter, "publishedAfter", "a", "", publishedAfterDesc,
95+
&publishedAfter, "publishedAfter", "a", "", publishedAfterUsage,
11196
)
11297
listCmd.Flags().StringVarP(
113-
&publishedBefore, "publishedBefore", "b", "", publishedBeforeDesc,
98+
&publishedBefore, "publishedBefore", "b", "", publishedBeforeUsage,
11499
)
115-
listCmd.Flags().StringVarP(&regionCode, "regionCode", "r", "", regionCodeDesc)
100+
listCmd.Flags().StringVarP(&regionCode, "regionCode", "r", "", regionCodeUsage)
116101

117102
listCmd.Flags().StringArrayVarP(
118-
&parts, "parts", "p", []string{"id", "snippet", "contentDetails"}, partsDesc,
103+
&parts, "parts", "p", defaultParts, partsUsage,
119104
)
120-
listCmd.Flags().StringVarP(&output, "output", "o", "", outputDesc)
105+
listCmd.Flags().StringVarP(&output, "output", "o", "", outputUsage)
121106
}
122107

123108
func listHandler(ctx context.Context, request mcp.CallToolRequest) (

cmd/caption/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ go_library(
1515
deps = [
1616
"//cmd",
1717
"//pkg/caption",
18+
"//pkg/utils",
1819
"@com_github_spf13_cobra//:cobra",
20+
"@com_github_spf13_pflag//:pflag",
1921
],
2022
)

cmd/caption/caption.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@ package caption
22

33
import (
44
"github.com/eat-pray-ai/yutu/cmd"
5+
"github.com/eat-pray-ai/yutu/pkg/utils"
56
"github.com/spf13/cobra"
7+
"github.com/spf13/pflag"
68
)
79

810
var (
911
id string
1012
file string
1113
audioTrackType string
12-
isAutoSynced bool
13-
isCC bool
14-
isDraft bool
15-
isEasyReader bool
16-
isLarge bool
14+
isAutoSynced = utils.BoolPtr("")
15+
isCC = utils.BoolPtr("")
16+
isDraft = utils.BoolPtr("")
17+
isEasyReader = utils.BoolPtr("")
18+
isLarge = utils.BoolPtr("")
1719
language string
1820
name string
1921
trackKind string
@@ -30,6 +32,9 @@ var captionCmd = &cobra.Command{
3032
Use: "caption",
3133
Short: "Manipulate YouTube captions",
3234
Long: "List, insert, update, download, or delete YouTube captions",
35+
PersistentPreRun: func(cmd *cobra.Command, args []string) {
36+
resetFlags(cmd.Flags())
37+
},
3338
Run: func(cmd *cobra.Command, args []string) {
3439
_ = cmd.Help()
3540
},
@@ -38,3 +43,15 @@ var captionCmd = &cobra.Command{
3843
func init() {
3944
cmd.RootCmd.AddCommand(captionCmd)
4045
}
46+
47+
func resetFlags(flagSet *pflag.FlagSet) {
48+
boolMap := map[string]*bool{
49+
"isAutoSynced": isAutoSynced,
50+
"isCC": isCC,
51+
"isDraft": isDraft,
52+
"isEasyReader": isEasyReader,
53+
"isLarge": isLarge,
54+
}
55+
56+
utils.ResetBool(boolMap, flagSet)
57+
}

cmd/caption/delete.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,7 @@ func init() {
2525

2626
deleteCmd.Flags().StringVarP(&id, "id", "i", "", "ID of the caption")
2727
deleteCmd.Flags().StringVarP(&onBehalfOf, "onBehalfOf", "b", "", "")
28-
deleteCmd.Flags().StringVarP(&onBehalfOfContentOwner, "onBehalfOfContentOwner", "B", "", "")
28+
deleteCmd.Flags().StringVarP(
29+
&onBehalfOfContentOwner, "onBehalfOfContentOwner", "B", "", "",
30+
)
2931
}

cmd/caption/download.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,17 @@ func init() {
2727
captionCmd.AddCommand(downloadCmd)
2828

2929
downloadCmd.Flags().StringVarP(&id, "id", "i", "", "ID of the caption")
30-
downloadCmd.Flags().StringVarP(&file, "file", "f", "", "Path to save the caption file")
30+
downloadCmd.Flags().StringVarP(
31+
&file, "file", "f", "", "Path to save the caption file",
32+
)
3133
downloadCmd.Flags().StringVarP(&tfmt, "tfmt", "t", "", "sbv, srt or vtt")
32-
downloadCmd.Flags().StringVarP(&tlang, "tlang", "l", "", "Translate the captions into this language")
34+
downloadCmd.Flags().StringVarP(
35+
&tlang, "tlang", "l", "", "Translate the captions into this language",
36+
)
3337
downloadCmd.Flags().StringVarP(&onBehalfOf, "onBehalfOf", "b", "", "")
34-
downloadCmd.Flags().StringVarP(&onBehalfOfContentOwner, "onBehalfOfContentOwner", "B", "", "")
38+
downloadCmd.Flags().StringVarP(
39+
&onBehalfOfContentOwner, "onBehalfOfContentOwner", "B", "", "",
40+
)
3541

3642
downloadCmd.MarkFlagRequired("id")
3743
downloadCmd.MarkFlagRequired("file")

cmd/caption/insert.go

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ var insertCmd = &cobra.Command{
1313
c := caption.NewCation(
1414
caption.WithFile(file),
1515
caption.WithAudioTrackType(audioTrackType),
16-
caption.WithIsAutoSynced(isAutoSynced, true),
17-
caption.WithIsCC(isCC, true),
18-
caption.WithIsDraft(isDraft, true),
19-
caption.WithIsEasyReader(isEasyReader, true),
20-
caption.WithIsLarge(isLarge, true),
16+
caption.WithIsAutoSynced(isAutoSynced),
17+
caption.WithIsCC(isCC),
18+
caption.WithIsDraft(isDraft),
19+
caption.WithIsEasyReader(isEasyReader),
20+
caption.WithIsLarge(isLarge),
2121
caption.WithLanguage(language),
2222
caption.WithName(name),
2323
caption.WithTrackKind(trackKind),
@@ -33,31 +33,47 @@ var insertCmd = &cobra.Command{
3333
func init() {
3434
captionCmd.AddCommand(insertCmd)
3535

36-
insertCmd.Flags().StringVarP(&file, "file", "f", "", "Path to the caption file")
3736
insertCmd.Flags().StringVarP(
38-
&audioTrackType, "audioTrackType", "a", "unknown", "unknown, primary, commentary or descriptive",
37+
&file, "file", "f", "", "Path to the caption file",
38+
)
39+
insertCmd.Flags().StringVarP(
40+
&audioTrackType, "audioTrackType", "a", "unknown",
41+
"unknown, primary, commentary or descriptive",
3942
)
4043
insertCmd.Flags().BoolVarP(
41-
&isAutoSynced, "isAutoSynced", "A", true,
44+
isAutoSynced, "isAutoSynced", "A", true,
4245
"Whether YouTube synchronized the caption track to the audio track in the video",
4346
)
4447
insertCmd.Flags().BoolVarP(
45-
&isCC, "isCC", "C", false,
48+
isCC, "isCC", "C", false,
4649
"Whether the track contains closed captions for the deaf and hard of hearing",
4750
)
48-
insertCmd.Flags().BoolVarP(&isDraft, "isDraft", "D", false, "whether the caption track is a draft")
4951
insertCmd.Flags().BoolVarP(
50-
&isEasyReader, "isEasyReader", "E", false, "Whether caption track is formatted for 'easy reader'",
52+
isDraft, "isDraft", "D", false, "whether the caption track is a draft",
53+
)
54+
insertCmd.Flags().BoolVarP(
55+
isEasyReader, "isEasyReader", "E", false,
56+
"Whether caption track is formatted for 'easy reader'",
5157
)
5258
insertCmd.Flags().BoolVarP(
53-
&isLarge, "isLarge", "L", false,
59+
isLarge, "isLarge", "L", false,
5460
"Whether the caption track uses large text for the vision-impaired",
5561
)
56-
insertCmd.Flags().StringVarP(&language, "language", "l", "", "Language of the caption track")
57-
insertCmd.Flags().StringVarP(&name, "name", "n", "", "Name of the caption track")
58-
insertCmd.Flags().StringVarP(&trackKind, "trackKind", "t", "standard", "standard, ASR or forced")
62+
insertCmd.Flags().StringVarP(
63+
&language, "language", "l", "", "Language of the caption track",
64+
)
65+
insertCmd.Flags().StringVarP(
66+
&name, "name", "n", "", "Name of the caption track",
67+
)
68+
insertCmd.Flags().StringVarP(
69+
&trackKind, "trackKind", "t", "standard", "standard, ASR or forced",
70+
)
5971
insertCmd.Flags().StringVarP(&videoId, "videoId", "v", "", "ID of the video")
6072
insertCmd.Flags().StringVarP(&onBehalfOf, "onBehalfOf", "b", "", "")
61-
insertCmd.Flags().StringVarP(&onBehalfOfContentOwner, "onBehalfOfContentOwner", "B", "", "")
62-
insertCmd.Flags().StringVarP(&output, "output", "o", "", "json, yaml or silent")
73+
insertCmd.Flags().StringVarP(
74+
&onBehalfOfContentOwner, "onBehalfOfContentOwner", "B", "", "",
75+
)
76+
insertCmd.Flags().StringVarP(
77+
&output, "output", "o", "", "json, yaml or silent",
78+
)
6379
}

cmd/caption/list.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ func init() {
2727
listCmd.Flags().StringVarP(&id, "id", "i", "", "ID of the caption")
2828
listCmd.Flags().StringVarP(&videoId, "videoId", "v", "", "ID of the video")
2929
listCmd.Flags().StringVarP(&onBehalfOf, "onBehalfOf", "b", "", "")
30-
listCmd.Flags().StringVarP(&onBehalfOfContentOwner, "onBehalfOfContentOwner", "B", "", "")
31-
listCmd.Flags().StringArrayVarP(&parts, "parts", "p", []string{"id", "snippet"}, "Comma separated parts")
30+
listCmd.Flags().StringVarP(
31+
&onBehalfOfContentOwner, "onBehalfOfContentOwner", "B", "", "",
32+
)
33+
listCmd.Flags().StringArrayVarP(
34+
&parts, "parts", "p", []string{"id", "snippet"}, "Comma separated parts",
35+
)
3236
listCmd.Flags().StringVarP(&output, "output", "o", "", "json or yaml")
3337
}

0 commit comments

Comments
 (0)