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 10, 2024
1 parent 2645c52 commit 89be418
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion subcommands/devices/cmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package devices

import (
"fmt"

"golang.org/x/exp/slices"

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

Expand Down Expand Up @@ -60,10 +64,31 @@ 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, err := cmd.Flags().GetBool("by-uuid")
if err != nil {
fmt.Println("ERROR:", err)
}
if byUuid && err == nil {
return api.DeviceApiByUuid(viper.GetString("factory"), name)
}
return api.DeviceApiByName(viper.GetString("factory"), name)
}

Expand Down

0 comments on commit 89be418

Please sign in to comment.