-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateways_register.go
59 lines (47 loc) · 1.52 KB
/
gateways_register.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
// 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 cmd
import (
"strings"
"github.com/TheThingsNetwork/api"
"github.com/TheThingsNetwork/go-account-lib/account"
"github.com/TheThingsNetwork/ttn/ttnctl/util"
"github.com/spf13/cobra"
)
var gatewaysRegisterCmd = &cobra.Command{
Use: "register [GatewayID] [FrequencyPlan] [Location]",
Short: "Register a gateway",
Long: `ttnctl gateways register can be used to register a gateway`,
Example: `$ ttnctl gateways register test US 52.37403,4.88968
INFO Registered gateway Gateway ID=test
`,
Run: func(cmd *cobra.Command, args []string) {
assertArgsLength(cmd, args, 2, 3)
gatewayID := strings.ToLower(args[0])
if err := api.NotEmptyAndValidID(gatewayID, "Gateway ID"); err != nil {
ctx.Fatal(err.Error())
}
frequencyPlan := args[1]
var err error
var location *account.Location
if len(args) == 3 {
location, err = util.ParseLocation(args[2])
if err != nil {
ctx.WithError(err).Fatal("Invalid location")
}
}
settings := account.GatewaySettings{
AntennaLocation: location,
}
act := util.GetAccount(ctx)
gateway, err := act.RegisterGateway(gatewayID, frequencyPlan, settings)
if err != nil {
ctx.WithError(err).Fatal("Could not register gateway")
}
util.ForceRefreshToken(ctx)
ctx.WithField("Gateway ID", gateway.ID).Info("Registered Gateway")
},
}
func init() {
gatewaysCmd.AddCommand(gatewaysRegisterCmd)
}