Skip to content

Commit 50fd850

Browse files
committed
🥅 Improve err handling in caption
1 parent ad4a29d commit 50fd850

File tree

7 files changed

+87
-53
lines changed

7 files changed

+87
-53
lines changed

BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ gazelle(name = "gazelle")
55

66
nogo(
77
name = "yutu_nogo",
8+
vet = True,
89
visibility = ["//visibility:public"],
9-
deps = TOOLS_NOGO,
10+
# deps = TOOLS_NOGO,
1011
)
1112

1213
go_library(

cmd/caption/delete.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ var deleteCmd = &cobra.Command{
2222
caption.WithOnBehalfOfContentOwner(onBehalfOfContentOwner),
2323
caption.WithService(nil),
2424
)
25-
c.Delete()
25+
26+
err := c.Delete(cmd.OutOrStdout())
27+
if err != nil {
28+
_ = cmd.Help()
29+
cmd.PrintErrf("Error: %v\n", err)
30+
}
2631
},
2732
}
2833

cmd/caption/download.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ var downloadCmd = &cobra.Command{
2525
caption.WithOnBehalfOfContentOwner(onBehalfOfContentOwner),
2626
caption.WithService(nil),
2727
)
28-
c.Download()
28+
29+
err := c.Download(cmd.OutOrStdout())
30+
if err != nil {
31+
_ = cmd.Help()
32+
cmd.PrintErrf("Error: %s\n", err.Error())
33+
}
2934
},
3035
}
3136

cmd/caption/insert.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ var insertCmd = &cobra.Command{
3232
caption.WithVideoId(videoId),
3333
caption.WithService(nil),
3434
)
35-
c.Insert(output)
35+
36+
err := c.Insert(output, cmd.OutOrStdout())
37+
if err != nil {
38+
_ = cmd.Help()
39+
cmd.PrintErrf("Error: %s\n", err.Error())
40+
}
3641
},
3742
}
3843

cmd/caption/list.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ var listCmd = &cobra.Command{
2424
caption.WithOnBehalfOfContentOwner(onBehalfOfContentOwner),
2525
caption.WithService(nil),
2626
)
27-
c.List(parts, output)
27+
28+
err := c.List(parts, output, cmd.OutOrStdout())
29+
if err != nil {
30+
_ = cmd.Help()
31+
cmd.PrintErrf("Error: %s\n", err.Error())
32+
}
2833
},
2934
}
3035

cmd/caption/update.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ var updateCmd = &cobra.Command{
3232
caption.WithVideoId(videoId),
3333
caption.WithService(nil),
3434
)
35-
c.Update(output)
35+
36+
err := c.Update(output, cmd.OutOrStdout())
37+
if err != nil {
38+
_ = cmd.Help()
39+
cmd.PrintErrf("Error: %s\n", err.Error())
40+
}
3641
},
3742
}
3843

pkg/caption/caption.go

Lines changed: 55 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/eat-pray-ai/yutu/pkg/utils"
88
"google.golang.org/api/youtube/v3"
99
"io"
10-
"log"
1110
"os"
1211
)
1312

@@ -40,12 +39,12 @@ type caption struct {
4039
}
4140

4241
type Caption interface {
43-
get(parts []string) []*youtube.Caption // todo: return error
44-
List(parts []string, output string)
45-
Insert(output string)
46-
Update(output string)
47-
Delete()
48-
Download()
42+
get(parts []string) ([]*youtube.Caption, error) // todo: return error
43+
List(parts []string, output string, writer io.Writer) error
44+
Insert(output string, writer io.Writer) error
45+
Update(output string, writer io.Writer) error
46+
Delete(writer io.Writer) error
47+
Download(writer io.Writer) error
4948
}
5049

5150
type Option func(*caption)
@@ -58,7 +57,7 @@ func NewCation(opts ...Option) Caption {
5857
return c
5958
}
6059

