From e99348c66d47b4ddd60f357d6ee17edf938c49de Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Tue, 26 May 2026 23:15:59 +0200 Subject: [PATCH] no more init() and package level config. --- cmd/bridge/cli.go | 79 +++++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 41 deletions(-) diff --git a/cmd/bridge/cli.go b/cmd/bridge/cli.go index 6cfc38d..3442100 100644 --- a/cmd/bridge/cli.go +++ b/cmd/bridge/cli.go @@ -30,59 +30,56 @@ func newConfig() *config { return &config{} } -var rootCmd = &cobra.Command{ - Use: "bridge", - Short: "ANT+ to Bluetooth LE bridge", - Long: `Bridge an ANT+ cycling power trainer over Bluetooth Low Energy. +func main() { + rootCmd := &cobra.Command{ + Use: "bridge", + Short: "ANT+ to Bluetooth LE bridge", + Long: `Bridge an ANT+ cycling power trainer over Bluetooth Low Energy. This allows these trainers to be used with modern devices that don't support ANT+.`, - Version: fmt.Sprintf("%s (built %s)", Version, BuildTime), - RunE: func(cmd *cobra.Command, args []string) error { - c := newConfig() + Version: fmt.Sprintf("%s (built %s)", Version, BuildTime), + RunE: func(cmd *cobra.Command, args []string) error { + c := newConfig() - c.vendor = cmd.Flags().Lookup("vendor").Value.String() - c.product = cmd.Flags().Lookup("product").Value.String() - c.device = cmd.Flags().Lookup("device").Value.String() + c.vendor = cmd.Flags().Lookup("vendor").Value.String() + c.product = cmd.Flags().Lookup("product").Value.String() + c.device = cmd.Flags().Lookup("device").Value.String() - vendorID, err := parseHex(c.vendor, 16) - if err != nil { - return fmt.Errorf("invalid --vendor %q: %w", c.vendor, err) - } - productID, err := parseHex(c.product, 16) - if err != nil { - return fmt.Errorf("invalid --product %q: %w", c.product, err) - } - deviceNumber, err := parseHex(c.device, 32) - if err != nil { - return fmt.Errorf("invalid --device %q: %w", c.device, err) - } + vendorID, err := parseHex(c.vendor, 16) + if err != nil { + return fmt.Errorf("invalid --vendor %q: %w", c.vendor, err) + } + productID, err := parseHex(c.product, 16) + if err != nil { + return fmt.Errorf("invalid --product %q: %w", c.product, err) + } + deviceNumber, err := parseHex(c.device, 32) + if err != nil { + return fmt.Errorf("invalid --device %q: %w", c.device, err) + } - c.vendorID = vendorID - c.productID = productID - c.deviceNumber = uint32(deviceNumber) + c.vendorID = vendorID + c.productID = productID + c.deviceNumber = uint32(deviceNumber) - logMsgf("ANT Bridge %s built %s", Version, BuildTime) - logMsgf("vendor: 0x%04x/%d", vendorID, vendorID) - logMsgf("product: 0x%04x/%d", productID, productID) - logMsgf("device: 0x%03x/%d", deviceNumber, deviceNumber) + logMsgf("ANT Bridge %s built %s", Version, BuildTime) + logMsgf("vendor: 0x%04x/%d", vendorID, vendorID) + logMsgf("product: 0x%04x/%d", productID, productID) + logMsgf("device: 0x%03x/%d", deviceNumber, deviceNumber) - exec.Command("hciconfig", "hci0", "down").Run() - exec.Command("hciconfig", "hci0", "up").Run() + exec.Command("hciconfig", "hci0", "down").Run() + exec.Command("hciconfig", "hci0", "up").Run() - events := make(chan Event) - go ListenForTrainer(uint16(c.vendorID), uint16(c.productID), c.deviceNumber, events) - ExposeBluetooth(events) + events := make(chan Event) + go ListenForTrainer(uint16(c.vendorID), uint16(c.productID), c.deviceNumber, events) + ExposeBluetooth(events) - return nil - }, -} + return nil + }, + } -func init() { rootCmd.Flags().String("vendor", "0x0fcf", "USB vendor ID (hex, e.g. 0x0fcf)") rootCmd.Flags().String("product", "0x1008", "USB product ID (hex, e.g. 0x1008)") rootCmd.Flags().String("device", "3001", "ANT+ device number (hex or decimal, e.g. 3001 or 0xbb9)") -} - -func main() { if err := rootCmd.Execute(); err != nil { os.Exit(1) }