Add flag --command to the stream command
authorMikael Berthe <mikael@lilotux.net>
Tue, 09 May 2017 20:14:28 +0200
changeset 112 58d30ab47543
parent 111 863b61d682e1
child 113 2e411da68fd3
Add flag --command to the stream command If --command is set to the path of an executable file, it will be run for each event and the output be be sent to the standard input. The --output or --theme options can still be used.
cmd/stream.go
--- a/cmd/stream.go	Tue May 09 20:14:28 2017 +0200
+++ b/cmd/stream.go	Tue May 09 20:14:28 2017 +0200
@@ -16,11 +16,9 @@
 	"github.com/McKael/madon"
 )
 
-/*
 var streamOpts struct {
-	local bool
+	command string
 }
-*/
 
 // Maximum number of websockets (1 hashtag <=> 1 ws)
 const maximumHashtagStreamWS = 4
@@ -52,7 +50,7 @@
 func init() {
 	RootCmd.AddCommand(streamCmd)
 
-	//streamCmd.Flags().BoolVar(&streamOpts.local, "local", false, "Events from the local instance")
+	streamCmd.Flags().StringVar(&streamOpts.command, "command", "", "Execute external command")
 }
 
 func streamRunE(cmd *cobra.Command, args []string) error {
@@ -155,6 +153,9 @@
 		os.Exit(1)
 	}
 
+	// Set up external command
+	p.setCommand(streamOpts.command)
+
 LISTEN:
 	for {
 		select {
@@ -176,11 +177,15 @@
 				errPrint("Event: [%s]", ev.Event)
 			case "update":
 				s := ev.Data.(madon.Status)
-				p.printObj(&s)
+				if err = p.printObj(&s); err != nil {
+					break LISTEN
+				}
 				continue
 			case "notification":
 				n := ev.Data.(madon.Notification)
-				p.printObj(&n)
+				if p.printObj(&n); err != nil {
+					break LISTEN
+				}
 				continue
 			case "delete":
 				// TODO PrintObj ?
@@ -192,5 +197,9 @@
 	}
 	close(stop)
 	close(evChan)
+	if err != nil {
+		errPrint("Error: %s", err.Error())
+		os.Exit(1)
+	}
 	return nil
 }