Skip to content

Commit c5f7fbf

Browse files
committed
refactor upload command to use code from the arguments package
1 parent 11c80e2 commit c5f7fbf

File tree

2 files changed

+22
-42
lines changed

2 files changed

+22
-42
lines changed

cli/upload/upload.go

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ import (
3030
)
3131

3232
var (
33-
fqbn string
33+
fqbn arguments.Fqbn
3434
port arguments.Port
3535
verbose bool
3636
verify bool
3737
importDir string
3838
importFile string
39-
programmer string
39+
programmer arguments.Programmer
4040
dryRun bool
4141
tr = i18n.Tr
4242
)
@@ -49,36 +49,25 @@ func NewCommand() *cobra.Command {
4949
Long: tr("Upload Arduino sketches. This does NOT compile the sketch prior to upload."),
5050
Example: " " + os.Args[0] + " upload /home/user/Arduino/MySketch",
5151
Args: cobra.MaximumNArgs(1),
52-
PreRun: checkFlagsConflicts,
53-
Run: run,
52+
PreRun: func(cmd *cobra.Command, args []string) {
53+
arguments.CheckFlagsConflicts(cmd, "input-file", "input-dir")
54+
},
55+
Run: runUploadCommand,
5456
}
5557

56-
uploadCommand.Flags().StringVarP(&fqbn, "fqbn", "b", "", tr("Fully Qualified Board Name, e.g.: arduino:avr:uno"))
57-
uploadCommand.RegisterFlagCompletionFunc("fqbn", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
58-
return arguments.GetInstalledBoards(), cobra.ShellCompDirectiveDefault
59-
})
58+
fqbn.AddToCommand(uploadCommand)
6059
port.AddToCommand(uploadCommand)
6160
uploadCommand.Flags().StringVarP(&importDir, "input-dir", "", "", tr("Directory containing binaries to upload."))
6261
uploadCommand.Flags().StringVarP(&importFile, "input-file", "i", "", tr("Binary file to upload."))
6362
uploadCommand.Flags().BoolVarP(&verify, "verify", "t", false, tr("Verify uploaded binary after the upload."))
6463
uploadCommand.Flags().BoolVarP(&verbose, "verbose", "v", false, tr("Optional, turns on verbose mode."))
65-
uploadCommand.Flags().StringVarP(&programmer, "programmer", "P", "", tr("Optional, use the specified programmer to upload."))
66-
uploadCommand.RegisterFlagCompletionFunc("programmer", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
67-
return arguments.GetInstalledProgrammers(), cobra.ShellCompDirectiveDefault
68-
})
64+
programmer.AddToCommand(uploadCommand)
6965
uploadCommand.Flags().BoolVar(&dryRun, "dry-run", false, tr("Do not perform the actual upload, just log out actions"))
7066
uploadCommand.Flags().MarkHidden("dry-run")
7167
return uploadCommand
7268
}
7369

