Browse Source

fix: address minor issues detected by Go linters

Frédéric Guillot 1 year ago
parent
commit
e342a4f143

+ 3 - 5
.github/workflows/linters.yml

@@ -33,12 +33,10 @@ jobs:
         with:
           args: >
             --timeout 10m
-            --disable errcheck,staticcheck
+            --disable errcheck
             --enable sqlclosecheck,misspell,whitespace,gocritic
-      - uses: dominikh/staticcheck-action@v1.3.1
-        with:
-          version: "latest"
-          install-go: false
+      - name: Run gofmt linter
+        run: gofmt -d -e .
 
   commitlint:
     name: Commit Linter

+ 6 - 2
internal/config/parser.go

@@ -189,11 +189,15 @@ func (p *Parser) parseLines(lines []string) (err error) {
 		case "PROXY_PRIVATE_KEY":
 			slog.Warn("The PROXY_PRIVATE_KEY environment variable is deprecated, use MEDIA_PROXY_PRIVATE_KEY instead")
 			randomKey := make([]byte, 16)
-			rand.Read(randomKey)
+			if _, err := rand.Read(randomKey); err != nil {
+				return fmt.Errorf("config: unable to generate random key: %w", err)
+			}
 			p.opts.mediaProxyPrivateKey = parseBytes(value, randomKey)
 		case "MEDIA_PROXY_PRIVATE_KEY":
 			randomKey := make([]byte, 16)
-			rand.Read(randomKey)
+			if _, err := rand.Read(randomKey); err != nil {
+				return fmt.Errorf("config: unable to generate random key: %w", err)
+			}
 			p.opts.mediaProxyPrivateKey = parseBytes(value, randomKey)
 		case "MEDIA_PROXY_CUSTOM_URL":
 			p.opts.mediaProxyCustomURL = parseString(value, defaultMediaProxyURL)

+ 1 - 1
internal/model/webauthn.go

@@ -36,7 +36,7 @@ func (s WebAuthnSession) String() string {
 	if s.SessionData == nil {
 		return "{}"
 	}
-	return fmt.Sprintf("{Challenge: %s, UserID: %x}", s.SessionData.Challenge, s.SessionData.UserID)
+	return fmt.Sprintf("{Challenge: %s, UserID: %x}", s.Challenge, s.UserID)
 }
 
 type WebAuthnCredential struct {

+ 4 - 4
internal/reader/atom/atom_03.go

@@ -116,12 +116,12 @@ type Atom03Content struct {
 func (a *Atom03Content) Content() string {
 	content := ""
 
-	switch {
-	case a.Mode == "xml":
+	switch a.Mode {
+	case "xml":
 		content = a.InnerXML
-	case a.Mode == "escaped":
+	case "escaped":
 		content = a.CharData
-	case a.Mode == "base64":
+	case "base64":
 		b, err := base64.StdEncoding.DecodeString(a.CharData)
 		if err == nil {
 			content = string(b)

+ 3 - 1
internal/reader/opml/handler.go

@@ -77,7 +77,9 @@ func (h *Handler) Import(userID int64, data io.Reader) error {
 				Category:    category,
 			}
 
-			h.store.CreateFeed(feed)
+			if err := h.store.CreateFeed(feed); err != nil {
+				return fmt.Errorf(`opml: unable to create this feed: %q`, subscription.FeedURL)
+			}
 		}
 	}
 

+ 4 - 4
internal/reader/rss/adapter.go

@@ -39,7 +39,7 @@ func (r *RSSAdapter) BuildFeed(baseURL string) *model.Feed {
 	}
 
 	// Try to find the feed URL from the Atom links.
-	for _, atomLink := range r.rss.Channel.AtomLinks.Links {
+	for _, atomLink := range r.rss.Channel.Links {
 		atomLinkHref := strings.TrimSpace(atomLink.Href)
 		if atomLinkHref != "" && atomLink.Rel == "self" {
 			if absoluteFeedURL, err := urllib.AbsoluteURL(feed.FeedURL, atomLinkHref); err == nil {
@@ -189,7 +189,7 @@ func findEntryURL(rssItem *RSSItem) string {
 		}
 	}
 
-	for _, atomLink := range rssItem.AtomLinks.Links {
+	for _, atomLink := range rssItem.Links {
 		if atomLink.Href != "" && (strings.EqualFold(atomLink.Rel, "alternate") || atomLink.Rel == "") {
 			return strings.TrimSpace(atomLink.Href)
 		}
@@ -253,8 +253,8 @@ func findEntryAuthor(rssItem *RSSItem) string {
 		author = rssItem.ItunesAuthor
 	case rssItem.DublinCoreCreator != "":
 		author = rssItem.DublinCoreCreator
-	case rssItem.AtomAuthor.PersonName() != "":
-		author = rssItem.AtomAuthor.PersonName()
+	case rssItem.PersonName() != "":
+		author = rssItem.PersonName()
 	case strings.Contains(rssItem.Author.Inner, "<![CDATA["):
 		author = rssItem.Author.Data
 	default:

+ 3 - 2
internal/reader/sanitizer/sanitizer.go

@@ -283,9 +283,10 @@ func isPixelTracker(tagName string, attributes []html.Attribute) bool {
 
 	for _, attribute := range attributes {
 		if attribute.Val == "1" {
-			if attribute.Key == "height" {
+			switch attribute.Key {
+			case "height":
 				hasHeight = true
-			} else if attribute.Key == "width" {
+			case "width":
 				hasWidth = true
 			}
 		}

+ 3 - 3
internal/reader/sanitizer/srcset.go

@@ -49,10 +49,10 @@ func parseImageCandidate(input string) (*ImageCandidate, error) {
 	parts := strings.Split(strings.TrimSpace(input), " ")
 	nbParts := len(parts)
 
-	switch {
-	case nbParts == 1:
+	switch nbParts {
+	case 1:
 		return &ImageCandidate{ImageURL: parts[0]}, nil
-	case nbParts == 2:
+	case 2:
 		if !isValidWidthOrDensityDescriptor(parts[1]) {
 			return nil, fmt.Errorf(`srcset: invalid descriptor`)
 		}

+ 3 - 2
internal/storage/feed_query_builder.go

@@ -318,9 +318,10 @@ func (f *FeedQueryBuilder) fetchFeedCounter() (unreadCounters map[int64]int, rea
 			return nil, nil, fmt.Errorf(`store: unable to fetch feed counter row: %w`, err)
 		}
 
-		if status == model.EntryStatusRead {
+		switch status {
+		case model.EntryStatusRead:
 			readCounters[feedID] = count
-		} else if status == model.EntryStatusUnread {
+		case model.EntryStatusUnread:
 			unreadCounters[feedID] = count
 		}
 	}

+ 1 - 1
internal/ui/webauthn.go

@@ -235,7 +235,7 @@ func (h *handler) finishLogin(w http.ResponseWriter, r *http.Request) {
 			return
 		}
 
-		sessionData.SessionData.UserID = parsedResponse.Response.UserHandle
+		sessionData.UserID = parsedResponse.Response.UserHandle
 		webAuthUser := WebAuthnUser{user, parsedResponse.Response.UserHandle, storedCredentials}
 
 		// Since go-webauthn v0.11.0, the backup eligibility flag is strictly validated, but Miniflux does not store this flag.