Skip to content

Commit f1877ef

Browse files
Roberto Soracmaglie
andauthored
Reintroduce the --input-file flag for the upload command (#777)
* Remove deprecation from importFile param * Implement --input-file flag * Add comment in --input-file splitting step * Add e2e test for --input-x flags * Refine upload flags testing * Add --input-file file existence check * Use TrimSuffix instead of replace * Restore -i shorthand flag for --input-file and add CLI checkFlagsConflicts function * Improved build path and project name auto-detection This should make the upload command compatibile with all the reasonable usages. See #764 See #641 * Made UploadTest more resilient * upload: sketch is ignored if input-dir or input-file is specified There is no point in overriding the sketch name if the user explicitly give it via command line. * Update go-paths-helper to version 1.3.2 fixes EquivalentTo when used with abs paths * fix TestGetCommandLine * 100% coverage on detectSketchNameFromBuildPath function * Do not git-ignore all *.bin but just inside the client_example folder * slightly simplified function signature (cosmetic) Co-authored-by: Cristian Maglie <[email protected]>
1 parent 5045656 commit f1877ef

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+352
-97
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ venv
1515

1616
# gRPC client example folder
1717
/client_example/client_example
18-
*.bin
19-
*.elf
18+
/client_example/**/*.bin
19+
/client_example/**/*.elf
2020

2121
# Misc.
2222
.DS_Store

arduino/sketches/sketches.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121

2222
"github.com/arduino/go-paths-helper"
23+
"github.com/pkg/errors"
2324
)
2425

