-
Notifications
You must be signed in to change notification settings - Fork 0
/
discovery.go
31 lines (26 loc) · 963 Bytes
/
discovery.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
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package util
import (
"io/ioutil"
"path"
"github.com/TheThingsNetwork/api/discovery"
ttnlog "github.com/TheThingsNetwork/go-utils/log"
"github.com/TheThingsNetwork/ttn/api"
"github.com/TheThingsNetwork/ttn/api/pool"
"github.com/spf13/viper"
"google.golang.org/grpc"
)
// GetDiscovery gets the Discovery client for ttnctl
func GetDiscovery(ctx ttnlog.Interface) (*grpc.ClientConn, discovery.DiscoveryClient) {
path := path.Join(GetDataDir(), "/ca.cert")
cert, err := ioutil.ReadFile(path)
if err == nil && !pool.RootCAs.AppendCertsFromPEM(cert) {
ctx.Warnf("Could not add root certificates from %s", path)
}
conn, err := api.Dial(viper.GetString("discovery-address"))
if err != nil {
ctx.WithError(err).Fatal("Could not connect to Discovery server")
}
return conn, discovery.NewDiscoveryClient(conn)
}