Skip to content

Commit

Permalink
lint: err shadowing
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Mar 22, 2022
1 parent ef483a7 commit 2a8eb16
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
30 changes: 15 additions & 15 deletions app/youtube/service.go
Expand Up @@ -94,8 +94,8 @@ func (s *Service) RSSFeed(cinfo ChannelInfo) (string, error) {
fileURL := s.RootURL + "/" + path.Base(entry.File)

var fileSize int
if fileInfo, err := os.Stat(entry.File); err != nil {
log.Printf("[WARN] failed to get file size for %s: %v", entry.File, err)
if fileInfo, fiErr := os.Stat(entry.File); fiErr != nil {
log.Printf("[WARN] failed to get file size for %s: %v", entry.File, fiErr)
} else {
fileSize = int(fileInfo.Size())
}
Expand Down Expand Up @@ -146,24 +146,24 @@ func (s *Service) procChannels(ctx context.Context) error {
if i >= s.KeepPerChannel {
break
}
exists, err := s.Store.Exist(entry)
exists, exErr := s.Store.Exist(entry)
if err != nil {
return errors.Wrapf(err, "failed to check if entry %s exists", entry.VideoID)
return errors.Wrapf(exErr, "failed to check if entry %s exists", entry.VideoID)
}
if exists {
continue
}
log.Printf("[INFO] new entry %s, %s, %s", entry.VideoID, entry.Title, chanInfo.Name)
file, err := s.Downloader.Get(ctx, entry.VideoID, uuid.New().String())
if err != nil {
log.Printf("[WARN] failed to download %s: %s", entry.VideoID, err)
file, downErr := s.Downloader.Get(ctx, entry.VideoID, uuid.New().String())
if downErr != nil {
log.Printf("[WARN] failed to download %s: %s", entry.VideoID, downErr)
continue
}
log.Printf("[DEBUG] downloaded %s (%s) to %s, channel: %+v", entry.VideoID, entry.Title, file, chanInfo)
entry.File = file
ok, err := s.Store.Save(entry)
if err != nil {
return errors.Wrapf(err, "failed to save entry %+v", entry)
ok, saveErr := s.Store.Save(entry)
if saveErr != nil {
return errors.Wrapf(saveErr, "failed to save entry %+v", entry)
}
if !ok {
log.Printf("[WARN] attempt to save dup entry %+v", entry)
Expand All @@ -172,13 +172,13 @@ func (s *Service) procChannels(ctx context.Context) error {
}

// remove old entries and files
files, err := s.Store.RemoveOld(chanInfo.ID, s.KeepPerChannel)
if err != nil {
return errors.Wrapf(err, "failed to remove old meta data for %s", chanInfo.ID)
files, rmErr := s.Store.RemoveOld(chanInfo.ID, s.KeepPerChannel)
if rmErr != nil {
return errors.Wrapf(rmErr, "failed to remove old meta data for %s", chanInfo.ID)
}
for _, f := range files {
if err := os.Remove(f); err != nil {
log.Printf("[WARN] failed to remove file %s: %s", f, err)
if e := os.Remove(f); e != nil {
log.Printf("[WARN] failed to remove file %s: %s", f, e)
continue
}
log.Printf("[INFO] removed %s for %s (%s)", f, chanInfo.ID, chanInfo.Name)
Expand Down
9 changes: 3 additions & 6 deletions app/youtube/store/store.go
Expand Up @@ -136,14 +136,13 @@ func (s *BoltDB) RemoveOld(channelID string, keep int) ([]string, error) {
deleted := 0
var res []string

err := s.DB.Update(func(tx *bolt.Tx) error {
err := s.DB.Update(func(tx *bolt.Tx) (e error) {
bucket := tx.Bucket([]byte(channelID))
if bucket == nil {
return fmt.Errorf("no bucket for %s", channelID)
}
recs := 0
c := bucket.Cursor()
var err error
for k, v := c.Last(); k != nil; k, v = c.Prev() {
recs++
if recs > keep {
Expand All @@ -154,13 +153,11 @@ func (s *BoltDB) RemoveOld(channelID string, keep int) ([]string, error) {
}
res = append(res, item.File)

if e := bucket.Delete(k); e != nil {
err = e
}
e = bucket.Delete(k)
deleted++
}
}
return err
return e
})
return res, err
}

0 comments on commit 2a8eb16

Please sign in to comment.