Skip to content

Commit 9131edb

Browse files
committed
refactor upload command to use code from the arguments package
1 parent ea0334c commit 9131edb

File tree

1 file changed

+14
-25
lines changed

1 file changed

+14
-25
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 := ""
@@ -90,15 +79,15 @@ func run(command *cobra.Command, args []string) {
9079
sk := arguments.NewSketch(sketchPath)
9180
discoveryPort := port.GetDiscoveryPort(instance, sk)
9281

93-
if fqbn == "" && sk != nil && sk.Metadata != nil {
82+
if fqbn.String() == "" && sk != nil && sk.Metadata != nil {
9483
// If the user didn't specify an FQBN and a sketch.json file is present
9584
// read it from there.
96-
fqbn = sk.Metadata.CPU.Fqbn
85+
fqbn.Set(sk.Metadata.CPU.Fqbn)
9786
}
9887

9988
userFieldRes, err := upload.SupportedUserFields(context.Background(), &rpc.SupportedUserFieldsRequest{
10089
Instance: instance,
101-
Fqbn: fqbn,
90+
Fqbn: fqbn.String(),
10291
Protocol: discoveryPort.Protocol,
10392
})
10493
if err != nil {
@@ -118,14 +107,14 @@ func run(command *cobra.Command, args []string) {
118107

119108
if _, err := upload.Upload(context.Background(), &rpc.UploadRequest{
120109
Instance: instance,
121-
Fqbn: fqbn,
110+
Fqbn: fqbn.String(),
122111
SketchPath: path,
123112
Port: discoveryPort.ToRPC(),
124113
Verbose: verbose,
125114
Verify: verify,
126115
ImportFile: importFile,
127116
ImportDir: importDir,
128-
Programmer: programmer,
117+
Programmer: programmer.String(),
129118
DryRun: dryRun,
130119
UserFields: fields,
131120
}, os.Stdout, os.Stderr); err != nil {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy