-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.go
361 lines (315 loc) Β· 7.91 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package main
import (
"errors"
"fmt"
"os"
"strings"
"github.com/ktr0731/go-fuzzyfinder"
"github.com/mmcdole/gofeed"
"github.com/sheepla/srss/cache"
"github.com/sheepla/srss/opml"
"github.com/sheepla/srss/ui"
"github.com/sheepla/srss/urlentry"
"github.com/urfave/cli/v2"
)
//nolint:gochecknoglobals
var (
appName = "srss"
appVersion = "unknown"
appUsage = "A simple command line RSS feed reader"
)
type exitCode int
const (
exitCodeOK exitCode = iota
exitCodeErrArgs
exitCodeErrFetchFeeds
exitCodeErrURLEntry
exitCodeErrFuzzyFinder
exitCodeErrPager
exitCodeErrOPML
exitCodeErrEditor
exitCodeErrBrowser
exitCodeErrCache
)
const asciiArt = `
ββββββββββββββ ββββββββββββββββββββββββββββββββββ
β β β
ββββββββββ β β ββββββ ββ¬ββββ ββββββ ββββββ β
β β β β β β β β
ββββββ β β β βββββββ β βββββββ βββββββ β
β β β β β β β β β
βββββ β β β β ββββββ ββ΄β ββββββ ββββββ β
β β β β β β β
βββββ ββββββββββββββββββββββββββββββββββ
-- A command line RSS/ATOM/JSON feed reader --
`
func main() {
app := initApp()
if err := app.Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
//nolint:funlen,exhaustruct,exhaustivestruct
func initApp() *cli.App {
return &cli.App{
Name: appName,
HelpName: appName,
Version: appVersion,
Usage: appUsage,
Suggest: false,
EnableBashCompletion: true,
Before: func(ctx *cli.Context) error {
return nil
},
Action: func(ctx *cli.Context) error {
if ctx.NArg() == 0 {
//nolint:forbidigo
fmt.Print(asciiArt)
return cli.Exit("must require arguments", int(exitCodeErrArgs))
}
return nil
},
Commands: []*cli.Command{
{
Name: "add",
Aliases: []string{"a"},
Usage: "Add url entry",
Action: runAddCommand,
},
{
Name: "edit",
Aliases: []string{"e"},
Usage: "Edit URL entry file",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "editor",
Aliases: []string{"e"},
Usage: "Editor command to edit URL entry file",
Value: "vim",
EnvVars: []string{"EDITOR"},
},
},
Action: runEditCommand,
},
{
Name: "tui",
Aliases: []string{"t"},
Usage: "View items in the feed with built-in pager",
Action: runTUICommand,
},
{
Name: "open",
Aliases: []string{"o"},
Usage: "Open feed URL on your browser",
Action: runOpenCommand,
},
{
Name: "import",
Aliases: []string{"i"},
Usage: "Import Feed URL from OPML file",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "path",
Aliases: []string{"p"},
Usage: "OPML file path",
},
},
Action: runImportCommand,
},
{
Name: "update",
Aliases: []string{"u"},
Usage: "Fetch the latest feeds and update the cache",
Action: runUpdateCommand,
},
},
}
}
func runAddCommand(ctx *cli.Context) error {
if ctx.NArg() != 1 {
return cli.Exit(
"requires URL as an argument",
int(exitCodeErrArgs),
)
}
url := strings.TrimSpace(ctx.Args().Get(0))
if !urlentry.IsUniqueURL(url) {
return cli.Exit(
fmt.Sprintf("the URL(%s) has already registered", url),
int(exitCodeErrURLEntry),
)
}
if err := urlentry.Add(url); err != nil {
return cli.Exit(
fmt.Sprintf("failed to add URL(%s) to entry file: %s", url, err),
int(exitCodeErrURLEntry),
)
}
return cli.Exit("", int(exitCodeOK))
}
func runEditCommand(ctx *cli.Context) error {
if ctx.NArg() != 0 {
return cli.Exit(
fmt.Sprintf("extra arguments (%s)", ctx.Args().Slice()),
int(exitCodeErrArgs),
)
}
editor := strings.TrimSpace(ctx.String("editor"))
if editor == "" {
return cli.Exit(
"requires editor command name",
int(exitCodeErrArgs),
)
}
if err := urlentry.OpenEditor(editor); err != nil {
return cli.Exit(
fmt.Sprintf("failed to launch editor: %s", err),
int(exitCodeErrEditor),
)
}
return cli.Exit("", int(exitCodeOK))
}
func runTUICommand(ctx *cli.Context) error {
items, err := cache.Import()
if err != nil {
return cli.Exit(
fmt.Sprintf("failed to load cache: %s", err),
int(exitCodeErrCache),
)
}
for {
idx, err := ui.FindItem(items)
if err != nil {
if errors.Is(fuzzyfinder.ErrAbort, err) {
return cli.Exit(
"quit",
int(exitCodeOK),
)
}
return cli.Exit(
fmt.Sprintf("an error occurred on fuzzyfinder: %s", err),
int(exitCodeErrFuzzyFinder),
)
}
pager, err := ui.NewPager(items[idx])
if err != nil {
return cli.Exit(
fmt.Sprintf("failed to init pager: %s", err),
int(exitCodeErrPager),
)
}
if err := pager.Start(); err != nil {
return cli.Exit(
fmt.Sprintf("an error occurred on pager: %s", err),
int(exitCodeErrPager),
)
}
}
}
func runOpenCommand(ctx *cli.Context) error {
items, err := cache.Import()
if err != nil {
return cli.Exit(
fmt.Sprintf("failed to load cache: %s", err),
int(exitCodeErrCache),
)
}
choises, err := ui.FindItemMulti(items)
if err != nil {
return cli.Exit(
fmt.Sprintf("an error occurred on fuzzyfinder: %s", err),
int(exitCodeErrFuzzyFinder),
)
}
for _, idx := range choises {
if err := ui.OpenURL(items[idx].Link); err != nil {
return cli.Exit(
fmt.Sprintf("failed to open URL in browser: %s", err),
int(exitCodeErrBrowser),
)
}
}
return cli.Exit("", int(exitCodeOK))
}
func runImportCommand(ctx *cli.Context) error {
if ctx.NArg() != 0 {
return cli.Exit(
fmt.Sprintf("extra arguments (%s)", ctx.Args().Slice()),
int(exitCodeErrArgs),
)
}
path := strings.TrimSpace(ctx.String("path"))
if path == "" {
return cli.Exit(
"requires OPML file path",
int(exitCodeErrArgs),
)
}
outlines, err := opml.ParseOPML(path)
if err != nil {
return cli.Exit(
fmt.Sprintf("failed to parse OPML file (%s) %s", path, err),
int(exitCodeErrOPML),
)
}
urls := opml.ExtractFeedURL(outlines.Outlines())
for _, url := range urls {
if err := urlentry.Add(url); err != nil {
return cli.Exit(
fmt.Sprintf("failed to regester the URL entry (%s): %s", url, err),
int(exitCodeErrURLEntry),
)
}
}
return cli.Exit("", int(exitCodeOK))
}
func runUpdateCommand(ctx *cli.Context) error {
if ctx.NArg() != 0 {
return cli.Exit(
fmt.Sprintf("extra arguments (%s)", ctx.Args().Slice()),
int(exitCodeErrArgs),
)
}
urls, err := urlentry.Load()
if err != nil {
return cli.Exit(
fmt.Sprintf("failed to load URL entry: %s", err),
int(exitCodeErrURLEntry),
)
}
if len(urls) == 0 {
return cli.Exit(
"URL entry not registered",
int(exitCodeErrURLEntry),
)
}
//nolint:prealloc
var feeds []gofeed.Feed
for _, url := range urls {
feed, err := fetchFeed(url)
if err != nil {
return cli.Exit(
fmt.Sprintf("failed to fetch the feeds: %s", err),
int(exitCodeErrFetchFeeds),
)
}
//nolint:forbidigo
fmt.Printf("Fetched the feed: %s\n", url)
feeds = append(feeds, *feed)
}
var items []*gofeed.Item
for i := 0; i < len(feeds); i++ {
items = append(items, feeds[i].Items...)
}
if err := cache.Export(items); err != nil {
return fmt.Errorf("failed save the cache: %w", err)
}
return nil
}
func fetchFeed(url string) (*gofeed.Feed, error) {
feed, err := gofeed.NewParser().ParseURL(url)
if err != nil {
return nil, fmt.Errorf("failed to fetch or parse feed at %s: %w", url, err)
}
return feed, nil
}