|
@@ -17,6 +17,7 @@ import (
|
|
|
log "github.com/go-pkgz/lgr"
|
|
|
flags "github.com/jessevdk/go-flags"
|
|
|
|
|
|
+ "git.lattuga.net/boyska/rss2twitter/app/publisher"
|
|
|
"git.lattuga.net/boyska/rss2twitter/app/rss"
|
|
|
)
|
|
|
|
|
@@ -40,23 +41,12 @@ type notifier interface {
|
|
|
Go(ctx context.Context) <-chan rss.Event
|
|
|
}
|
|
|
|
|
|
-type Publisher interface {
|
|
|
- Publish(event rss.Event, formatter func(rss.Event) string) error
|
|
|
-}
|
|
|
-
|
|
|
-// Stdout implements publisher.Interface and sends to stdout
|
|
|
-type Stdout struct{}
|
|
|
-
|
|
|
-// Publish to logger
|
|
|
-func (s Stdout) Publish(event rss.Event, formatter func(rss.Event) string) error {
|
|
|
- log.Printf("[INFO] event - %s", formatter(event))
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
+// CommandPublisher runs a command for every event
|
|
|
type CommandPublisher struct {
|
|
|
Command []string
|
|
|
}
|
|
|
|
|
|
+// Publish run the command for event. Most rss fields are translated as env vars
|
|
|
func (p CommandPublisher) Publish(event rss.Event, formatter func(rss.Event) string) error {
|
|
|
log.Printf("[INFO] lancio - %s ", strings.Join(p.Command, " "))
|
|
|
cmd := exec.Command(p.Command[0], p.Command[1:]...)
|
|
@@ -140,10 +130,10 @@ func main() {
|
|
|
log.Print("[INFO] terminated")
|
|
|
}
|
|
|
|
|
|
-func setup(o opts) (n notifier, pub Publisher, err error) {
|
|
|
+func setup(o opts) (n notifier, pub publisher.Interface, err error) {
|
|
|
n = &rss.Notify{Feed: o.Feed, Duration: o.Refresh, Timeout: o.TimeOut, IncludeFirst: o.IncludeFirst}
|
|
|
if o.Dry {
|
|
|
- pub = Stdout{}
|
|
|
+ pub = publisher.Stdout{}
|
|
|
} else {
|
|
|
cmd := append([]string{o.Command.Name}, o.Command.Args...)
|
|
|
pub = CommandPublisher{Command: cmd}
|
|
@@ -153,11 +143,11 @@ func setup(o opts) (n notifier, pub Publisher, err error) {
|
|
|
}
|
|
|
|
|
|
// do runs event loop getting rss events and publishing them
|
|
|
-func do(ctx context.Context, notif notifier, pub Publisher) {
|
|
|
+func do(ctx context.Context, notif notifier, pub publisher.Interface) {
|
|
|
ch := notif.Go(ctx)
|
|
|
for event := range ch {
|
|
|
err := pub.Publish(event, func(r rss.Event) string {
|
|
|
- return ""
|
|
|
+ return event.Title
|
|
|
})
|
|
|
if err != nil {
|
|
|
log.Printf("[WARN] failed to publish, %s", err)
|