Skip to content

Commit

Permalink
Pass our version down through the layers, so that it shows in the def…
Browse files Browse the repository at this point in the history
…ault user-agent
  • Loading branch information
skx committed Jun 4, 2024
1 parent 3257281 commit f429fd8
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 14 deletions.
3 changes: 3 additions & 0 deletions daemon_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ func (d *daemonCmd) Execute(args []string) int {
return 1
}

// Ensure we send our version
p.SetVersion(version)

// Setup the state - note we ALWAYS send emails in this mode.
p.SetSendEmail(true)
p.SetLogger(logger)
Expand Down
4 changes: 2 additions & 2 deletions httpfetch/httpfetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ type HTTPFetch struct {
}

// New creates a new object which will fetch our content.
func New(entry configfile.Feed, log *slog.Logger) *HTTPFetch {
func New(entry configfile.Feed, log *slog.Logger, version string) *HTTPFetch {

// Create object with defaults
state := &HTTPFetch{url: entry.URL,
maxRetries: 3,
retryDelay: 1000 * time.Millisecond,
userAgent: "rss2email (https://github.com/skx/rss2email)",
userAgent: fmt.Sprintf("rss2email %s (https://github.com/skx/rss2email)", version),
}

// Are any of our options overridden?
Expand Down
22 changes: 13 additions & 9 deletions httpfetch/httpfetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func init() {
func TestNonFeed(t *testing.T) {

// Not a feed.
x := New(configfile.Feed{URL: "http://example.com/"}, logger)
x := New(configfile.Feed{URL: "http://example.com/"}, logger, "v1.2.3")
x.content = "this is not an XML file, so not a feed"

// Parse it, which should fail.
Expand All @@ -51,13 +51,17 @@ func TestNonFeed(t *testing.T) {
if !strings.Contains(err.Error(), "Failed to detect feed type") {
t.Fatalf("got an error, but not what we expected; %s", err.Error())
}

if !strings.Contains(x.userAgent, "v1.2.3") {
t.Fatalf("our default agent doesn't contain our version string: '%s'", x.userAgent)
}
}

// TestOneEntry confirms a feed contains a single entry
func TestOneEntry(t *testing.T) {

// The contents of our feed.
x := New(configfile.Feed{URL: "https://blog.steve.fi/index.rss"}, logger)
x := New(configfile.Feed{URL: "https://blog.steve.fi/index.rss"}, logger, "unversioned")
x.content = `<?xml version="1.0"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
Expand Down Expand Up @@ -103,7 +107,7 @@ func TestOneEntry(t *testing.T) {
func TestRewrite(t *testing.T) {

// The contents of our feed.
x := New(configfile.Feed{URL: "https://blog.steve.fi/index.rss"}, logger)
x := New(configfile.Feed{URL: "https://blog.steve.fi/index.rss"}, logger, "unversioned")
x.content = `<?xml version="1.0"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
Expand Down Expand Up @@ -164,7 +168,7 @@ func TestDelay(t *testing.T) {
n := New(configfile.Feed{URL: "https://blog.steve.fi/index.rss",
Options: []configfile.Option{
{Name: "delay", Value: "15"},
}}, logger)
}}, logger, "unversioned")

if n.retryDelay != 15*time.Millisecond {
t.Errorf("failed to parse delay value")
Expand All @@ -174,7 +178,7 @@ func TestDelay(t *testing.T) {
i := New(configfile.Feed{URL: "https://blog.steve.fi/index.rss",
Options: []configfile.Option{
{Name: "delay", Value: "steve"},
}}, logger)
}}, logger, "unversioned")

if i.retryDelay != 1000*time.Millisecond {
t.Errorf("bogus value changed our delay-value")
Expand All @@ -188,7 +192,7 @@ func TestRetry(t *testing.T) {
Options: []configfile.Option{
{Name: "retry", Value: "33"},
{Name: "moi", Value: "3"},
}}, logger)
}}, logger, "unversioned")

if n.maxRetries != 33 {
t.Errorf("failed to parse retry value")
Expand All @@ -198,7 +202,7 @@ func TestRetry(t *testing.T) {
i := New(configfile.Feed{URL: "https://blog.steve.fi/index.rss",
Options: []configfile.Option{
{Name: "retry", Value: "steve"},
}}, logger)
}}, logger, "unversioned")

if i.maxRetries != 3 {
t.Errorf("bogus value changed our default")
Expand All @@ -218,7 +222,7 @@ func TestHTTPFetch(t *testing.T) {
conf := configfile.Feed{URL: ts.URL}

// Create a fetcher
obj := New(conf, logger)
obj := New(conf, logger, "unversioned")

// Now make the HTTP-fetch
_, err := obj.Fetch()
Expand Down Expand Up @@ -279,7 +283,7 @@ func TestHTTPFetchValid(t *testing.T) {
}

// Create a fetcher
obj := New(conf, logger)
obj := New(conf, logger, "unversioned")

if obj.userAgent != agent {
t.Fatalf("failed to setup user-agent")
Expand Down
2 changes: 1 addition & 1 deletion list_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Example:
func (l *listCmd) showFeedDetails(entry configfile.Feed) {

// Fetch the details
helper := httpfetch.New(entry, logger)
helper := httpfetch.New(entry, logger, version)
feed, err := helper.Fetch()
if err != nil {
fmt.Fprintf(out, "# %s\n%s\n", err.Error(), entry.URL)
Expand Down
13 changes: 11 additions & 2 deletions processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ type Processor struct {
// store feed-entry state within.
dbHandle *bbolt.DB

// logger stores the logging dbHandle
// logger stores the logging dbHandle.
logger *slog.Logger

// version stores the version of our application.
version string
}

// New creates a new Processor object.
Expand Down Expand Up @@ -271,7 +274,7 @@ func (p *Processor) processFeed(entry configfile.Feed, recipients []string) erro
}

// Fetch the feed for the input URL
helper := httpfetch.New(entry, logger)
helper := httpfetch.New(entry, logger, p.version)
feed, err := helper.Fetch()
if err != nil {

Expand Down Expand Up @@ -839,3 +842,9 @@ func (p *Processor) SetSendEmail(state bool) {
func (p *Processor) SetLogger(logger *slog.Logger) {
p.logger = logger
}

// SetVersion ensures we can pass the version of our client to our HTTP-fetcher,
// which means that the version will end up in our (default) user-agent.
func (p *Processor) SetVersion(version string) {
p.version = version
}

0 comments on commit f429fd8

Please sign in to comment.