forked from TheThingsArchive/ttn
/
gateways_register.go
56 lines (46 loc) · 1.43 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
// Copyright © 2016 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 (
"github.com/TheThingsNetwork/go-account-lib/account"
"github.com/TheThingsNetwork/ttn/api"
"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) {
if len(args) != 2 && len(args) != 3 {
cmd.UsageFunc()(cmd)
return
}
gatewayID := args[0]
if !api.ValidID(gatewayID) {
ctx.Fatal("Invalid Gateway ID")
}
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")
}
}
act := util.GetAccount(ctx)
gateway, err := act.RegisterGateway(gatewayID, frequencyPlan, location)
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)
}