author | convert-repo |
Fri, 05 May 2017 21:55:09 +0000 | |
changeset 69 | 7217174c217a |
parent 47 | 82d8b6074309 |
child 74 | 78e1c63e4bbe |
permissions | -rw-r--r-- |
0 | 1 |
// Copyright © 2017 Mikael Berthe <mikael@lilotux.net> |
2 |
// |
|
3 |
// Licensed under the MIT license. |
|
4 |
// Please see the LICENSE file is this directory. |
|
5 |
||
6 |
package cmd |
|
7 |
||
8 |
import ( |
|
9 |
"io" |
|
47
82d8b6074309
Set exit code to non-zero when API calls fail
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
10 |
"os" |
0 | 11 |
|
45
b58a7ea1aeb2
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
44
diff
changeset
|
12 |
"github.com/pkg/errors" |
0 | 13 |
"github.com/spf13/cobra" |
14 |
||
15 |
"github.com/McKael/madon" |
|
16 |
) |
|
17 |
||
18 |
/* |
|
19 |
var streamOpts struct { |
|
20 |
local bool |
|
21 |
} |
|
22 |
*/ |
|
23 |
||
24 |
// streamCmd represents the stream command |
|
25 |
var streamCmd = &cobra.Command{ |
|
26 |
Use: "stream [user|local|public|:HASHTAG]", |
|
27 |
Short: "Listen to an event stream", |
|
28 |
Long: ` |
|
29 |
The stream command stays connected to the server and listen to a stream of |
|
30 |
events (user, local or federated). |
|
31 |
It can also get a hashtag-based stream if the keyword or prefixed with |
|
32 |
':' or '#'.`, |
|
33 |
Example: ` madonctl stream # User timeline stream |
|
34 |
madonctl stream local # Local timeline stream |
|
35 |
madonctl stream public # Public timeline stream |
|
36 |
madonctl stream :mastodon # Hashtag |
|
37 |
madonctl stream #madonctl`, |
|
38 |
RunE: streamRunE, |
|
39 |
ValidArgs: []string{"user", "public"}, |
|
40 |
ArgAliases: []string{"home"}, |
|
41 |
} |
|
42 |
||
43 |
func init() { |
|
44 |
RootCmd.AddCommand(streamCmd) |
|
45 |
||
46 |
//streamCmd.Flags().BoolVar(&streamOpts.local, "local", false, "Events from the local instance") |
|
47 |
} |
|
48 |
||
49 |
func streamRunE(cmd *cobra.Command, args []string) error { |
|
50 |
streamName := "user" |
|
51 |
tag := "" |
|
52 |
||
53 |
if len(args) > 0 { |
|
54 |
if len(args) != 1 { |
|
55 |
return errors.New("too many parameters") |
|
56 |
} |
|
57 |
arg := args[0] |
|
58 |
switch arg { |
|
59 |
case "", "user": |
|
60 |
case "public": |
|
61 |
streamName = arg |
|
62 |
case "local": |
|
63 |
streamName = "public:local" |
|
64 |
default: |
|
65 |
if arg[0] != ':' && arg[0] != '#' { |
|
66 |
return errors.New("invalid argument") |
|
67 |
} |
|
68 |
streamName = "hashtag" |
|
69 |
tag = arg[1:] |
|
70 |
if len(tag) == 0 { |
|
71 |
return errors.New("empty hashtag") |
|
72 |
} |
|
73 |
} |
|
74 |
} |
|
75 |
||
76 |
if err := madonInit(true); err != nil { |
|
77 |
return err |
|
78 |
} |
|
79 |
||
80 |
evChan := make(chan madon.StreamEvent, 10) |
|
81 |
stop := make(chan bool) |
|
82 |
done := make(chan bool) |
|
83 |
||
84 |
// StreamListener(name string, hashTag string, events chan<- madon.StreamEvent, stopCh <-chan bool, doneCh chan<- bool) error |
|
85 |
err := gClient.StreamListener(streamName, tag, evChan, stop, done) |
|
86 |
if err != nil { |
|
87 |
errPrint("Error: %s", err.Error()) |
|
47
82d8b6074309
Set exit code to non-zero when API calls fail
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
88 |
os.Exit(1) |
0 | 89 |
} |
90 |
||
91 |
p, err := getPrinter() |
|
92 |
if err != nil { |
|
93 |
close(stop) |
|
94 |
<-done |
|
95 |
close(evChan) |
|
96 |
return err |
|
97 |
} |
|
98 |
||
99 |
LISTEN: |
|
100 |
for { |
|
101 |
select { |
|
102 |
case _, ok := <-done: |
|
103 |
if !ok { // done is closed, end of streaming |
|
104 |
done = nil |
|
105 |
break LISTEN |
|
106 |
} |
|
107 |
case ev := <-evChan: |
|
108 |
switch ev.Event { |
|
109 |
case "error": |
|
110 |
if ev.Error != nil { |
|
111 |
if ev.Error == io.ErrUnexpectedEOF { |
|
112 |
errPrint("The stream connection was unexpectedly closed") |
|
113 |
continue |
|
114 |
} |
|
115 |
errPrint("Error event: [%s] %s", ev.Event, ev.Error) |
|
116 |
continue |
|
117 |
} |
|
118 |
errPrint("Event: [%s]", ev.Event) |
|
119 |
case "update": |
|
120 |
s := ev.Data.(madon.Status) |
|
121 |
p.PrintObj(&s, nil, "") |
|
122 |
continue |
|
123 |
case "notification": |
|
124 |
n := ev.Data.(madon.Notification) |
|
125 |
p.PrintObj(&n, nil, "") |
|
126 |
continue |
|
127 |
case "delete": |
|
128 |
// TODO PrintObj ? |
|
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
129 |
errPrint("Event: [%s] Status %d was deleted", ev.Event, ev.Data.(int64)) |
0 | 130 |
default: |
131 |
errPrint("Unhandled event: [%s] %T", ev.Event, ev.Data) |
|
132 |
} |
|
133 |
} |
|
134 |
} |
|
135 |
close(evChan) |
|
136 |
return nil |
|
137 |
} |