This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 279
/
devices_delete.go
60 lines (49 loc) · 1.54 KB
/
devices_delete.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
// 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 (
"fmt"
"strings"
"github.com/TheThingsNetwork/api"
ttnlog "github.com/TheThingsNetwork/go-utils/log"
"github.com/TheThingsNetwork/ttn/ttnctl/util"
"github.com/spf13/cobra"
)
var devicesDeleteCmd = &cobra.Command{
Use: "delete [Device ID]",
Short: "Delete a device",
Long: `ttnctl devices delete can be used to delete a device.`,
Example: `$ ttnctl devices delete test
INFO Using Application AppID=test
Are you sure you want to delete device test from application test?
> yes
INFO Discovering Handler...
INFO Connecting with Handler...
INFO Deleted device AppID=test DevID=test
`,
Run: func(cmd *cobra.Command, args []string) {
assertArgsLength(cmd, args, 1, 1)
devID := strings.ToLower(args[0])
if err := api.NotEmptyAndValidID(devID, "Device ID"); err != nil {
ctx.Fatal(err.Error())
}
appID := util.GetAppID(ctx)
if !confirm(fmt.Sprintf("Are you sure you want to delete device %s from application %s?", devID, appID)) {
ctx.Info("Not doing anything")
return
}
conn, manager := util.GetHandlerManager(ctx, appID)
defer conn.Close()
err := manager.DeleteDevice(appID, devID)
if err != nil {
ctx.WithError(err).Fatal("Could not delete device.")
}
ctx.WithFields(ttnlog.Fields{
"AppID": appID,
"DevID": devID,
}).Info("Deleted device")
},
}
func init() {
devicesCmd.AddCommand(devicesDeleteCmd)
}