2526
// Sketch is a sketch for Arduino
@@ -43,9 +44,17 @@ type BoardMetadata struct {
4344

4445
// NewSketchFromPath loads a sketch from the specified path
4546
func NewSketchFromPath(path *paths.Path) (*Sketch, error) {
47+
path, err := path.Abs()
48+
if err != nil {
49+
return nil, errors.Errorf("getting sketch path: %s", err)
50+
}
4651
if !path.IsDir() {
4752
path = path.Parent()
4853
}
54+
sketchFile := path.Join(path.Base() + ".ino")
55+
if !sketchFile.Exist() {
56+
return nil, errors.Errorf("no valid sketch found in %s: missing %s", path, sketchFile.Base())
57+
}
4958
sketch := &Sketch{
5059
FullPath: path,
5160
Name: path.Base(),

cli/upload/upload.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ var (
3636
verbose bool
3737
verify bool
3838
importDir string
39+
importFile string
3940
programmer string
4041
)
4142

@@ -47,19 +48,28 @@ func NewCommand() *cobra.Command {
4748
Long: "Upload Arduino sketches. This does NOT compile the sketch prior to upload.",
4849
Example: " " + os.Args[0] + " upload /home/user/Arduino/MySketch",
4950
Args: cobra.MaximumNArgs(1),
51+
PreRun: checkFlagsConflicts,
5052
Run: run,
5153
}
5254

5355
uploadCommand.Flags().StringVarP(&fqbn, "fqbn", "b", "", "Fully Qualified Board Name, e.g.: arduino:avr:uno")
5456
uploadCommand.Flags().StringVarP(&port, "port", "p", "", "Upload port, e.g.: COM10 or /dev/ttyACM0")
5557
uploadCommand.Flags().StringVarP(&importDir, "input-dir", "", "", "Directory containing binaries to upload.")
58+
uploadCommand.Flags().StringVarP(&importFile, "input-file", "i", "", "Binary file to upload.")
5659
uploadCommand.Flags().BoolVarP(&verify, "verify", "t", false, "Verify uploaded binary after the upload.")
5760
uploadCommand.Flags().BoolVarP(&verbose, "verbose", "v", false, "Optional, turns on verbose mode.")
5861
uploadCommand.Flags().StringVarP(&programmer, "programmer", "P", "", "Optional, use the specified programmer to upload or 'list' to list supported programmers.")
5962

6063
return uploadCommand
6164
}
6265

66+
func checkFlagsConflicts(command *cobra.Command, args []string) {
67+
if importFile != "" && importDir != "" {
68+
feedback.Errorf("error: --input-file and --input-dir flags cannot be used together")
69+
os.Exit(errorcodes.ErrBadArgument)
70+
}
71+
}
72+
6373
func run(command *cobra.Command, args []string) {
6474
instance, err := instance.CreateInstance()
6575
if err != nil {
@@ -80,6 +90,7 @@ func run(command *cobra.Command, args []string) {
8090
Port: port,
8191
Verbose: verbose,
8292
Verify: verify,
93+
ImportFile: importFile,
8394
ImportDir: importDir,
8495
Programmer: programmer,
8596
}, os.Stdout, os.Stderr); err != nil {

commands/debug/debug_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@ import (
2626
dbg "github.com/arduino/arduino-cli/rpc/debug"
2727
"github.com/arduino/go-paths-helper"
2828
"github.com/stretchr/testify/assert"
29+
"github.com/stretchr/testify/require"
2930
)
3031

31-
var customHardware = paths.New("testdata", "custom_hardware")
32-
var dataDir = paths.New("testdata", "data_dir", "packages")
33-
var sketch = "hello"
34-
var sketchPath = paths.New("testdata", sketch)
35-
3632
func TestGetCommandLine(t *testing.T) {
33+
customHardware := paths.New("testdata", "custom_hardware")
34+
dataDir := paths.New("testdata", "data_dir", "packages")
35+
sketch := "hello"
36+
sketchPath := paths.New("testdata", sketch)
37+
require.NoError(t, sketchPath.ToAbs())
38+
3739
pm := packagemanager.NewPackageManager(nil, nil, nil, nil)
3840
pm.LoadHardwareFromDirectory(customHardware)
3941
pm.LoadHardwareFromDirectory(dataDir)
@@ -59,9 +61,9 @@ func TestGetCommandLine(t *testing.T) {
5961
fmt.Sprintf(" -c \"gdb_port pipe\" -c \"telnet_port 0\" -c init -c halt %s/build/arduino-test.samd.arduino_zero_edbg/hello.ino.elf", sketchPath)
6062

6163
command, err := getCommandLine(req, pm)
62-
assert.Nil(t, err)
64+
require.Nil(t, err)
6365
commandToTest := strings.Join(command[:], " ")
64-
assert.Equal(t, filepath.FromSlash(goldCommand), filepath.FromSlash(commandToTest))
66+
require.Equal(t, filepath.FromSlash(goldCommand), filepath.FromSlash(commandToTest))
6567

6668
// Other samd boards such as mkr1000 can be debugged using an external tool such as Atmel ICE connected to
6769
// the board debug port
@@ -83,5 +85,4 @@ func TestGetCommandLine(t *testing.T) {
8385
assert.Nil(t, err)
8486
commandToTest2 := strings.Join(command2[:], " ")
8587
assert.Equal(t, filepath.FromSlash(goldCommand2), filepath.FromSlash(commandToTest2))
86-
8788
}

commands/debug/testdata/hello/hello.ino

Whitespace-only changes.

commands/upload/burnbootloader.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func BurnBootloader(ctx context.Context, req *rpc.BurnBootloaderReq, outStream i
3737
err := runProgramAction(
3838
pm,
3939
nil, // sketch
40+
"", // importFile
4041
"", // importDir
4142
req.GetFqbn(),
4243
req.GetPort(),

commands/upload/testdata/Blonk/Blonk.ino

Whitespace-only changes.

commands/upload/testdata/Blonk/build/arduino.samd.mkr1000/Blonk.ino.bin

Whitespace-only changes.

commands/upload/testdata/Blonk/build/arduino.samd.mkr1000/Blonk.ino.elf

Whitespace-only changes.

commands/upload/testdata/Blonk/build/arduino.samd.mkr1000/Blonk.ino.hex

Whitespace-only changes.

0 commit comments

Comments
 (0)