Skip to content

Commit

Permalink
device: Add --by-uuid support to all subcommands
Browse files Browse the repository at this point in the history
Signed-off-by: Andy Doan <[email protected]>
  • Loading branch information
doanac committed Jun 6, 2024
1 parent 2645c52 commit a7e1393
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion subcommands/devices/cmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package devices

import (
"golang.org/x/exp/slices"

"github.com/spf13/cobra"
"github.com/spf13/viper"

Expand Down Expand Up @@ -60,10 +62,28 @@ func NewCommand() *cobra.Command {

cmd.AddCommand(configCmd)
cmd.AddCommand(updatesCmd)

addUuidFlagToChildren(cmd)

return cmd
}

func getDeviceApi(_ *cobra.Command, name string) client.DeviceApi {
func addUuidFlagToChildren(c *cobra.Command) {
ignores := []string{"list-denied", "list", "delete-denied"}
for _, child := range c.Commands() {
if child.HasSubCommands() {
addUuidFlagToChildren(child)
} else if !slices.Contains(ignores, child.Name()) {
child.Flags().BoolP("by-uuid", "u", false, "Look up device by UUID rather than name")
}
}
}

func getDeviceApi(cmd *cobra.Command, name string) client.DeviceApi {
byUuid, _ := cmd.Flags().GetBool("by-uuid")
if byUuid {
return api.DeviceApiByUuid(viper.GetString("factory"), name)
}
return api.DeviceApiByName(viper.GetString("factory"), name)
}

Expand Down

0 comments on commit a7e1393

Please sign in to comment.