Przeglądaj źródła

some refactoring

boyska 4 lat temu
rodzic
commit
7404ddd772
2 zmienionych plików z 8 dodań i 18 usunięć
  1. 7 17
      app/cmd/rss2cmd/main.go
  2. 1 1
      app/cmd/rss2twitter/main_test.go

+ 7 - 17
app/cmd/rss2cmd/main.go

@@ -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)

+ 1 - 1
app/cmd/rss2twitter/main_test.go

@@ -15,10 +15,10 @@ import (
 	"testing"
 	"time"
 
+	"git.lattuga.net/boyska/rss2twitter/app/rss"
 	log "github.com/go-pkgz/lgr"
 	"github.com/stretchr/testify/assert"
 	"github.com/stretchr/testify/require"
-	"git.lattuga.net/boyska/rss2twitter/app/rss"
 )
 
 func TestMain(t *testing.T) {