61-
func (c *caption) get(parts []string) []*youtube.Caption {
60+
func (c *caption) get(parts []string) ([]*youtube.Caption, error) {
6261
call := service.Captions.List(parts, c.VideoId)
6362
if len(c.IDs) > 0 {
6463
call = call.Id(c.IDs...)
@@ -72,33 +71,36 @@ func (c *caption) get(parts []string) []*youtube.Caption {
7271

7372
res, err := call.Do()
7473
if err != nil {
75-
utils.PrintJSON(c, nil)
76-
log.Fatalln(errors.Join(errGetCaption, err))
74+
return nil, errors.Join(errGetCaption, err)
7775
}
7876

79-
return res.Items
77+
return res.Items, nil
8078
}
8179

82-
func (c *caption) List(parts []string, output string) {
83-
captions := c.get(parts)
80+
func (c *caption) List(parts []string, output string, writer io.Writer) error {
81+
captions, err := c.get(parts)
82+
if err != nil {
83+
return err
84+
}
85+
8486
switch output {
8587
case "json":
86-
utils.PrintJSON(captions, nil)
88+
utils.PrintJSON(captions, writer)
8789
case "yaml":
88-
utils.PrintYAML(captions, nil)
90+
utils.PrintYAML(captions, writer)
8991
default:
90-
fmt.Println("ID\tName")
92+
_, _ = fmt.Fprintln(writer, "ID\tName")
9193
for _, caption := range captions {
92-
fmt.Printf("%s\t%s\n", caption.Id, caption.Snippet.Name)
94+
_, _ = fmt.Fprintf(writer, "%s\t%s\n", caption.Id, caption.Snippet.Name)
9395
}
9496
}
97+
return nil
9598
}
9699

97-
func (c *caption) Insert(output string) {
100+
func (c *caption) Insert(output string, writer io.Writer) error {
98101
file, err := os.Open(c.File)
99102
if err != nil {
100-
utils.PrintJSON(c, nil)
101-
log.Fatalln(errors.Join(errInsertCaption, err))
103+
return errors.Join(errInsertCaption, err)
102104
}
103105
defer file.Close()
104106

@@ -127,23 +129,31 @@ func (c *caption) Insert(output string) {
127129

128130
res, err := call.Do()
129131
if err != nil {
130-
utils.PrintJSON(c, nil)
131-
log.Fatalln(errors.Join(errInsertCaption, err))
132+
return errors.Join(errInsertCaption, err)
132133
}
133134

134135
switch output {
135136
case "json":
136-
utils.PrintJSON(res, nil)
137+
utils.PrintJSON(res, writer)
137138
case "yaml":
138-
utils.PrintYAML(res, nil)
139+
utils.PrintYAML(res, writer)
139140
case "silent":
140141
default:
141-
fmt.Printf("Caption inserted: %s\n", res.Id)
142+
_, _ = fmt.Fprintf(writer, "Caption inserted: %s\n", res.Id)
142143
}
144+
return nil
143145
}
144146

145-
func (c *caption) Update(output string) {
146-
caption := c.get([]string{"snippet"})[0]
147+
func (c *caption) Update(output string, writer io.Writer) error {
148+
captions, err := c.get([]string{"snippet"})
149+
if err != nil {
150+
return errors.Join(errUpdateCaption, err)
151+
}
152+
if len(captions) == 0 {
153+
return errGetCaption
154+
}
155+
156+
caption := captions[0]
147157
if c.AudioTrackType != "" {
148158
caption.Snippet.AudioTrackType = c.AudioTrackType
149159
}
@@ -179,8 +189,7 @@ func (c *caption) Update(output string) {
179189
if c.File != "" {
180190
file, err := os.Open(c.File)
181191
if err != nil {
182-
utils.PrintJSON(c, nil)
183-
log.Fatalln(errors.Join(errUpdateCaption, err))
192+
return errors.Join(errUpdateCaption, err)
184193
}
185194
defer file.Close()
186195
call = call.Media(file)
@@ -194,22 +203,22 @@ func (c *caption) Update(output string) {
194203

195204
res, err := call.Do()
196205
if err != nil {
197-
utils.PrintJSON(c, nil)
198-
log.Fatalln(errors.Join(errUpdateCaption, err))
206+
return errors.Join(errUpdateCaption, err)
199207
}
200208

201209
switch output {
202210
case "json":
203-
utils.PrintJSON(res, nil)
211+
utils.PrintJSON(res, writer)
204212
case "yaml":
205-
utils.PrintYAML(res, nil)
213+
utils.PrintYAML(res, writer)
206214
case "silent":
207215
default:
208-
fmt.Printf("Caption updated: %s\n", res.Id)
216+
_, _ = fmt.Fprintf(writer, "Caption updated: %s\n", res.Id)
209217
}
218+
return nil
210219
}
211220

212-
func (c *caption) Delete() {
221+
func (c *caption) Delete(writer io.Writer) error {
213222
for _, id := range c.IDs {
214223
call := service.Captions.Delete(id)
215224
if c.OnBehalfOf != "" {
@@ -221,15 +230,15 @@ func (c *caption) Delete() {
221230

222231
err := call.Do()
223232
if err != nil {
224-
utils.PrintJSON(c, nil)
225-
log.Fatalln(errors.Join(errDeleteCaption, err))
233+
return errors.Join(errDeleteCaption, err)
226234
}
227235

228-
fmt.Printf("Caption %s deleted\n", id)
236+
_, _ = fmt.Fprintf(writer, "Caption %s deleted\n", id)
229237
}
238+
return nil
230239
}
231240

232-
func (c *caption) Download() {
241+
func (c *caption) Download(writer io.Writer) error {
233242
call := service.Captions.Download(c.IDs[0])
234243
if c.Tfmt != "" {
235244
call = call.Tfmt(c.Tfmt)
@@ -246,29 +255,28 @@ func (c *caption) Download() {
246255

247256
res, err := call.Download()
248257
if err != nil {
249-
utils.PrintJSON(c, nil)
250-
log.Fatalln(errors.Join(errDownloadCaption, err))
258+
return errors.Join(errDownloadCaption, err)
251259
}
252260
defer res.Body.Close()
261+
253262
body, err := io.ReadAll(res.Body)
254263
if err != nil {
255-
utils.PrintJSON(c, nil)
256-
log.Fatalln(errors.Join(errDownloadCaption, err))
264+
return errors.Join(errDownloadCaption, err)
257265
}
258266

259267
file, err := os.Create(c.File)
260268
if err != nil {
261-
utils.PrintJSON(c, nil)
262-
log.Fatalln(errors.Join(errDownloadCaption, err))
269+
return errors.Join(errDownloadCaption, err)
263270
}
264271
defer file.Close()
272+
265273
_, err = file.Write(body)
266274
if err != nil {
267-
utils.PrintJSON(c, nil)
268-
log.Fatalln(errors.Join(errDownloadCaption, err))
275+
return errors.Join(errDownloadCaption, err)
269276
}
270277

271278
fmt.Printf("Caption %s downloaded to %s\n", c.IDs[0], c.File)
279+
return nil
272280
}
273281

274282
func WithIDs(ids []string) Option {

0 commit comments

Comments
 (0)