Commit 9131edb8 authored by Umberto Baldi's avatar Umberto Baldi

refactor upload command to use code from the arguments package

parent ea0334cd
......@@ -30,13 +30,13 @@ import (
)
var (
fqbn string
fqbn arguments.Fqbn
port arguments.Port
verbose bool
verify bool
importDir string
importFile string
programmer string
programmer arguments.Programmer
dryRun bool
tr = i18n.Tr
)
......@@ -49,36 +49,25 @@ func NewCommand() *cobra.Command {
Long: tr("Upload Arduino sketches. This does NOT compile the sketch prior to upload."),
Example: " " + os.Args[0] + " upload /home/user/Arduino/MySketch",
Args: cobra.MaximumNArgs(1),
PreRun: checkFlagsConflicts,
Run: run,
PreRun: func(cmd *cobra.Command, args []string) {
arguments.CheckFlagsConflicts(cmd, "input-file", "input-dir")
},
Run: runUploadCommand,
}
uploadCommand.Flags().StringVarP(&fqbn, "fqbn", "b", "", tr("Fully Qualified Board Name, e.g.: arduino:avr:uno"))
uploadCommand.RegisterFlagCompletionFunc("fqbn", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return arguments.GetInstalledBoards(), cobra.ShellCompDirectiveDefault
})
fqbn.AddToCommand(uploadCommand)
port.AddToCommand(uploadCommand)
uploadCommand.Flags().StringVarP(&importDir, "input-dir", "", "", tr("Directory containing binaries to upload."))
uploadCommand.Flags().StringVarP(&importFile, "input-file", "i", "", tr("Binary file to upload."))
uploadCommand.Flags().BoolVarP(&verify, "verify", "t", false, tr("Verify uploaded binary after the upload."))
uploadCommand.Flags().BoolVarP(&verbose, "verbose", "v", false, tr("Optional, turns on verbose mode."))
uploadCommand.Flags().StringVarP(&programmer, "programmer", "P", "", tr("Optional, use the specified programmer to upload."))
uploadCommand.RegisterFlagCompletionFunc("programmer", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return arguments.GetInstalledProgrammers(), cobra.ShellCompDirectiveDefault
})
programmer.AddToCommand(uploadCommand)
uploadCommand.Flags().BoolVar(&dryRun, "dry-run", false, tr("Do not perform the actual upload, just log out actions"))
uploadCommand.Flags().MarkHidden("dry-run")
return uploadCommand
}
func checkFlagsConflicts(command *cobra.Command, args []string) {
if importFile != "" && importDir != "" {
feedback.Errorf(tr("error: %s and %s flags cannot be used together", "--input-file", "--input-dir"))
os.Exit(errorcodes.ErrBadArgument)
}
}
func run(command *cobra.Command, args []string) {
func runUploadCommand(command *cobra.Command, args []string) {
instance := instance.CreateAndInit()
path := ""
......@@ -90,15 +79,15 @@ func run(command *cobra.Command, args []string) {
sk := arguments.NewSketch(sketchPath)
discoveryPort := port.GetDiscoveryPort(instance, sk)
if fqbn == "" && sk != nil && sk.Metadata != nil {
if fqbn.String() == "" && sk != nil && sk.Metadata != nil {
// If the user didn't specify an FQBN and a sketch.json file is present
// read it from there.
fqbn = sk.Metadata.CPU.Fqbn
fqbn.Set(sk.Metadata.CPU.Fqbn)
}
userFieldRes, err := upload.SupportedUserFields(context.Background(), &rpc.SupportedUserFieldsRequest{
Instance: instance,
Fqbn: fqbn,
Fqbn: fqbn.String(),
Protocol: discoveryPort.Protocol,
})
if err != nil {
......@@ -118,14 +107,14 @@ func run(command *cobra.Command, args []string) {
if _, err := upload.Upload(context.Background(), &rpc.UploadRequest{
Instance: instance,
Fqbn: fqbn,
Fqbn: fqbn.String(),
SketchPath: path,
Port: discoveryPort.ToRPC(),
Verbose: verbose,
Verify: verify,
ImportFile: importFile,
ImportDir: importDir,
Programmer: programmer,
Programmer: programmer.String(),
DryRun: dryRun,
UserFields: fields,
}, os.Stdout, os.Stderr); err != nil {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment