Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added upload --upload-field key=value flag to set upload fields value #2348

Merged
merged 3 commits into from
Oct 16, 2023

Conversation

cmaglie
Copy link
Member

@cmaglie cmaglie commented Sep 29, 2023

Please check if the PR fulfills these requirements

See how to contribute

  • The PR has no duplicates (please search among the Pull Requests
    before creating one)
  • The PR follows
    our contributing guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)
  • UPGRADING.md has been updated with a migration guide (for breaking changes)
  • configuration.schema.json updated if new parameters are added.

What kind of change does this PR introduce?

Adds a --upload-field flag to the upload command to set upload fields directly from the command line.

What is the current behavior?

The upload fields must be entered interactively via terminal.

$ arduino-cli upload -b arduino:samd:mkr1000 -p 192.168.0.126 -v
Uploading to specified board using network protocol requires the following info:
Password: 
"/home/megabug/.arduino15/packages/arduino/tools/arduinoOTA/1.2.1/bin/arduinoOTA" -address "192.168.0.126" -port 65280 -username arduino -password "xxx" -sketch "/tmp/arduino/sketches/023276D40808FCC18E5644A85923D55D/WiFi101_OTA.ino.bin" -upload /sketch -b 
Connecting to board ...  done
Uploading sketch ...  done
Flashing sketch ...  done
Error flashing the sketch:Unauthorized
Failed uploading: uploading error: exit status 1

What is the new behavior?

The upload fields may be passed via --upload-field field=value flag.

$ arduino-cli upload -b arduino:samd:mkr1000 -p 192.168.0.126 --upload-field pass=xxx -v
Missing required upload field: password
$ arduino-cli upload -b arduino:samd:mkr1000 -p 192.168.0.126 --upload-field password=xxx -v
"/home/megabug/.arduino15/packages/arduino/tools/arduinoOTA/1.2.1/bin/arduinoOTA" -address "192.168.0.126" -port 65280 -username arduino -password "xxx" -sketch "/tmp/arduino/sketches/023276D40808FCC18E5644A85923D55D/WiFi101_OTA.ino.bin" -upload /sketch -b 
Connecting to board ...  done
Uploading sketch ...  done
Flashing sketch ...  done
Error flashing the sketch:Unauthorized
Failed uploading: uploading error: exit status 1
$

Does this PR introduce a breaking change, and is titled accordingly?

No

Other information

Fix #2270

@cmaglie cmaglie added type: enhancement Proposed improvement topic: CLI Related to the command line interface labels Sep 29, 2023
@cmaglie cmaglie self-assigned this Sep 29, 2023
@codecov
Copy link

codecov bot commented Sep 29, 2023

Codecov Report

Attention: 45 lines in your changes are missing coverage. Please review.

Comparison is base (331541a) 62.91% compared to head (2863760) 62.82%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2348      +/-   ##
==========================================
- Coverage   62.91%   62.82%   -0.10%     
==========================================
  Files         203      204       +1     
  Lines       19249    19307      +58     
==========================================
+ Hits        12110    12129      +19     
- Misses       6080     6118      +38     
- Partials     1059     1060       +1     
Flag Coverage Δ
unit 62.82% <35.71%> (-0.10%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
internal/cli/upload/upload.go 56.73% <44.82%> (-2.14%) ⬇️
internal/cli/arguments/key_value.go 29.26% <29.26%> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@umbynos umbynos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, please integrate this new feature in the docs

@cmaglie
Copy link
Member Author

cmaglie commented Oct 13, 2023

I've added a KeyValue type by implementing the cobra.Value interface. This makes the help a little more self-explanatory:

Upload Arduino sketches. This does NOT compile the sketch prior to upload.

Usage:
  arduino-cli upload [flags]

Examples:
  arduino-cli upload /home/user/Arduino/MySketch -p /dev/ttyACM0 -b arduino:avr:uno
  arduino-cli upload -p 192.168.10.1 -b arduino:avr:uno --upload-field password=abc

Flags:
      --board-options strings        List of board options separated by commas. Or can be used multiple times for multiple options.
      --discovery-timeout duration   Max time to wait for port discovery, e.g.: 30s, 1m (default 1s)
  -b, --fqbn string                  Fully Qualified Board Name, e.g.: arduino:avr:uno
  -h, --help                         help for upload
      --input-dir string             Directory containing binaries to upload.
  -i, --input-file string            Binary file to upload.
  -p, --port string                  Upload port address, e.g.: COM3 or /dev/ttyACM2
  -m, --profile string               Sketch profile to use
  -P, --programmer string            Programmer to use, e.g: atmel_ice
  -l, --protocol string              Upload port protocol, e.g: serial
  -F, --upload-field key=value       Set a value for a field required to upload.
  -t, --verify                       Verify uploaded binary after the upload.

Global Flags:
      --additional-urls strings   Comma-separated list of additional URLs for the Boards Manager.
      --config-file string        The custom config file (if not specified the default will be used).
      --format string             The output format for the logs, can be: text, json, jsonmini, yaml (default "text")
      --log                       Print the logs on the standard output.
      --log-file string           Path to the file where logs will be written.
      --log-format string         The output format for the logs, can be: text, json
      --log-level string          Messages with this level and above will be logged. Valid levels are: trace, debug, info, warn, error, fatal, panic
      --no-color                  Disable colored output.

The other benefit is that the flag syntax check is performed much earlier in the parsing process:

$ arduino-cli upload -F password
Error: invalid argument "password" for "-F, --upload-field" flag: required format is 'key=value'
[....]
$ arduino-cli upload -F =value
Error: invalid argument "=value" for "-F, --upload-field" flag: key cannot be empty
[...]
$ arduino-cli upload -F password=value -F password=another
Error: invalid argument "password=another" for "-F, --upload-field" flag: duplicate key: password
[...]

and finally, we get directly a map[string]string without further processing on the CLI side.

@cmaglie cmaglie requested a review from umbynos October 13, 2023 12:29
@cmaglie cmaglie changed the title Added 'upload' flags to set upload fields value Added upload --upload-field key=value flag to set upload fields value Oct 16, 2023
@cmaglie cmaglie merged commit 98c0480 into arduino:master Oct 16, 2023
92 checks passed
@cmaglie cmaglie deleted the cli_upload_fields branch October 16, 2023 10:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: CLI Related to the command line interface type: enhancement Proposed improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support setting "user provided fields" non-interactively
2 participants