From bc714480d43f9999bc07497c4dc32bd9d6d1cee4 Mon Sep 17 00:00:00 2001 From: Umputun Date: Tue, 19 Mar 2019 20:54:05 -0500 Subject: [PATCH] lint: multiple shadowed errors, missed comments for exported methods --- backend/app/cmd/avatar.go | 1 + backend/app/cmd/cleanup.go | 2 +- backend/app/cmd/server.go | 16 +++++++++------- backend/app/migrator/disqus.go | 4 ++-- backend/app/migrator/native.go | 16 ++++++++-------- backend/app/migrator/wordpress.go | 3 ++- backend/app/notify/notify.go | 3 ++- backend/app/notify/telegram_test.go | 2 ++ backend/app/rest/api/admin.go | 6 +++--- backend/app/rest/api/rest_private.go | 12 ++++++------ backend/app/rest/api/rss.go | 6 +++--- backend/app/store/engine/bolt_accessor.go | 12 ++++++------ backend/app/store/engine/bolt_admin.go | 20 ++++++++++---------- backend/app/store/engine/mongo.go | 4 ++-- backend/app/store/image/image.go | 4 ++-- 15 files changed, 59 insertions(+), 52 deletions(-) diff --git a/backend/app/cmd/avatar.go b/backend/app/cmd/avatar.go index b93d6b32..5c7e9a92 100644 --- a/backend/app/cmd/avatar.go +++ b/backend/app/cmd/avatar.go @@ -31,6 +31,7 @@ type AvatarMigrator interface { type avatarMigrator struct{} +// Migrate from one avatar store to another. Can be used to convert between stores func (a avatarMigrator) Migrate(dst, src avatar.Store) (int, error) { return avatar.Migrate(dst, src) } diff --git a/backend/app/cmd/cleanup.go b/backend/app/cmd/cleanup.go index c7b3e3e5..bbd29417 100644 --- a/backend/app/cmd/cleanup.go +++ b/backend/app/cmd/cleanup.go @@ -181,7 +181,7 @@ func (cc *CleanupCommand) listComments(postURL string) ([]store.Comment, error) Info store.PostInfo `json:"info,omitempty"` }{} - if err := json.NewDecoder(r.Body).Decode(&commentsWithInfo); err != nil { + if err = json.NewDecoder(r.Body).Decode(&commentsWithInfo); err != nil { return nil, errors.Wrapf(err, "can't decode list of comments for %s", postURL) } return commentsWithInfo.Comments, nil diff --git a/backend/app/cmd/server.go b/backend/app/cmd/server.go index f9a44d66..cef2c8e7 100644 --- a/backend/app/cmd/server.go +++ b/backend/app/cmd/server.go @@ -15,7 +15,7 @@ import ( bolt "github.com/coreos/bbolt" log "github.com/go-pkgz/lgr" - auth_cache "github.com/patrickmn/go-cache" + authcache "github.com/patrickmn/go-cache" "github.com/pkg/errors" "github.com/go-pkgz/auth" @@ -304,9 +304,9 @@ func (s *ServerCommand) newServerApp() (*serverApp, error) { var devAuth *provider.DevAuthServer if s.Auth.Dev { - da, err := authenticator.DevAuth() - if err != nil { - return nil, errors.Wrap(err, "can't make dev oauth2 server") + da, errDevAuth := authenticator.DevAuth() + if errDevAuth != nil { + return nil, errors.Wrap(errDevAuth, "can't make dev oauth2 server") } devAuth = da } @@ -618,17 +618,19 @@ func (s *ServerCommand) makeAuthenticator(ds *service.DataStore, avas avatar.Sto // authRefreshCache used by authenticator to minimize repeatable token refreshes type authRefreshCache struct { - *auth_cache.Cache + *authcache.Cache } func newAuthRefreshCache() *authRefreshCache { - return &authRefreshCache{Cache: auth_cache.New(5*time.Minute, 10*time.Minute)} + return &authRefreshCache{Cache: authcache.New(5*time.Minute, 10*time.Minute)} } +// Get implements cache getter with key converted to string func (c *authRefreshCache) Get(key interface{}) (interface{}, bool) { return c.Cache.Get(key.(string)) } +// Set implements cache setter with key converted to string func (c *authRefreshCache) Set(key, value interface{}) { - c.Cache.Set(key.(string), value, auth_cache.DefaultExpiration) + c.Cache.Set(key.(string), value, authcache.DefaultExpiration) } diff --git a/backend/app/migrator/disqus.go b/backend/app/migrator/disqus.go index bbe0b260..14a5dc89 100644 --- a/backend/app/migrator/disqus.go +++ b/backend/app/migrator/disqus.go @@ -105,7 +105,7 @@ func (d *Disqus) convert(r io.Reader, siteID string) (ch chan store.Comment) { if se.Name.Local == "thread" { stats.inpThreads++ thread := disqusThread{} - if err := decoder.DecodeElement(&thread, &se); err != nil { + if err = decoder.DecodeElement(&thread, &se); err != nil { log.Printf("[WARN] can't decode disqus thread, %s", err) stats.failedThreads++ continue @@ -116,7 +116,7 @@ func (d *Disqus) convert(r io.Reader, siteID string) (ch chan store.Comment) { if se.Name.Local == "post" { stats.inpComments++ comment := disqusComment{} - if err := decoder.DecodeElement(&comment, &se); err != nil { + if err = decoder.DecodeElement(&comment, &se); err != nil { log.Printf("[WARN] can't decode disqus comment, %s", err) stats.failedPosts++ continue diff --git a/backend/app/migrator/native.go b/backend/app/migrator/native.go index 99cf9d34..40db2a17 100644 --- a/backend/app/migrator/native.go +++ b/backend/app/migrator/native.go @@ -15,7 +15,7 @@ import ( "github.com/umputun/remark/backend/app/store/service" ) -const natvieVersion = 1 +const nativeVersion = 1 const defaultConcurrent = 8 // Native implements exporter and importer for internal store format @@ -50,7 +50,7 @@ func (n *Native) Export(w io.Writer, siteID string) (size int, err error) { for i := len(topics) - 1; i >= 0; i-- { // topics from List sorted in opposite direction topic := topics[i] comments, e := n.DataStore.Find(store.Locator{SiteID: siteID, URL: topic.URL}, "time") - if err != nil { + if e != nil { return commentsCount, e } @@ -75,13 +75,13 @@ func (n *Native) Export(w io.Writer, siteID string) (size int, err error) { // exportMeta appends user and post metas to exported stream func (n *Native) exportMeta(siteID string, w io.Writer) (err error) { - m := meta{Version: natvieVersion} + m := meta{Version: nativeVersion} m.Users, m.Posts, err = n.DataStore.Metas(siteID) if err != nil { return errors.Wrap(err, "can't get meta") } - if err := json.NewEncoder(w).Encode(m); err != nil { + if err = json.NewEncoder(w).Encode(m); err != nil { return errors.Wrap(err, "can't encode meta") } return nil @@ -96,7 +96,7 @@ func (n *Native) Import(reader io.Reader, siteID string) (size int, err error) { return 0, errors.Wrapf(err, "failed to import meta for site %s", siteID) } - if m.Version != natvieVersion && m.Version != 0 { // this version allows back compatibility with 0 version + if m.Version != nativeVersion && m.Version != 0 { // this version allows back compatibility with 0 version return 0, errors.Errorf("unexpected import file version %d", m.Version) } @@ -134,9 +134,9 @@ func (n *Native) Import(reader io.Reader, siteID string) (size int, err error) { log.Printf("[WARN] can't write %+v to store, %s", comment, e) return } - n := atomic.AddInt64(&comments, 1) - if n%1000 == 0 { - log.Printf("[DEBUG] imported %d comments", n) + num := atomic.AddInt64(&comments, 1) + if num%1000 == 0 { + log.Printf("[DEBUG] imported %d comments", num) } }) diff --git a/backend/app/migrator/wordpress.go b/backend/app/migrator/wordpress.go index 90dce39b..c1199ea7 100644 --- a/backend/app/migrator/wordpress.go +++ b/backend/app/migrator/wordpress.go @@ -39,6 +39,7 @@ type wpTime struct { time time.Time } +// UnmarshalXML decoding xml with time in WP format func (w *wpTime) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { var v string if err := d.DecodeElement(&v, &start); err != nil { @@ -111,7 +112,7 @@ func (w *WordPress) convert(r io.Reader, siteID string) chan store.Comment { if el.Name.Local == "item" { stats.inpItems++ item := wpItem{} - if err := decoder.DecodeElement(&item, &el); err != nil { + if err = decoder.DecodeElement(&item, &el); err != nil { log.Printf("[WARN] Can't decode item, %s", err) stats.failedItems++ continue diff --git a/backend/app/notify/notify.go b/backend/app/notify/notify.go index 67b1b7bb..886fabad 100644 --- a/backend/app/notify/notify.go +++ b/backend/app/notify/notify.go @@ -29,10 +29,11 @@ type Destination interface { Send(ctx context.Context, req request) error } -// Store defines the minimal interface accessing stored commens used by notifier +// Store defines the minimal interface accessing stored comments used by notifier type Store interface { Get(locator store.Locator, id string) (store.Comment, error) } + type request struct { comment store.Comment parent store.Comment diff --git a/backend/app/notify/telegram_test.go b/backend/app/notify/telegram_test.go index 8c429d5b..35dd2a21 100644 --- a/backend/app/notify/telegram_test.go +++ b/backend/app/notify/telegram_test.go @@ -9,6 +9,7 @@ import ( "github.com/go-chi/chi" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/umputun/remark/backend/app/store" ) @@ -71,6 +72,7 @@ func TestTelegram_Send(t *testing.T) { tb, err = NewTelegram("non-json-resp", "remark_test", 2*time.Second, ts.URL+"/") assert.NotNil(t, err, "should failed") err = tb.Send(context.TODO(), request{comment: c, parent: cp}) + require.NotNil(t, err) assert.Contains(t, err.Error(), "unexpected telegram status code 404", "send on broken tg") assert.Equal(t, "telegram: @remark_test", tb.String()) diff --git a/backend/app/rest/api/admin.go b/backend/app/rest/api/admin.go index 6f634e29..cba6472a 100644 --- a/backend/app/rest/api/admin.go +++ b/backend/app/rest/api/admin.go @@ -115,14 +115,14 @@ func (a *admin) deleteMeRequestCtrl(w http.ResponseWriter, r *http.Request) { return } - if err := a.dataService.DeleteUser(claims.Audience, claims.User.ID); err != nil { + if err = a.dataService.DeleteUser(claims.Audience, claims.User.ID); err != nil { rest.SendErrorJSON(w, r, http.StatusBadRequest, err, "can't delete user", rest.ErrNoAccess) return } if claims.User.Picture != "" && a.authenticator.AvatarProxy() != nil { - avatartStore := a.authenticator.AvatarProxy().Store - if err := avatartStore.Remove(path.Base(claims.User.Picture)); err != nil { + avatarStore := a.authenticator.AvatarProxy().Store + if err = avatarStore.Remove(path.Base(claims.User.Picture)); err != nil { rest.SendErrorJSON(w, r, http.StatusBadRequest, err, "can't delete user's avatar", rest.ErrInternal) return } diff --git a/backend/app/rest/api/rest_private.go b/backend/app/rest/api/rest_private.go index ec17967d..97e2b493 100644 --- a/backend/app/rest/api/rest_private.go +++ b/backend/app/rest/api/rest_private.go @@ -228,14 +228,14 @@ func (s *Rest) userAllDataCtrl(w http.ResponseWriter, r *http.Request) { // get comments in 100 in each paginated request for i := 0; i < 100; i++ { - comments, err := s.DataService.User(siteID, user.ID, 100, i*100) - if err != nil { - rest.SendErrorJSON(w, r, http.StatusInternalServerError, err, "can't get user comments", rest.ErrInternal) + comments, errUser := s.DataService.User(siteID, user.ID, 100, i*100) + if errUser != nil { + rest.SendErrorJSON(w, r, http.StatusInternalServerError, errUser, "can't get user comments", rest.ErrInternal) return } - b, err := json.Marshal(comments) - if err != nil { - rest.SendErrorJSON(w, r, http.StatusInternalServerError, err, "can't marshal user comments", rest.ErrInternal) + b, errUser := json.Marshal(comments) + if errUser != nil { + rest.SendErrorJSON(w, r, http.StatusInternalServerError, errUser, "can't marshal user comments", rest.ErrInternal) return } diff --git a/backend/app/rest/api/rss.go b/backend/app/rest/api/rss.go index 1573befc..1b7107d2 100644 --- a/backend/app/rest/api/rss.go +++ b/backend/app/rest/api/rss.go @@ -57,7 +57,7 @@ func (s *Rest) rssPostCommentsCtrl(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/xml; charset=utf-8") w.WriteHeader(http.StatusOK) - if _, err := w.Write(data); err != nil { + if _, err = w.Write(data); err != nil { log.Printf("[WARN] failed to send response to %s, %s", r.RemoteAddr, err) } } @@ -89,7 +89,7 @@ func (s *Rest) rssSiteCommentsCtrl(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/xml; charset=utf-8") w.WriteHeader(http.StatusOK) - if _, err := w.Write(data); err != nil { + if _, err = w.Write(data); err != nil { log.Printf("[WARN] failed to send response to %s, %s", r.RemoteAddr, err) } } @@ -141,7 +141,7 @@ func (s *Rest) rssRepliesCtrl(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/xml; charset=utf-8") w.WriteHeader(http.StatusOK) - if _, err := w.Write(data); err != nil { + if _, err = w.Write(data); err != nil { log.Printf("[WARN] failed to send response to %s, %s", r.RemoteAddr, err) } } diff --git a/backend/app/store/engine/bolt_accessor.go b/backend/app/store/engine/bolt_accessor.go index 9076f874..22a62893 100644 --- a/backend/app/store/engine/bolt_accessor.go +++ b/backend/app/store/engine/bolt_accessor.go @@ -155,7 +155,7 @@ func (b *BoltDB) Find(locator store.Locator, sortFld string) (comments []store.C return bucket.ForEach(func(k, v []byte) error { comment := store.Comment{} - if e := json.Unmarshal(v, &comment); e != nil { + if e = json.Unmarshal(v, &comment); e != nil { return errors.Wrap(e, "failed to unmarshal") } comments = append(comments, comment) @@ -195,7 +195,7 @@ func (b *BoltDB) Last(siteID string, max int) (comments []store.Comment, err err } comment := store.Comment{} - if e := b.load(postBkt, []byte(commentID), &comment); e != nil { + if e = b.load(postBkt, []byte(commentID), &comment); e != nil { log.Printf("[WARN] can't load comment for %s from store %s", commentID, url) continue } @@ -335,11 +335,11 @@ func (b *BoltDB) User(siteID, userID string, limit, skip int) (comments []store. // retrieve comments for refs for _, v := range commentRefs { - url, commentID, e := b.parseRef([]byte(v)) - if e != nil { - return comments, errors.Wrapf(e, "can't parse reference %s", v) + url, commentID, errParse := b.parseRef([]byte(v)) + if errParse != nil { + return comments, errors.Wrapf(errParse, "can't parse reference %s", v) } - if c, e := b.Get(store.Locator{SiteID: siteID, URL: url}, commentID); e == nil { + if c, errRef := b.Get(store.Locator{SiteID: siteID, URL: url}, commentID); errRef == nil { comments = append(comments, c) } } diff --git a/backend/app/store/engine/bolt_admin.go b/backend/app/store/engine/bolt_admin.go index 579d34db..429ac991 100644 --- a/backend/app/store/engine/bolt_admin.go +++ b/backend/app/store/engine/bolt_admin.go @@ -29,19 +29,19 @@ func (b *BoltDB) Delete(locator store.Locator, commentID string, mode store.Dele } comment := store.Comment{} - if err := b.load(postBkt, []byte(commentID), &comment); err != nil { + if err = b.load(postBkt, []byte(commentID), &comment); err != nil { return errors.Wrapf(err, "can't load key %s from bucket %s", commentID, locator.URL) } // set deleted status and clear fields comment.SetDeleted(mode) - if err := b.save(postBkt, []byte(commentID), comment); err != nil { + if err = b.save(postBkt, []byte(commentID), comment); err != nil { return errors.Wrapf(err, "can't save deleted comment for key %s from bucket %s", commentID, locator.URL) } // delete from "last" bucket lastBkt := tx.Bucket([]byte(lastBucketName)) - if err := lastBkt.Delete([]byte(commentID)); err != nil { + if err = lastBkt.Delete([]byte(commentID)); err != nil { return errors.Wrapf(err, "can't delete key %s from bucket %s", commentID, lastBucketName) } @@ -200,8 +200,8 @@ func (b *BoltDB) IsBlocked(siteID string, userID string) (blocked bool) { return nil } - until, err := time.Parse(tsNano, string(val)) - if err != nil { + until, e := time.Parse(tsNano, string(val)) + if e != nil { blocked = false return nil } @@ -223,15 +223,15 @@ func (b *BoltDB) Blocked(siteID string) (users []store.BlockedUser, err error) { err = bdb.View(func(tx *bolt.Tx) error { bucket := tx.Bucket([]byte(blocksBucketName)) return bucket.ForEach(func(k []byte, v []byte) error { - ts, e := time.ParseInLocation(tsNano, string(v), time.Local) - if e != nil { - return errors.Wrap(e, "can't parse block ts") + ts, errParse := time.ParseInLocation(tsNano, string(v), time.Local) + if errParse != nil { + return errors.Wrap(errParse, "can't parse block ts") } if time.Now().Before(ts) { // get user name from comment user section userName := "" - userComments, e := b.User(siteID, string(k), 1, 0) - if e == nil && len(userComments) > 0 { + userComments, errUser := b.User(siteID, string(k), 1, 0) + if errUser == nil && len(userComments) > 0 { userName = userComments[0].User.Name } users = append(users, store.BlockedUser{ID: string(k), Name: userName, Until: ts}) diff --git a/backend/app/store/engine/mongo.go b/backend/app/store/engine/mongo.go index 587e7bda..d36846d2 100644 --- a/backend/app/store/engine/mongo.go +++ b/backend/app/store/engine/mongo.go @@ -230,8 +230,8 @@ func (m *Mongo) Verified(siteID string) (ids []string, err error) { if err != nil { return nil, err } - for _, m := range metas { - ids = append(ids, m.ID) + for _, meta := range metas { + ids = append(ids, meta.ID) } return ids, nil } diff --git a/backend/app/store/image/image.go b/backend/app/store/image/image.go index 7983f69e..30a74ef4 100644 --- a/backend/app/store/image/image.go +++ b/backend/app/store/image/image.go @@ -51,7 +51,7 @@ func (f *FileSystem) Save(name string, r io.Reader) (id string, err error) { location := f.location(id) dst := path.Join(location, id) - if err := os.MkdirAll(location, 0700); err != nil { + if err = os.MkdirAll(location, 0700); err != nil { return "", errors.Wrap(err, "can't make image directory") } @@ -64,7 +64,7 @@ func (f *FileSystem) Save(name string, r io.Reader) (id string, err error) { if err != nil { return "", errors.Wrapf(err, "can't write image file %s", dst) } - if err := fh.Close(); err != nil { + if err = fh.Close(); err != nil { return "", errors.Wrapf(err, "can't close image file %s", dst) } if written > int64(f.MaxSize) {