package cli import ( "fmt" "sort" "text/tabwriter" "github.com/spf13/cobra" "galahad2lcd/internal/device" ) func newListCmd() *cobra.Command { return &cobra.Command{ Use: "list", Short: "List USB devices visible to the driver", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { ctx, stop := newSignalContext() defer stop() infos, err := device.ListDevices(ctx) if err != nil { return err } if len(infos) == 0 { fmt.Fprintln(cmd.OutOrStdout(), "no USB devices found (are permissions configured?)") return nil } sort.Slice(infos, func(i, j int) bool { return infos[i].Bus < infos[j].Bus || (infos[i].Bus == infos[j].Bus && infos[i].Address < infos[j].Address) }) w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 4, 2, ' ', 0) fmt.Fprintln(w, "BUS\tADDR\tVID:PID\tPRODUCT\t") for _, info := range infos { product := info.Product if product == "" { product = "-" } marker := " " if info.IsLCD { marker = "*" } fmt.Fprintf(w, "%s%d\t%d\t%04x:%04x\t%s\t\n", marker, info.Bus, info.Address, info.VendorID, info.ProductID, product) } fmt.Fprintln(w, "* Lian Li Galahad II LCD (0416:7395)") return w.Flush() }, } }