github.com/robgonnella/ardi/v2@v2.4.5-0.20230102052001-11a49de978c3/commands/attach_and_watch.go (about) 1 package commands 2 3 import ( 4 "errors" 5 6 cli "github.com/robgonnella/ardi/v2/cli-wrapper" 7 "github.com/robgonnella/ardi/v2/core" 8 "github.com/spf13/cobra" 9 ) 10 11 func getWatchCmd(env *CommandEnv) *cobra.Command { 12 var fqbn string 13 var buildProps []string 14 var port string 15 var baud int 16 var watchCmd = &cobra.Command{ 17 Use: "attach-and-watch [sketch|build]", 18 Aliases: []string{"develop", "dev"}, 19 Short: "Compile, upload, attach, and watch for changes", 20 Long: "\nCompile, upload, watch board logs, and watch for sketch " + 21 "changes. Updates to the sketch file will trigger automatic recompile, " + 22 "reupload, and restarts the board log watcher. If the sketch argument " + 23 "matches a user defined build in ardi.json, the build values will be " + 24 "used for compilation, upload, and watch path", 25 RunE: func(cmd *cobra.Command, args []string) error { 26 if err := requireProjectInit(); err != nil { 27 return err 28 } 29 optsList, err := env.ArdiCore.GetCompileOptsFromArgs(fqbn, buildProps, false, args) 30 if err != nil { 31 return err 32 } 33 34 opts := optsList[0] 35 36 baudRate := env.ArdiCore.GetBaudFromArgs(baud, args) 37 38 // Ignore errors here as user may have provided fqbn via build to mitigate 39 // custom boards that don't show up via auto detect for some reason 40 board, _ := env.ArdiCore.Cli.GetTargetBoard(fqbn, port, true) 41 42 if board == nil && opts.FQBN != "" && port != "" { 43 board = &cli.BoardWithPort{FQBN: opts.FQBN, Port: port} 44 } 45 46 if board == nil { 47 return errors.New("no connected boards detected") 48 } 49 50 opts.FQBN = board.FQBN 51 52 if err := env.ArdiCore.Compiler.Compile(*opts); err != nil { 53 return err 54 } 55 56 if err := env.ArdiCore.Uploader.Upload(board, opts.SketchDir); err != nil { 57 return err 58 } 59 60 targets := core.WatchCoreTargets{ 61 Board: board, 62 CompileOpts: opts, 63 Baud: baudRate, 64 } 65 66 env.ArdiCore.Watcher.SetTargets(targets) 67 68 defer env.ArdiCore.Watcher.Stop() 69 return env.ArdiCore.Watcher.Watch() 70 }, 71 } 72 73 watchCmd.Flags().StringVarP(&fqbn, "fqbn", "f", "", "Specify fully qualified board name") 74 watchCmd.Flags().IntVarP(&baud, "baud", "b", 0, "Specify baud rate") 75 watchCmd.Flags().StringArrayVarP(&buildProps, "build-prop", "p", []string{}, "Specify build property to compiler") 76 watchCmd.Flags().StringVar(&port, "port", "", "The port your arduino board is connected to") 77 return watchCmd 78 }