|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2023 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to license@arduino.cc. |
| 15 | + |
| 16 | +package debug |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "os" |
| 21 | + |
| 22 | + "github.com/arduino/arduino-cli/commands/debug" |
| 23 | + "github.com/arduino/arduino-cli/internal/cli/arguments" |
| 24 | + "github.com/arduino/arduino-cli/internal/cli/feedback" |
| 25 | + "github.com/arduino/arduino-cli/internal/cli/feedback/result" |
| 26 | + "github.com/arduino/arduino-cli/internal/cli/instance" |
| 27 | + rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" |
| 28 | + "github.com/sirupsen/logrus" |
| 29 | + "github.com/spf13/cobra" |
| 30 | +) |
| 31 | + |
| 32 | +func newDebugCheckCommand() *cobra.Command { |
| 33 | + var ( |
| 34 | + fqbnArg arguments.Fqbn |
| 35 | + portArgs arguments.Port |
| 36 | + interpreter string |
| 37 | + programmer arguments.Programmer |
| 38 | + ) |
| 39 | + debugCheckCommand := &cobra.Command{ |
| 40 | + Use: "check", |
| 41 | + Short: tr("Check if the given board/programmer combination supports debugging."), |
| 42 | + Example: " " + os.Args[0] + " debug check -b arduino:samd:mkr1000 -P atmel_ice", |
| 43 | + Run: func(cmd *cobra.Command, args []string) { |
| 44 | + runDebugCheckCommand(&portArgs, &fqbnArg, interpreter, &programmer) |
| 45 | + }, |
| 46 | + } |
| 47 | + fqbnArg.AddToCommand(debugCheckCommand) |
| 48 | + portArgs.AddToCommand(debugCheckCommand) |
| 49 | + programmer.AddToCommand(debugCheckCommand) |
| 50 | + debugCheckCommand.Flags().StringVar(&interpreter, "interpreter", "console", tr("Debug interpreter e.g.: %s", "console, mi, mi1, mi2, mi3")) |
| 51 | + return debugCheckCommand |
| 52 | +} |
| 53 | + |
| 54 | +func runDebugCheckCommand(portArgs *arguments.Port, fqbnArg *arguments.Fqbn, interpreter string, programmerArg *arguments.Programmer) { |
| 55 | + instance := instance.CreateAndInit() |
| 56 | + logrus.Info("Executing `arduino-cli debug`") |
| 57 | + |
| 58 | + port, err := portArgs.GetPort(instance, "", "") |
| 59 | + if err != nil { |
| 60 | + feedback.FatalError(err, feedback.ErrBadArgument) |
| 61 | + } |
| 62 | + fqbn := fqbnArg.String() |
| 63 | + resp, err := debug.IsDebugSupported(context.Background(), &rpc.IsDebugSupportedRequest{ |
| 64 | + Instance: instance, |
| 65 | + Fqbn: fqbn, |
| 66 | + Port: port, |
| 67 | + Interpreter: interpreter, |
| 68 | + Programmer: programmerArg.String(instance, fqbn), |
| 69 | + }) |
| 70 | + if err != nil { |
| 71 | + feedback.FatalError(err, feedback.ErrGeneric) |
| 72 | + } |
| 73 | + feedback.PrintResult(&debugCheckResult{result.NewIsDebugSupportedResponse(resp)}) |
| 74 | +} |
| 75 | + |
| 76 | +type debugCheckResult struct { |
| 77 | + Result *result.IsDebugSupportedResponse |
| 78 | +} |
| 79 | + |
| 80 | +func (d *debugCheckResult) Data() interface{} { |
| 81 | + return d.Result |
| 82 | +} |
| 83 | + |
| 84 | +func (d *debugCheckResult) String() string { |
| 85 | + if d.Result.Supported { |
| 86 | + return tr("The given board/programmer configuration supports debugging.") |
| 87 | + } |
| 88 | + return tr("The given board/programmer configuration does NOT support debugging.") |
| 89 | +} |
0 commit comments