|
| 1 | +package command |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "net/url" |
| 7 | + |
| 8 | + "github.com/FileFormatInfo/fflint/internal/argtype" |
| 9 | + "github.com/FileFormatInfo/fflint/internal/shared" |
| 10 | + "github.com/mmcdole/gofeed" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + feedFormat = argtype.NewStringSet("Feed file format", "auto", []string{"atom", "auto", "jsonfeed", "rss"}) |
| 16 | + feedStrict bool |
| 17 | +) |
| 18 | + |
| 19 | +// xmlCmd represents the xml command |
| 20 | +var feedCmd = &cobra.Command{ |
| 21 | + Args: cobra.MinimumNArgs(1), |
| 22 | + Use: "feed [options] files...", |
| 23 | + Short: "Validate feeds (RSS/Atom/Jsonfeed)", |
| 24 | + Long: `Checks that your feeds are valid. RSS, Atom and JSONFeed are supported.`, |
| 25 | + PreRunE: feedInit, |
| 26 | + RunE: shared.MakeFileCommand(feedCheck), |
| 27 | + PostRunE: feedCleanup, |
| 28 | +} |
| 29 | + |
| 30 | +func AddFeedCommand(rootCmd *cobra.Command) { |
| 31 | + rootCmd.AddCommand(feedCmd) |
| 32 | + feedCmd.Flags().Var(&feedFormat, "format", feedFormat.HelpText()) |
| 33 | + feedCmd.Flags().BoolVar(&feedStrict, "strict", true, "Check contents in addition to parsability") |
| 34 | +} |
| 35 | + |
| 36 | +func feedCheck(f *shared.FileContext) { |
| 37 | + |
| 38 | + data, readErr := f.ReadFile() |
| 39 | + if readErr != nil { |
| 40 | + f.RecordResult("fileRead", false, map[string]interface{}{ |
| 41 | + "error": readErr, |
| 42 | + }) |
| 43 | + return |
| 44 | + } |
| 45 | + var parseErr error |
| 46 | + var feed *gofeed.Feed |
| 47 | + |
| 48 | + detectedFeedType := gofeed.DetectFeedType(bytes.NewReader(data)) |
| 49 | + if feedFormat.String() == "auto" { |
| 50 | + if detectedFeedType == gofeed.FeedTypeUnknown { |
| 51 | + f.RecordResult("feedDetectType", false, map[string]interface{}{ |
| 52 | + "error": "Unknown feed type", |
| 53 | + "startOfInput": Substr(string(data), 0, 100), |
| 54 | + }) |
| 55 | + return |
| 56 | + } |
| 57 | + } else if (feedFormat.String() == "rss" && detectedFeedType != gofeed.FeedTypeRSS) || |
| 58 | + (feedFormat.String() == "atom" && detectedFeedType != gofeed.FeedTypeAtom) || |
| 59 | + (feedFormat.String() == "jsonfeed" && detectedFeedType != gofeed.FeedTypeJSON) { |
| 60 | + f.RecordResult("feedTypeMismatch", false, map[string]interface{}{ |
| 61 | + "error": "Feed type mismatch", |
| 62 | + "detected": detectedFeedType, |
| 63 | + "expected": feedFormat.String(), |
| 64 | + }) |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + p := gofeed.NewParser() |
| 69 | + feed, parseErr = p.Parse(bytes.NewReader(data)) |
| 70 | + |
| 71 | + if parseErr != nil { |
| 72 | + f.RecordResult("feedParse", false, map[string]interface{}{ |
| 73 | + "error": parseErr, |
| 74 | + }) |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + if feed == nil { |
| 79 | + f.RecordResult("feedEmpty", false, map[string]interface{}{ |
| 80 | + "error": "Empty feed", |
| 81 | + }) |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + if !feedStrict { |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + if feed.Title == "" { |
| 90 | + f.RecordResult("feedTitle", false, map[string]interface{}{ |
| 91 | + "error": "Missing title", |
| 92 | + }) |
| 93 | + } |
| 94 | + |
| 95 | + if feed.Description == "" { |
| 96 | + f.RecordResult("feedTitle", false, map[string]interface{}{ |
| 97 | + "error": "Missing title", |
| 98 | + }) |
| 99 | + } |
| 100 | + |
| 101 | + if feedParentLinkErr := IsValidUrl(feed.Link); feedParentLinkErr != nil { |
| 102 | + f.RecordResult("feedParentLink", false, map[string]interface{}{ |
| 103 | + "error": feedParentLinkErr, |
| 104 | + "url": feed.Link, |
| 105 | + }) |
| 106 | + } |
| 107 | + |
| 108 | + if feedSelfLinkErr := IsValidUrl(feed.FeedLink); feedSelfLinkErr != nil { |
| 109 | + f.RecordResult("feedSelfLink", false, map[string]interface{}{ |
| 110 | + "error": feedSelfLinkErr, |
| 111 | + "url": feed.FeedLink, |
| 112 | + }) |
| 113 | + } |
| 114 | + |
| 115 | + if feed.Updated != "" && feed.UpdatedParsed == nil { |
| 116 | + f.RecordResult("feedUpdated", false, map[string]interface{}{ |
| 117 | + "error": "Invalid updated date", |
| 118 | + "rawdate": feed.Updated, |
| 119 | + }) |
| 120 | + } |
| 121 | + |
| 122 | + if feed.Published != "" && feed.PublishedParsed == nil { |
| 123 | + f.RecordResult("feedPublished", false, map[string]interface{}{ |
| 124 | + "error": "Invalid published date", |
| 125 | + "rawdate": feed.Published, |
| 126 | + }) |
| 127 | + } |
| 128 | + |
| 129 | + if feed.Items == nil || len(feed.Items) == 0 { |
| 130 | + f.RecordResult("feedItems", false, map[string]interface{}{ |
| 131 | + "error": "No items found", |
| 132 | + }) |
| 133 | + } else { |
| 134 | + guidMap := make(map[string]int) |
| 135 | + for i, item := range feed.Items { |
| 136 | + if item.Title == "" { |
| 137 | + f.RecordResult("feedItemTitle", false, map[string]interface{}{ |
| 138 | + "error": "Missing title", |
| 139 | + "index": i, |
| 140 | + }) |
| 141 | + } |
| 142 | + if item.Description == "" { |
| 143 | + f.RecordResult("feedItemDescription", false, map[string]interface{}{ |
| 144 | + "error": "Missing description", |
| 145 | + "index": i, |
| 146 | + }) |
| 147 | + } |
| 148 | + if item.Link == "" { |
| 149 | + f.RecordResult("feedItemLink", false, map[string]interface{}{ |
| 150 | + "error": "Missing link", |
| 151 | + "index": i, |
| 152 | + }) |
| 153 | + } |
| 154 | + if item.Published != "" && item.PublishedParsed == nil { |
| 155 | + f.RecordResult("feedItemPublished", false, map[string]interface{}{ |
| 156 | + "error": "Invalid published date", |
| 157 | + "index": i, |
| 158 | + "rawdate": item.Published, |
| 159 | + }) |
| 160 | + } |
| 161 | + if item.Updated != "" && item.UpdatedParsed == nil { |
| 162 | + f.RecordResult("feedItemUpdated", false, map[string]interface{}{ |
| 163 | + "error": "Invalid updated date", |
| 164 | + "index": i, |
| 165 | + "rawdate": item.Updated, |
| 166 | + }) |
| 167 | + } |
| 168 | + if item.GUID == "" { |
| 169 | + f.RecordResult("feedItemGUID", false, map[string]interface{}{ |
| 170 | + "error": "Missing GUID", |
| 171 | + "index": i, |
| 172 | + }) |
| 173 | + } else if originalIndex, ok := guidMap[item.GUID]; ok { |
| 174 | + f.RecordResult("feedItemGUID", false, map[string]interface{}{ |
| 175 | + "error": "Duplicate GUID", |
| 176 | + "originalIndex": originalIndex, |
| 177 | + "duplicateIndex": i, |
| 178 | + "guid": item.GUID, |
| 179 | + }) |
| 180 | + } else { |
| 181 | + guidMap[item.GUID] = i |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +func feedInit(cmd *cobra.Command, args []string) error { |
| 188 | + return nil |
| 189 | +} |
| 190 | + |
| 191 | +func feedCleanup(cmd *cobra.Command, args []string) error { |
| 192 | + return nil |
| 193 | +} |
| 194 | + |
| 195 | +func IsValidUrl(target string) error { |
| 196 | + if target == "" { |
| 197 | + return fmt.Errorf("URL not set") |
| 198 | + } |
| 199 | + _, err := url.ParseRequestURI(target) |
| 200 | + if err != nil { |
| 201 | + return err |
| 202 | + } |
| 203 | + return nil |
| 204 | +} |
| 205 | + |
| 206 | +// UTF8-safe substring |
| 207 | +func Substr(input string, start int, length int) string { |
| 208 | + |
| 209 | + if start == 0 && length >= len(input) { |
| 210 | + return input |
| 211 | + } |
| 212 | + |
| 213 | + asRunes := []rune(input) |
| 214 | + if start >= len(asRunes) { |
| 215 | + return "" |
| 216 | + } |
| 217 | + |
| 218 | + if start+length > len(asRunes) { |
| 219 | + length = len(asRunes) - start |
| 220 | + } |
| 221 | + |
| 222 | + return string(asRunes[start : start+length]) |
| 223 | +} |
0 commit comments