|
| 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 license@arduino.cc. |
| 15 | + |
| 16 | +package burnbootloader |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "os" |
| 21 | + |
| 22 | + "github.com/arduino/arduino-cli/cli/errorcodes" |
| 23 | + "github.com/arduino/arduino-cli/cli/feedback" |
| 24 | + "github.com/arduino/arduino-cli/cli/instance" |
| 25 | + "github.com/arduino/arduino-cli/commands/upload" |
| 26 | + rpc "github.com/arduino/arduino-cli/rpc/commands" |
| 27 | + "github.com/arduino/arduino-cli/table" |
| 28 | + "github.com/arduino/go-paths-helper" |
| 29 | + "github.com/sirupsen/logrus" |
| 30 | + "github.com/spf13/cobra" |
| 31 | +) |
| 32 | + |
| 33 | +var ( |
| 34 | + fqbn string |
| 35 | + port string |
| 36 | + verbose bool |
| 37 | + verify bool |
| 38 | + importDir string |
| 39 | + programmer string |
| 40 | + burnBootloader bool |
| 41 | +) |
| 42 | + |
| 43 | +// NewCommand created a new `burn-bootloader` command |
| 44 | +func NewCommand() *cobra.Command { |
| 45 | + burnBootloaderCommand := &cobra.Command{ |
| 46 | + Use: "burn-bootloader", |
| 47 | + Short: "Upload the bootloader.", |
| 48 | + Long: "Upload the bootloader on the board using an external programmer.", |
| 49 | + Example: " " + os.Args[0] + " burn-bootloader -b arduino:avr:uno -P atmel-ice", |
| 50 | + Args: cobra.MaximumNArgs(1), |
| 51 | + Run: run, |
| 52 | + } |
| 53 | + |
| 54 | + burnBootloaderCommand.Flags().StringVarP(&fqbn, "fqbn", "b", "", "Fully Qualified Board Name, e.g.: arduino:avr:uno") |
| 55 | + burnBootloaderCommand.Flags().StringVarP(&port, "port", "p", "", "Upload port, e.g.: COM10 or /dev/ttyACM0") |
| 56 | + burnBootloaderCommand.Flags().BoolVarP(&verify, "verify", "t", false, "Verify uploaded binary after the upload.") |
| 57 | + burnBootloaderCommand.Flags().BoolVarP(&verbose, "verbose", "v", false, "Turns on verbose mode.") |
| 58 | + burnBootloaderCommand.Flags().StringVarP(&programmer, "programmer", "P", "", "Use the specified programmer to upload or 'list' to list supported programmers.") |
| 59 | + |
| 60 | + return burnBootloaderCommand |
| 61 | +} |
| 62 | + |
| 63 | +func run(command *cobra.Command, args []string) { |
| 64 | + instance, err := instance.CreateInstance() |
| 65 | + if err != nil { |
| 66 | + feedback.Errorf("Error during Upload: %v", err) |
| 67 | + os.Exit(errorcodes.ErrGeneric) |
| 68 | + } |
| 69 | + |
| 70 | + if programmer == "list" { |
| 71 | + resp, err := upload.ListProgrammersAvailableForUpload(context.Background(), &rpc.ListProgrammersAvailableForUploadReq{ |
| 72 | + Instance: instance, |
| 73 | + Fqbn: fqbn, |
| 74 | + }) |
| 75 | + if err != nil { |
| 76 | + feedback.Errorf("Error listing programmers: %v", err) |
| 77 | + os.Exit(errorcodes.ErrGeneric) |
| 78 | + } |
| 79 | + feedback.PrintResult(&programmersList{ |
| 80 | + Programmers: resp.GetProgrammers(), |
| 81 | + }) |
| 82 | + os.Exit(0) |
| 83 | + } |
| 84 | + |
| 85 | + if _, err := upload.BurnBootloader(context.Background(), &rpc.BurnBootloaderReq{ |
| 86 | + Instance: instance, |
| 87 | + Fqbn: fqbn, |
| 88 | + Port: port, |
| 89 | + Verbose: verbose, |
| 90 | + Verify: verify, |
| 91 | + Programmer: programmer, |
| 92 | + }, os.Stdout, os.Stderr); err != nil { |
| 93 | + feedback.Errorf("Error during Upload: %v", err) |
| 94 | + os.Exit(errorcodes.ErrGeneric) |
| 95 | + } |
| 96 | + os.Exit(0) |
| 97 | +} |
| 98 | + |
| 99 | +// initSketchPath returns the current working directory |
| 100 | +func initSketchPath(sketchPath *paths.Path) *paths.Path { |
| 101 | + if sketchPath != nil { |
| 102 | + return sketchPath |
| 103 | + } |
| 104 | + |
| 105 | + wd, err := paths.Getwd() |
| 106 | + if err != nil { |
| 107 | + feedback.Errorf("Couldn't get current working directory: %v", err) |
| 108 | + os.Exit(errorcodes.ErrGeneric) |
| 109 | + } |
| 110 | + logrus.Infof("Reading sketch from dir: %s", wd) |
| 111 | + return wd |
| 112 | +} |
| 113 | + |
| 114 | +type programmersList struct { |
| 115 | + Programmers []*rpc.Programmer |
| 116 | +} |
| 117 | + |
| 118 | +func (p *programmersList) Data() interface{} { |
| 119 | + return p.Programmers |
| 120 | +} |
| 121 | + |
| 122 | +func (p *programmersList) String() string { |
| 123 | + t := table.New() |
| 124 | + t.SetHeader("ID", "Programmer Name", "Platform") |
| 125 | + for _, prog := range p.Programmers { |
| 126 | + t.AddRow(prog.GetId(), prog.GetName(), prog.GetPlatform()) |
| 127 | + } |
| 128 | + return t.Render() |
| 129 | +} |
0 commit comments