|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2020 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 [email protected]. |
| 15 | + |
| 16 | +package board |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + "os" |
| 21 | + |
| 22 | + "github.com/arduino/arduino-cli/cli/arguments" |
| 23 | + "github.com/arduino/arduino-cli/cli/errorcodes" |
| 24 | + "github.com/arduino/arduino-cli/cli/feedback" |
| 25 | + "github.com/spf13/cobra" |
| 26 | +) |
| 27 | + |
| 28 | +func initAttachCommand() *cobra.Command { |
| 29 | + var port arguments.Port |
| 30 | + attachCommand := &cobra.Command{ |
| 31 | + Use: fmt.Sprintf("attach [-p <%s>] [-b <%s>] [%s]", tr("port"), tr("FQBN"), tr("sketchPath")), |
| 32 | + Short: tr("Attaches a sketch to a board."), |
| 33 | + Long: tr("Attaches a sketch to a board."), |
| 34 | + Example: " " + os.Args[0] + " board attach -p /dev/ttyACM0\n" + |
| 35 | + " " + os.Args[0] + " board attach -p /dev/ttyACM0 HelloWorld\n" + |
| 36 | + " " + os.Args[0] + " board attach -b arduino:samd:mkr1000", |
| 37 | + Args: cobra.MaximumNArgs(1), |
| 38 | + Run: func(cmd *cobra.Command, args []string) { |
| 39 | + sketchPath := "" |
| 40 | + if len(args) > 0 { |
| 41 | + sketchPath = args[0] |
| 42 | + } |
| 43 | + runAttachCommand(sketchPath, &port, fqbn.String()) |
| 44 | + }, |
| 45 | + } |
| 46 | + fqbn.AddToCommand(attachCommand) |
| 47 | + port.AddToCommand(attachCommand) |
| 48 | + |
| 49 | + return attachCommand |
| 50 | +} |
| 51 | + |
| 52 | +func runAttachCommand(path string, port *arguments.Port, fqbn string) { |
| 53 | + sketchPath := arguments.InitSketchPath(path) |
| 54 | + sk := arguments.NewSketch(sketchPath) |
| 55 | + |
| 56 | + address, protocol, _ := port.GetPortAddressAndProtocol(nil, sk) |
| 57 | + if address != "" { |
| 58 | + sk.Metadata.CPU.Port = address |
| 59 | + sk.Metadata.CPU.Protocol = protocol |
| 60 | + msg := fmt.Sprintf("%s: %s", tr("Set port to"), address) |
| 61 | + if protocol != "" { |
| 62 | + msg += " / " + protocol |
| 63 | + } |
| 64 | + feedback.Print(msg) |
| 65 | + } |
| 66 | + if fqbn != "" { |
| 67 | + sk.Metadata.CPU.Fqbn = fqbn |
| 68 | + msg := fmt.Sprintf("%s: %s", tr("Set FQBN to"), fqbn) |
| 69 | + feedback.Print(msg) |
| 70 | + } |
| 71 | + if err := sk.ExportMetadata(); err != nil { |
| 72 | + feedback.Errorf("%s: %s", tr("Error saving sketch metadata"), err) |
| 73 | + os.Exit(errorcodes.ErrGeneric) |
| 74 | + } |
| 75 | +} |
0 commit comments