74-
func checkFlagsConflicts(command *cobra.Command, args []string) {
75-
if importFile != "" && importDir != "" {
76-
feedback.Errorf(tr("error: %s and %s flags cannot be used together", "--input-file", "--input-dir"))
77-
os.Exit(errorcodes.ErrBadArgument)
78-
}
79-
}
80-
81-
func run(command *cobra.Command, args []string) {
70+
func runUploadCommand(command *cobra.Command, args []string) {
8271
instance := instance.CreateAndInit()
8372

8473
path := ""
@@ -87,15 +76,15 @@ func run(command *cobra.Command, args []string) {
8776
}
8877
sketchPath, sk, discoveryPort := arguments.SketchPathCalculation(path, port, instance, true)
8978

90-
if fqbn == "" && sk != nil && sk.Metadata != nil {
79+
if fqbn.GetFQBN() == "" && sk != nil && sk.Metadata != nil {
9180
// If the user didn't specify an FQBN and a sketch.json file is present
9281
// read it from there.
93-
fqbn = sk.Metadata.CPU.Fqbn
82+
fqbn.SetFQBN(sk.Metadata.CPU.Fqbn)
9483
}
9584

9685
userFieldRes, err := upload.SupportedUserFields(context.Background(), &rpc.SupportedUserFieldsRequest{
9786
Instance: instance,
98-
Fqbn: fqbn,
87+
Fqbn: fqbn.GetFQBN(),
9988
Protocol: discoveryPort.Protocol,
10089
})
10190
if err != nil {
@@ -115,14 +104,14 @@ func run(command *cobra.Command, args []string) {
115104

116105
if _, err := upload.Upload(context.Background(), &rpc.UploadRequest{
117106
Instance: instance,
118-
Fqbn: fqbn,
107+
Fqbn: fqbn.GetFQBN(),
119108
SketchPath: path,
120109
Port: discoveryPort.ToRPC(),
121110
Verbose: verbose,
122111
Verify: verify,
123112
ImportFile: importFile,
124113
ImportDir: importDir,
125-
Programmer: programmer,
114+
Programmer: programmer.GetProgrammer(),
126115
DryRun: dryRun,
127116
UserFields: fields,
128117
}, os.Stdout, os.Stderr); err != nil {

i18n/data/en.po

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ msgstr "Available"
212212
msgid "Available Commands:"
213213
msgstr "Available Commands:"
214214

215-
#: cli/upload/upload.go:62
215+
#: cli/upload/upload.go:61
216216
msgid "Binary file to upload."
217217
msgstr "Binary file to upload."
218218

@@ -545,7 +545,7 @@ msgstr "Detects and displays a list of boards connected to the current computer.
545545
msgid "Directory containing binaries for debug."
546546
msgstr "Directory containing binaries for debug."
547547

548-
#: cli/upload/upload.go:61
548+
#: cli/upload/upload.go:60
549549
msgid "Directory containing binaries to upload."
550550
msgstr "Directory containing binaries to upload."
551551

@@ -570,7 +570,7 @@ msgid "Do not install dependencies."
570570
msgstr "Do not install dependencies."
571571

572572
#: cli/burnbootloader/burnbootloader.go:58
573-
#: cli/upload/upload.go:69
573+
#: cli/upload/upload.go:65
574574
msgid "Do not perform the actual upload, just log out actions"
575575
msgstr "Do not perform the actual upload, just log out actions"
576576

@@ -727,8 +727,8 @@ msgstr "Error during JSON encoding of the output: %v"
727727
#: cli/burnbootloader/burnbootloader.go:83
728728
#: cli/compile/compile.go:192
729729
#: cli/compile/compile.go:224
730-
#: cli/upload/upload.go:102
731-
#: cli/upload/upload.go:129
730+
#: cli/upload/upload.go:91
731+
#: cli/upload/upload.go:118
732732
msgid "Error during Upload: %v"
733733
msgstr "Error during Upload: %v"
734734

@@ -1089,7 +1089,6 @@ msgstr "Force skip of post-install scripts (if the CLI is running interactively)
10891089
#: cli/arguments/port.go:80
10901090
#: cli/debug/debug.go:60
10911091
#: cli/monitor/monitor.go:63
1092-
#: cli/upload/upload.go:56
10931092
msgid "Fully Qualified Board Name, e.g.: arduino:avr:uno"
10941093
msgstr "Fully Qualified Board Name, e.g.: arduino:avr:uno"
10951094

@@ -1608,14 +1607,10 @@ msgid "Optional, suppresses almost every output."
16081607
msgstr "Optional, suppresses almost every output."
16091608

16101609
#: cli/compile/compile.go:95
1611-
#: cli/upload/upload.go:64
1610+
#: cli/upload/upload.go:63
16121611
msgid "Optional, turns on verbose mode."
16131612
msgstr "Optional, turns on verbose mode."
16141613

1615-
#: cli/upload/upload.go:65
1616-
msgid "Optional, use the specified programmer to upload."
1617-
msgstr "Optional, use the specified programmer to upload."
1618-
16191614
#: cli/compile/compile.go:113
16201615
msgid "Optional. Path to a .json file that contains a set of replacements of the sketch source code."
16211616
msgstr "Optional. Path to a .json file that contains a set of replacements of the sketch source code."
@@ -2272,7 +2267,7 @@ msgid "Upload the bootloader."
22722267
msgstr "Upload the bootloader."
22732268

22742269
#: cli/compile/compile.go:198
2275-
#: cli/upload/upload.go:108
2270+
#: cli/upload/upload.go:97
22762271
msgid "Uploading to specified board using %s protocol requires the following info:"
22772272
msgstr "Uploading to specified board using %s protocol requires the following info:"
22782273

@@ -2349,7 +2344,7 @@ msgstr "Values"
23492344

23502345
#: cli/burnbootloader/burnbootloader.go:56
23512346
#: cli/compile/compile.go:99
2352-
#: cli/upload/upload.go:63
2347+
#: cli/upload/upload.go:62
23532348
msgid "Verify uploaded binary after the upload."
23542349
msgstr "Verify uploaded binary after the upload."
23552350

@@ -2638,10 +2633,6 @@ msgstr "error processing response from server"
26382633
msgid "error querying Arduino Cloud Api"
26392634
msgstr "error querying Arduino Cloud Api"
26402635

2641-
#: cli/upload/upload.go:76
2642-
msgid "error: %s and %s flags cannot be used together"
2643-
msgstr "error: %s and %s flags cannot be used together"
2644-
26452636
#: arduino/resources/install.go:67
26462637
msgid "extracting archive: %s"
26472638
msgstr "extracting archive: %s"

0 commit comments

Comments
 (0)