-
Notifications
You must be signed in to change notification settings - Fork 17
/
config_group.go
45 lines (39 loc) · 1.14 KB
/
config_group.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
package devices
import (
"fmt"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/foundriesio/fioctl/subcommands"
)
func init() {
groupCmd := &cobra.Command{
Use: "group <device> [<group>]",
Short: "Assign a device to an existing factory device group",
Run: doConfigGroup,
Args: cobra.RangeArgs(1, 2),
}
groupCmd.Flags().Bool("unset", false, "Unset an associated device group")
configCmd.AddCommand(groupCmd)
}
func doConfigGroup(cmd *cobra.Command, args []string) {
factory := viper.GetString("factory")
device := args[0]
unset, _ := cmd.Flags().GetBool("unset")
var group string
if unset {
if len(args) == 2 {
subcommands.DieNotNil(fmt.Errorf("Cannot assign and unset a device group in one command"))
}
group = ""
logrus.Debugf("Unsetting a device group from device %s", device)
} else {
if len(args) == 1 {
subcommands.DieNotNil(fmt.Errorf("Either device group or --unset option must be provided"))
}
group = args[1]
logrus.Debugf("Assigning device %s to group %s", device, group)
}
err := api.DeviceSetGroup(factory, device, group)
subcommands.DieNotNil(err)
}