/
events.go
103 lines (92 loc) · 2.61 KB
/
events.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package commands
import (
"os"
"sync"
"github.com/spf13/cobra"
"go.thethings.network/lorawan-stack/cmd/ttn-lw-cli/internal/api"
"go.thethings.network/lorawan-stack/cmd/ttn-lw-cli/internal/io"
"go.thethings.network/lorawan-stack/pkg/errors"
"go.thethings.network/lorawan-stack/pkg/ttnpb"
)
var eventsCommand = &cobra.Command{
Use: "events",
Aliases: []string{"event", "evt", "e"},
Short: "Subscribe to events",
RunE: func(cmd *cobra.Command, args []string) error {
var wg sync.WaitGroup
addresses := make(map[string]bool)
addresses[config.IdentityServerGRPCAddress] = true
if config.GatewayServerEnabled {
addresses[config.GatewayServerGRPCAddress] = true
}
if config.NetworkServerEnabled {
addresses[config.NetworkServerGRPCAddress] = true
}
if config.ApplicationServerEnabled {
addresses[config.ApplicationServerGRPCAddress] = true
}
if config.JoinServerEnabled {
addresses[config.JoinServerGRPCAddress] = true
}
ids := getCombinedIdentifiers(cmd.Flags()).GetEntityIdentifiers()
if len(ids) == 0 {
return errNoIDs
}
tail, _ := cmd.Flags().GetUint32("tail")
req := &ttnpb.StreamEventsRequest{
Identifiers: ids,
Tail: tail,
}
events := make(chan *ttnpb.Event)
for address := range addresses {
conn, err := api.Dial(ctx, address)
if err != nil {
return err
}
stream, err := ttnpb.NewEventsClient(conn).Stream(ctx, req)
if err != nil {
return err
}
wg.Add(1)
go func() {
for {
event, err := stream.Recv()
if err != nil {
if !errors.IsCanceled(err) {
logger.WithError(err).Warn("Event stream closed")
}
break
}
events <- event
}
wg.Done()
}()
}
go func() {
wg.Wait()
close(events)
}()
for evt := range events {
io.Write(os.Stdout, config.OutputFormat, evt)
}
return ctx.Err()
},
}
func init() {
eventsCommand.Flags().AddFlagSet(combinedIdentifiersFlags())
eventsCommand.Flags().Uint32("tail", 0, "")
Root.AddCommand(eventsCommand)
}