diff --git a/pkg/activity/activity.go b/pkg/activity/activity.go index 1634298..1fad5ee 100644 --- a/pkg/activity/activity.go +++ b/pkg/activity/activity.go @@ -46,7 +46,9 @@ func NewActivity(opts ...Option) IActivity[youtube.Activity] { } func (a *Activity) Get() ([]*youtube.Activity, error) { - a.EnsureService() + if err := a.EnsureService(); err != nil { + return nil, err + } call := a.Service.Activities.List(a.Parts) if a.ChannelId != "" { call = call.ChannelId(a.ChannelId) diff --git a/pkg/caption/caption.go b/pkg/caption/caption.go index 1e6eeb2..8ccce3c 100644 --- a/pkg/caption/caption.go +++ b/pkg/caption/caption.go @@ -69,7 +69,9 @@ func (c *Caption) GetFields() *common.Fields { } func (c *Caption) Get() ([]*youtube.Caption, error) { - c.EnsureService() + if err := c.EnsureService(); err != nil { + return nil, err + } call := c.Service.Captions.List(c.Parts, c.VideoId) if len(c.Ids) > 0 { call = call.Id(c.Ids...) @@ -119,7 +121,9 @@ func (c *Caption) List(writer io.Writer) error { } func (c *Caption) Insert(writer io.Writer) error { - c.EnsureService() + if err := c.EnsureService(); err != nil { + return err + } file, err := pkg.Root.Open(c.File) if err != nil { return errors.Join(errInsertCaption, err) @@ -246,7 +250,9 @@ func (c *Caption) Update(writer io.Writer) error { } func (c *Caption) Delete(writer io.Writer) error { - c.EnsureService() + if err := c.EnsureService(); err != nil { + return err + } for _, id := range c.Ids { call := c.Service.Captions.Delete(id) if c.OnBehalfOf != "" { @@ -267,7 +273,9 @@ func (c *Caption) Delete(writer io.Writer) error { } func (c *Caption) Download(writer io.Writer) error { - c.EnsureService() + if err := c.EnsureService(); err != nil { + return err + } call := c.Service.Captions.Download(c.Ids[0]) if c.Tfmt != "" { call = call.Tfmt(c.Tfmt) diff --git a/pkg/channel/channel.go b/pkg/channel/channel.go index 6f33f5d..636ceb3 100644 --- a/pkg/channel/channel.go +++ b/pkg/channel/channel.go @@ -59,7 +59,9 @@ func NewChannel(opts ...Option) IChannel[youtube.Channel] { } func (c *Channel) Get() ([]*youtube.Channel, error) { - c.EnsureService() + if err := c.EnsureService(); err != nil { + return nil, err + } call := c.Service.Channels.List(c.Parts) if c.CategoryId != "" { call = call.CategoryId(c.CategoryId) diff --git a/pkg/channelBanner/channelBanner.go b/pkg/channelBanner/channelBanner.go index bcecd95..649fef0 100644 --- a/pkg/channelBanner/channelBanner.go +++ b/pkg/channelBanner/channelBanner.go @@ -43,7 +43,9 @@ func NewChannelBanner(opts ...Option) IChannelBanner { } func (cb *ChannelBanner) Insert(writer io.Writer) error { - cb.EnsureService() + if err := cb.EnsureService(); err != nil { + return err + } file, err := pkg.Root.Open(cb.File) if err != nil { return errors.Join(errInsertChannelBanner, err) diff --git a/pkg/channelSection/channelSection.go b/pkg/channelSection/channelSection.go index 9fb72a4..8abeca0 100644 --- a/pkg/channelSection/channelSection.go +++ b/pkg/channelSection/channelSection.go @@ -55,7 +55,9 @@ func (cs *ChannelSection) GetFields() *common.Fields { func (cs *ChannelSection) Get() ( []*youtube.ChannelSection, error, ) { - cs.EnsureService() + if err := cs.EnsureService(); err != nil { + return nil, err + } call := cs.Service.ChannelSections.List(cs.Parts) if len(cs.Ids) > 0 { call = call.Id(cs.Ids...) @@ -105,7 +107,9 @@ func (cs *ChannelSection) List(writer io.Writer) error { } func (cs *ChannelSection) Delete(writer io.Writer) error { - cs.EnsureService() + if err := cs.EnsureService(); err != nil { + return err + } for _, id := range cs.Ids { call := cs.Service.ChannelSections.Delete(id) if cs.OnBehalfOfContentOwner != "" { diff --git a/pkg/comment/comment.go b/pkg/comment/comment.go index 81a0e77..c7dafae 100644 --- a/pkg/comment/comment.go +++ b/pkg/comment/comment.go @@ -62,7 +62,9 @@ func NewComment(opts ...Option) IComment[youtube.Comment] { } func (c *Comment) Get() ([]*youtube.Comment, error) { - c.EnsureService() + if err := c.EnsureService(); err != nil { + return nil, err + } call := c.Service.Comments.List(c.Parts) if len(c.Ids) > 0 && c.Ids[0] != "" { call = call.Id(c.Ids...) @@ -135,7 +137,9 @@ func (c *Comment) List(writer io.Writer) error { } func (c *Comment) Insert(writer io.Writer) error { - c.EnsureService() + if err := c.EnsureService(); err != nil { + return err + } comment := &youtube.Comment{ Snippet: &youtube.CommentSnippet{ AuthorChannelId: &youtube.CommentSnippetAuthorChannelId{ @@ -171,7 +175,9 @@ func (c *Comment) Insert(writer io.Writer) error { } func (c *Comment) Update(writer io.Writer) error { - c.EnsureService() + if err := c.EnsureService(); err != nil { + return err + } c.Parts = []string{"id", "snippet"} comments, err := c.Get() @@ -214,7 +220,9 @@ func (c *Comment) Update(writer io.Writer) error { } func (c *Comment) MarkAsSpam(writer io.Writer) error { - c.EnsureService() + if err := c.EnsureService(); err != nil { + return err + } call := c.Service.Comments.MarkAsSpam(c.Ids) err := call.Do() if err != nil { @@ -234,7 +242,9 @@ func (c *Comment) MarkAsSpam(writer io.Writer) error { } func (c *Comment) SetModerationStatus(writer io.Writer) error { - c.EnsureService() + if err := c.EnsureService(); err != nil { + return err + } call := c.Service.Comments.SetModerationStatus(c.Ids, c.ModerationStatus) if c.BanAuthor != nil { @@ -262,7 +272,9 @@ func (c *Comment) SetModerationStatus(writer io.Writer) error { } func (c *Comment) Delete(writer io.Writer) error { - c.EnsureService() + if err := c.EnsureService(); err != nil { + return err + } for _, id := range c.Ids { call := c.Service.Comments.Delete(id) err := call.Do() diff --git a/pkg/commentThread/commentThread.go b/pkg/commentThread/commentThread.go index 4fe1110..54743ab 100644 --- a/pkg/commentThread/commentThread.go +++ b/pkg/commentThread/commentThread.go @@ -54,7 +54,9 @@ func NewCommentThread(opts ...Option) ICommentThread[youtube.CommentThread] { } func (c *CommentThread) Get() ([]*youtube.CommentThread, error) { - c.EnsureService() + if err := c.EnsureService(); err != nil { + return nil, err + } call := c.Service.CommentThreads.List(c.Parts) if len(c.Ids) > 0 { call = call.Id(c.Ids...) @@ -136,7 +138,9 @@ func (c *CommentThread) List(writer io.Writer) error { } func (c *CommentThread) Insert(writer io.Writer) error { - c.EnsureService() + if err := c.EnsureService(); err != nil { + return err + } ct := &youtube.CommentThread{ Snippet: &youtube.CommentThreadSnippet{ ChannelId: c.ChannelId, diff --git a/pkg/common/common.go b/pkg/common/common.go index b70d1f5..8988ed4 100644 --- a/pkg/common/common.go +++ b/pkg/common/common.go @@ -21,22 +21,23 @@ func (d *Fields) GetFields() *Fields { return d } -func (d *Fields) EnsureService() { +func (d *Fields) EnsureService() error { if d.Service == nil { svc, err := auth.NewY2BService( auth.WithCredential("", pkg.Root.FS()), auth.WithCacheToken("", pkg.Root.FS()), ).GetService() if err != nil { - panic(fmt.Sprintf("failed to create YouTube service: %v", err)) + return fmt.Errorf("failed to create YouTube service: %w", err) } d.Service = svc } + return nil } type HasFields interface { GetFields() *Fields - EnsureService() + EnsureService() error } func WithParts[T HasFields](parts []string) func(T) { diff --git a/pkg/i18nLanguage/i18nLanguage.go b/pkg/i18nLanguage/i18nLanguage.go index 9fa1984..e617d7c 100644 --- a/pkg/i18nLanguage/i18nLanguage.go +++ b/pkg/i18nLanguage/i18nLanguage.go @@ -41,7 +41,9 @@ func NewI18nLanguage(opts ...Option) II18nLanguage[youtube.I18nLanguage] { func (i *I18nLanguage) Get() ( []*youtube.I18nLanguage, error, ) { - i.EnsureService() + if err := i.EnsureService(); err != nil { + return nil, err + } call := i.Service.I18nLanguages.List(i.Parts) if i.Hl != "" { call = call.Hl(i.Hl) diff --git a/pkg/i18nRegion/i18nRegion.go b/pkg/i18nRegion/i18nRegion.go index 95c1ee0..5fed114 100644 --- a/pkg/i18nRegion/i18nRegion.go +++ b/pkg/i18nRegion/i18nRegion.go @@ -39,7 +39,9 @@ func NewI18nRegion(opts ...Option) II18nRegion[youtube.I18nRegion] { } func (i *I18nRegion) Get() ([]*youtube.I18nRegion, error) { - i.EnsureService() + if err := i.EnsureService(); err != nil { + return nil, err + } call := i.Service.I18nRegions.List(i.Parts) if i.Hl != "" { call = call.Hl(i.Hl) diff --git a/pkg/member/member.go b/pkg/member/member.go index 5c1a1e1..eb0fbdf 100644 --- a/pkg/member/member.go +++ b/pkg/member/member.go @@ -43,7 +43,9 @@ func NewMember(opts ...Option) IMember[youtube.Member] { } func (m *Member) Get() ([]*youtube.Member, error) { - m.EnsureService() + if err := m.EnsureService(); err != nil { + return nil, err + } call := m.Service.Members.List(m.Parts) if m.MemberChannelId != "" { call = call.FilterByMemberChannelId(m.MemberChannelId) diff --git a/pkg/membershipsLevel/membershipsLevel.go b/pkg/membershipsLevel/membershipsLevel.go index 720f632..871c7c2 100644 --- a/pkg/membershipsLevel/membershipsLevel.go +++ b/pkg/membershipsLevel/membershipsLevel.go @@ -42,7 +42,9 @@ func (m *MembershipsLevel) GetFields() *common.Fields { } func (m *MembershipsLevel) Get() ([]*youtube.MembershipsLevel, error) { - m.EnsureService() + if err := m.EnsureService(); err != nil { + return nil, err + } call := m.Service.MembershipsLevels.List(m.Parts) res, err := call.Do() if err != nil { diff --git a/pkg/playlist/playlist.go b/pkg/playlist/playlist.go index 6df63d2..67157b4 100644 --- a/pkg/playlist/playlist.go +++ b/pkg/playlist/playlist.go @@ -59,7 +59,9 @@ func NewPlaylist(opts ...Option) IPlaylist[youtube.Playlist] { } func (p *Playlist) Get() ([]*youtube.Playlist, error) { - p.EnsureService() + if err := p.EnsureService(); err != nil { + return nil, err + } call := p.Service.Playlists.List(p.Parts) if len(p.Ids) > 0 { @@ -136,7 +138,9 @@ func (p *Playlist) List(writer io.Writer) error { } func (p *Playlist) Insert(writer io.Writer) error { - p.EnsureService() + if err := p.EnsureService(); err != nil { + return err + } upload := &youtube.Playlist{ Snippet: &youtube.PlaylistSnippet{ Title: p.Title, @@ -175,7 +179,9 @@ func (p *Playlist) Insert(writer io.Writer) error { } func (p *Playlist) Update(writer io.Writer) error { - p.EnsureService() + if err := p.EnsureService(); err != nil { + return err + } playlists, err := p.Get() if err != nil { return errors.Join(errUpdatePlaylist, err) @@ -223,7 +229,9 @@ func (p *Playlist) Update(writer io.Writer) error { } func (p *Playlist) Delete(writer io.Writer) error { - p.EnsureService() + if err := p.EnsureService(); err != nil { + return err + } for _, id := range p.Ids { call := p.Service.Playlists.Delete(id) if p.OnBehalfOfContentOwner != "" { diff --git a/pkg/playlistImage/playlistImage.go b/pkg/playlistImage/playlistImage.go index e0c13b6..84be5fe 100644 --- a/pkg/playlistImage/playlistImage.go +++ b/pkg/playlistImage/playlistImage.go @@ -58,7 +58,9 @@ func NewPlaylistImage(opts ...Option) IPlaylistImage[youtube.PlaylistImage] { } func (pi *PlaylistImage) Get() ([]*youtube.PlaylistImage, error) { - pi.EnsureService() + if err := pi.EnsureService(); err != nil { + return nil, err + } call := pi.Service.PlaylistImages.List() call = call.Part(pi.Parts...) if pi.Parent != "" { @@ -122,7 +124,9 @@ func (pi *PlaylistImage) List(writer io.Writer) error { } func (pi *PlaylistImage) Insert(writer io.Writer) error { - pi.EnsureService() + if err := pi.EnsureService(); err != nil { + return err + } file, err := pkg.Root.Open(pi.File) if err != nil { return errors.Join(errInsertPlaylistImage, err) @@ -168,7 +172,9 @@ func (pi *PlaylistImage) Insert(writer io.Writer) error { } func (pi *PlaylistImage) Update(writer io.Writer) error { - pi.EnsureService() + if err := pi.EnsureService(); err != nil { + return err + } pi.Parts = []string{"id", "kind", "snippet"} playlistImages, err := pi.Get() if err != nil { @@ -226,7 +232,9 @@ func (pi *PlaylistImage) Update(writer io.Writer) error { } func (pi *PlaylistImage) Delete(writer io.Writer) error { - pi.EnsureService() + if err := pi.EnsureService(); err != nil { + return err + } for _, id := range pi.Ids { call := pi.Service.PlaylistImages.Delete() call = call.Id(id) diff --git a/pkg/playlistItem/playlistItem.go b/pkg/playlistItem/playlistItem.go index 992baa5..4c1c970 100644 --- a/pkg/playlistItem/playlistItem.go +++ b/pkg/playlistItem/playlistItem.go @@ -60,7 +60,9 @@ func NewPlaylistItem(opts ...Option) IPlaylistItem[youtube.PlaylistItem] { } func (pi *PlaylistItem) Get() ([]*youtube.PlaylistItem, error) { - pi.EnsureService() + if err := pi.EnsureService(); err != nil { + return nil, err + } call := pi.Service.PlaylistItems.List(pi.Parts) if len(pi.Ids) > 0 { call = call.Id(pi.Ids...) @@ -145,7 +147,9 @@ func (pi *PlaylistItem) List(writer io.Writer) error { } func (pi *PlaylistItem) Insert(writer io.Writer) error { - pi.EnsureService() + if err := pi.EnsureService(); err != nil { + return err + } var resourceId *youtube.ResourceId switch pi.Kind { case "video": @@ -203,7 +207,9 @@ func (pi *PlaylistItem) Insert(writer io.Writer) error { } func (pi *PlaylistItem) Update(writer io.Writer) error { - pi.EnsureService() + if err := pi.EnsureService(); err != nil { + return err + } pi.Parts = []string{"id", "snippet", "status"} playlistItems, err := pi.Get() @@ -250,7 +256,9 @@ func (pi *PlaylistItem) Update(writer io.Writer) error { } func (pi *PlaylistItem) Delete(writer io.Writer) error { - pi.EnsureService() + if err := pi.EnsureService(); err != nil { + return err + } for _, id := range pi.Ids { call := pi.Service.PlaylistItems.Delete(id) if pi.OnBehalfOfContentOwner != "" { diff --git a/pkg/search/search.go b/pkg/search/search.go index 6ae92a4..2f3ddf3 100644 --- a/pkg/search/search.go +++ b/pkg/search/search.go @@ -68,7 +68,9 @@ func NewSearch(opts ...Option) ISearch[youtube.SearchResult] { } func (s *Search) Get() ([]*youtube.SearchResult, error) { - s.EnsureService() + if err := s.EnsureService(); err != nil { + return nil, err + } call := s.Service.Search.List(s.Parts) if s.ChannelId != "" { call = call.ChannelId(s.ChannelId) diff --git a/pkg/subscription/subscription.go b/pkg/subscription/subscription.go index 40ca69a..ca53bc6 100644 --- a/pkg/subscription/subscription.go +++ b/pkg/subscription/subscription.go @@ -58,7 +58,9 @@ func NewSubscription(opts ...Option) ISubscription[youtube.Subscription] { } func (s *Subscription) Get() ([]*youtube.Subscription, error) { - s.EnsureService() + if err := s.EnsureService(); err != nil { + return nil, err + } call := s.Service.Subscriptions.List(s.Parts) if len(s.Ids) > 0 { call = call.Id(s.Ids...) @@ -150,7 +152,9 @@ func (s *Subscription) List(writer io.Writer) error { } func (s *Subscription) Insert(writer io.Writer) error { - s.EnsureService() + if err := s.EnsureService(); err != nil { + return err + } subscription := &youtube.Subscription{ Snippet: &youtube.SubscriptionSnippet{ ChannelId: s.SubscriberChannelId, @@ -180,7 +184,9 @@ func (s *Subscription) Insert(writer io.Writer) error { } func (s *Subscription) Delete(writer io.Writer) error { - s.EnsureService() + if err := s.EnsureService(); err != nil { + return err + } for _, id := range s.Ids { call := s.Service.Subscriptions.Delete(id) err := call.Do() diff --git a/pkg/superChatEvent/superChatEvent.go b/pkg/superChatEvent/superChatEvent.go index c02985c..c9de299 100644 --- a/pkg/superChatEvent/superChatEvent.go +++ b/pkg/superChatEvent/superChatEvent.go @@ -41,7 +41,9 @@ func NewSuperChatEvent(opts ...Option) ISuperChatEvent[youtube.SuperChatEvent] { } func (s *SuperChatEvent) Get() ([]*youtube.SuperChatEvent, error) { - s.EnsureService() + if err := s.EnsureService(); err != nil { + return nil, err + } call := s.Service.SuperChatEvents.List(s.Parts) if s.Hl != "" { call = call.Hl(s.Hl) diff --git a/pkg/thumbnail/thumbnail.go b/pkg/thumbnail/thumbnail.go index 3ac0d7c..5d5fcfd 100644 --- a/pkg/thumbnail/thumbnail.go +++ b/pkg/thumbnail/thumbnail.go @@ -38,7 +38,9 @@ func NewThumbnail(opts ...Option) IThumbnail { } func (t *Thumbnail) Set(writer io.Writer) error { - t.EnsureService() + if err := t.EnsureService(); err != nil { + return err + } file, err := pkg.Root.Open(t.File) if err != nil { return errors.Join(errSetThumbnail, err) diff --git a/pkg/video/video.go b/pkg/video/video.go index c08a8ae..10104de 100644 --- a/pkg/video/video.go +++ b/pkg/video/video.go @@ -90,7 +90,9 @@ func NewVideo(opts ...Option) IVideo[youtube.Video] { } func (v *Video) Get() ([]*youtube.Video, error) { - v.EnsureService() + if err := v.EnsureService(); err != nil { + return nil, err + } call := v.Service.Videos.List(v.Parts) if len(v.Ids) > 0 { call = call.Id(v.Ids...) @@ -188,7 +190,9 @@ func (v *Video) List(writer io.Writer) error { } func (v *Video) Insert(writer io.Writer) error { - v.EnsureService() + if err := v.EnsureService(); err != nil { + return err + } file, err := pkg.Root.Open(v.File) if err != nil { return errors.Join(errInsertVideo, err) @@ -295,7 +299,9 @@ func (v *Video) Insert(writer io.Writer) error { } func (v *Video) Update(writer io.Writer) error { - v.EnsureService() + if err := v.EnsureService(); err != nil { + return err + } v.Parts = []string{"id", "snippet", "status"} videos, err := v.Get() @@ -385,7 +391,9 @@ func (v *Video) Update(writer io.Writer) error { } func (v *Video) Rate(writer io.Writer) error { - v.EnsureService() + if err := v.EnsureService(); err != nil { + return err + } for _, id := range v.Ids { call := v.Service.Videos.Rate(id, v.Rating) err := call.Do() @@ -398,7 +406,9 @@ func (v *Video) Rate(writer io.Writer) error { } func (v *Video) GetRating(writer io.Writer) error { - v.EnsureService() + if err := v.EnsureService(); err != nil { + return err + } call := v.Service.Videos.GetRating(v.Ids) if v.OnBehalfOfContentOwner != "" { call = call.OnBehalfOfContentOwner(v.OnBehalfOfContentOwner) @@ -427,7 +437,9 @@ func (v *Video) GetRating(writer io.Writer) error { } func (v *Video) Delete(writer io.Writer) error { - v.EnsureService() + if err := v.EnsureService(); err != nil { + return err + } for _, id := range v.Ids { call := v.Service.Videos.Delete(id) if v.OnBehalfOfContentOwner != "" { @@ -444,7 +456,9 @@ func (v *Video) Delete(writer io.Writer) error { } func (v *Video) ReportAbuse(writer io.Writer) error { - v.EnsureService() + if err := v.EnsureService(); err != nil { + return err + } for _, id := range v.Ids { videoAbuseReport := &youtube.VideoAbuseReport{ Comments: v.Comments, diff --git a/pkg/videoAbuseReportReason/videoAbuseReportReason.go b/pkg/videoAbuseReportReason/videoAbuseReportReason.go index 21c3b50..5e6c98e 100644 --- a/pkg/videoAbuseReportReason/videoAbuseReportReason.go +++ b/pkg/videoAbuseReportReason/videoAbuseReportReason.go @@ -41,7 +41,9 @@ func NewVideoAbuseReportReason(opt ...Option) IVideoAbuseReportReason[youtube.Vi func (va *VideoAbuseReportReason) Get() ( []*youtube.VideoAbuseReportReason, error, ) { - va.EnsureService() + if err := va.EnsureService(); err != nil { + return nil, err + } call := va.Service.VideoAbuseReportReasons.List(va.Parts) if va.Hl != "" { call = call.Hl(va.Hl) diff --git a/pkg/videoCategory/videoCategory.go b/pkg/videoCategory/videoCategory.go index 57f9834..b894ef7 100644 --- a/pkg/videoCategory/videoCategory.go +++ b/pkg/videoCategory/videoCategory.go @@ -41,7 +41,9 @@ func NewVideoCategory(opt ...Option) IVideoCategory[youtube.VideoCategory] { } func (vc *VideoCategory) Get() ([]*youtube.VideoCategory, error) { - vc.EnsureService() + if err := vc.EnsureService(); err != nil { + return nil, err + } call := vc.Service.VideoCategories.List(vc.Parts) if len(vc.Ids) > 0 { call = call.Id(vc.Ids...) diff --git a/pkg/watermark/watermark.go b/pkg/watermark/watermark.go index de5e4f8..daacf92 100644 --- a/pkg/watermark/watermark.go +++ b/pkg/watermark/watermark.go @@ -47,7 +47,9 @@ func NewWatermark(opts ...Option) IWatermark { } func (w *Watermark) Set(writer io.Writer) error { - w.EnsureService() + if err := w.EnsureService(); err != nil { + return err + } file, err := pkg.Root.Open(w.File) if err != nil { return errors.Join(errSetWatermark, err) @@ -89,7 +91,9 @@ func (w *Watermark) Set(writer io.Writer) error { } func (w *Watermark) Unset(writer io.Writer) error { - w.EnsureService() + if err := w.EnsureService(); err != nil { + return err + } call := w.Service.Watermarks.Unset(w.ChannelId) if w.OnBehalfOfContentOwner != "" { call = call.OnBehalfOfContentOwner(w.OnBehalfOfContentOwner)