Skip to content

Commit 6707556

Browse files
committed
Minor tweaks
1 parent d1e3c84 commit 6707556

File tree

2 files changed

+78
-14
lines changed

2 files changed

+78
-14
lines changed

add_cmd.go

-4
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@ import (
99
"fmt"
1010

1111
"github.com/skx/rss2email/configfile"
12-
"github.com/skx/subcommands"
1312
)
1413

1514
// Structure for our options and state.
1615
type addCmd struct {
1716

18-
// We embed the NoFlags option, because we accept no command-line flags.
19-
subcommands.NoFlags
20-
2117
// Configuration file, used for testing
2218
config *configfile.ConfigFile
2319
}

list_cmd.go

+78-10
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,40 @@
55
package main
66

77
import (
8+
"flag"
89
"fmt"
10+
"time"
911

1012
"github.com/skx/rss2email/configfile"
11-
"github.com/skx/subcommands"
13+
"github.com/skx/rss2email/httpfetch"
14+
)
15+
16+
var (
17+
maxInt = int(^uint(0) >> 1)
1218
)
1319

1420
// Structure for our options and state.
1521
type listCmd struct {
1622

17-
// We embed the NoFlags option, because we accept no command-line flags.
18-
subcommands.NoFlags
23+
// Configuration file, used for testing
24+
config *configfile.ConfigFile
25+
26+
// verbose controls whether our feed-list contains information
27+
// about feed entries and their ages
28+
verbose bool
29+
}
30+
31+
// Arguments handles argument-flags we might have.
32+
//
33+
// In our case we use this as a hook to setup our configuration-file,
34+
// which allows testing.
35+
func (l *listCmd) Arguments(flags *flag.FlagSet) {
36+
37+
// Setup configuration file
38+
l.config = configfile.New()
39+
40+
// Are we listing verbosely?
41+
flags.BoolVar(&l.verbose, "verbose", false, "Show extra information about each feed (slow)?")
1942
}
2043

2144
// Info is part of the subcommand-API
@@ -31,33 +54,78 @@ please run:
3154
$ rss2email help config
3255
3356
57+
You can add '-verbose' to see details about the feed contents, but note
58+
that this will require downloading the contents of each feed and will
59+
thus be slow.
60+
3461
Example:
3562
3663
$ rss2email list
3764
`
3865
}
3966

67+
func (l *listCmd) showFeedDetails(entry configfile.Feed) {
68+
69+
// Fetch the details
70+
helper := httpfetch.New(entry)
71+
feed, err := helper.Fetch()
72+
if err != nil {
73+
fmt.Fprintf(out, "# %s\n%s\n", err.Error(), entry.URL)
74+
return
75+
}
76+
77+
// Handle single vs. plural entries
78+
entriesString := "entries"
79+
if len(feed.Items) == 1 {
80+
entriesString = "entry"
81+
}
82+
83+
// get the age-range of the feed-entries
84+
oldest := -1
85+
newest := maxInt
86+
for _, item := range feed.Items {
87+
if item.PublishedParsed == nil {
88+
break
89+
}
90+
91+
age := int(time.Since(*item.PublishedParsed) / (24 * time.Hour))
92+
if age > oldest {
93+
oldest = age
94+
}
95+
96+
if age < newest {
97+
newest = age
98+
}
99+
}
100+
101+
// Now show the details, which is a bit messy.
102+
fmt.Fprintf(out, "# %d %s, aged %d-%d days\n", len(feed.Items), entriesString, newest, oldest)
103+
fmt.Fprintf(out, "%s\n", entry.URL)
104+
}
105+
40106
//
41107
// Entry-point.
42108
//
43109
func (l *listCmd) Execute(args []string) int {
44110

45-
// Get the configuration-file
46-
conf := configfile.New()
47-
48-
// Upgrade it if necessary
49-
conf.Upgrade()
111+
// Upgrade our configuration-file if necessary
112+
l.config.Upgrade()
50113

51114
// Now do the parsing
52-
entries, err := conf.Parse()
115+
entries, err := l.config.Parse()
53116
if err != nil {
54117
fmt.Printf("Error with config-file: %s\n", err.Error())
55118
return 1
56119
}
57120

58121
// Show the feeds
59122
for _, entry := range entries {
60-
fmt.Printf("%s\n", entry.URL)
123+
124+
if l.verbose {
125+
l.showFeedDetails(entry)
126+
} else {
127+
fmt.Fprintf(out, "%s\n", entry.URL)
128+
}
61129
}
62130

63131
return 0

0 commit comments

Comments
 (0)