-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathadd_cmd.go
81 lines (60 loc) · 1.57 KB
/
add_cmd.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
//
// Add a new feed to the users' list of configured feeds.
//
package main
import (
"flag"
"log/slog"
"github.com/skx/rss2email/configfile"
)
// Structure for our options and state.
type addCmd struct {
// Configuration file, used for testing
config *configfile.ConfigFile
}
// Arguments handles argument-flags we might have.
//
// In our case we use this as a hook to setup our configuration-file,
// which allows testing.
func (a *addCmd) Arguments(flags *flag.FlagSet) {
a.config = configfile.New()
}
// Info is part of the subcommand-API
func (a *addCmd) Info() (string, string) {
return "add", `Add a new feed to our feed-list.
Add one or more specified URLs to the configuration file.
To see details of the configuration file, including the location,
please run:
$ rss2email help config
Example:
$ rss2email add https://blog.steve.fi/index.rss
`
}
// Execute is invoked if the user specifies `add` as the subcommand.
func (a *addCmd) Execute(args []string) int {
// Parse the existing file
_, err := a.config.Parse()
if err != nil {
logger.Error("failed to parse configuration file",
slog.String("configfile", a.config.Path()),
slog.String("error", err.Error()))
return 1
}
changed := false
// For each argument add it to the list
for _, entry := range args {
// Add the entry
a.config.Add(entry)
changed = true
}
// Save the list.
if changed {
err = a.config.Save()
if err != nil {
logger.Error("failed to save the updated feed list", slog.String("error", err.Error()))
return 1
}
}
// All done, with no errors.
return 0
}