/
context.go
40 lines (35 loc) · 1.17 KB
/
context.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
// 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 (
"fmt"
"os"
"os/user"
"github.com/TheThingsNetwork/go-utils/grpc/ttnctx"
ttnlog "github.com/TheThingsNetwork/go-utils/log"
"github.com/spf13/viper"
"golang.org/x/net/context" // See https://github.com/grpc/grpc-go/issues/711"
)
// GetID retrns the ID of this ttnctl
func GetID() string {
id := "ttnctl"
if user, err := user.Current(); err == nil {
id += "-" + user.Username
}
if hostname, err := os.Hostname(); err == nil {
id += "@" + hostname
}
return id
}
// GetContext returns a new context
func GetContext(log ttnlog.Interface, extraPairs ...string) context.Context {
token, err := GetTokenSource(log).Token()
if err != nil {
log.WithError(err).Fatal("Could not get token")
}
ctx := context.Background()
ctx = ttnctx.OutgoingContextWithID(ctx, GetID())
ctx = ttnctx.OutgoingContextWithServiceInfo(ctx, "ttnctl", fmt.Sprintf("%s-%s (%s)", viper.GetString("version"), viper.GetString("gitCommit"), viper.GetString("buildDate")), "")
ctx = ttnctx.OutgoingContextWithToken(ctx, token.AccessToken)
return ctx
}