5
5
package main
6
6
7
7
import (
8
+ "flag"
8
9
"fmt"
10
+ "time"
9
11
10
12
"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 )
12
18
)
13
19
14
20
// Structure for our options and state.
15
21
type listCmd struct {
16
22
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)?" )
19
42
}
20
43
21
44
// Info is part of the subcommand-API
@@ -31,33 +54,78 @@ please run:
31
54
$ rss2email help config
32
55
33
56
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
+
34
61
Example:
35
62
36
63
$ rss2email list
37
64
`
38
65
}
39
66
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
+
40
106
//
41
107
// Entry-point.
42
108
//
43
109
func (l * listCmd ) Execute (args []string ) int {
44
110
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 ()
50
113
51
114
// Now do the parsing
52
- entries , err := conf .Parse ()
115
+ entries , err := l . config .Parse ()
53
116
if err != nil {
54
117
fmt .Printf ("Error with config-file: %s\n " , err .Error ())
55
118
return 1
56
119
}
57
120
58
121
// Show the feeds
59
122
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
+ }
61
129
}
62
130
63
131
return 0
0 commit comments