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 ( ...@@ -30,13 +30,13 @@ import (
) )
var ( var (
fqbn string fqbn arguments.Fqbn
port arguments.Port port arguments.Port
verbose bool verbose bool
verify bool verify bool
importDir string importDir string
importFile string importFile string
programmer string programmer arguments.Programmer
dryRun bool dryRun bool
tr = i18n.Tr tr = i18n.Tr
) )
...@@ -49,36 +49,25 @@ func NewCommand() *cobra.Command { ...@@ -49,36 +49,25 @@ func NewCommand() *cobra.Command {
Long: tr("Upload Arduino sketches. This does NOT compile the sketch prior to upload."), Long: tr("Upload Arduino sketches. This does NOT compile the sketch prior to upload."),
Example: " " + os.Args[0] + " upload /home/user/Arduino/MySketch", Example: " " + os.Args[0] + " upload /home/user/Arduino/MySketch",
Args: cobra.MaximumNArgs(1), Args: cobra.MaximumNArgs(1),
PreRun: checkFlagsConflicts, PreRun: func(cmd *cobra.Command, args []string) {
Run: run, 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")) fqbn.AddToCommand(uploadCommand)
uploadCommand.RegisterFlagCompletionFunc("fqbn", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return arguments.GetInstalledBoards(), cobra.ShellCompDirectiveDefault
})
port.AddToCommand(uploadCommand) port.AddToCommand(uploadCommand)
uploadCommand.Flags().StringVarP(&importDir, "input-dir", "", "", tr("Directory containing binaries to upload.")) 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().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(&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().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.")) programmer.AddToCommand(uploadCommand)
uploadCommand.RegisterFlagCompletionFunc("programmer", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return arguments.GetInstalledProgrammers(), cobra.ShellCompDirectiveDefault
})
uploadCommand.Flags().BoolVar(&dryRun, "dry-run", false, tr("Do not perform the actual upload, just log out actions")) uploadCommand.Flags().BoolVar(&dryRun, "dry-run", false, tr("Do not perform the actual upload, just log out actions"))
uploadCommand.Flags().MarkHidden("dry-run") uploadCommand.Flags().MarkHidden("dry-run")
return uploadCommand return uploadCommand
} }
func checkFlagsConflicts(command *cobra.Command, args []string) { func runUploadCommand(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) {
instance := instance.CreateAndInit() instance := instance.CreateAndInit()
path := "" path := ""
...@@ -90,15 +79,15 @@ func run(command *cobra.Command, args []string) { ...@@ -90,15 +79,15 @@ func run(command *cobra.Command, args []string) {
sk := arguments.NewSketch(sketchPath) sk := arguments.NewSketch(sketchPath)
discoveryPort := port.GetDiscoveryPort(instance, sk) 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 // If the user didn't specify an FQBN and a sketch.json file is present
// read it from there. // read it from there.
fqbn = sk.Metadata.CPU.Fqbn fqbn.Set(sk.Metadata.CPU.Fqbn)
} }
userFieldRes, err := upload.SupportedUserFields(context.Background(), &rpc.SupportedUserFieldsRequest{ userFieldRes, err := upload.SupportedUserFields(context.Background(), &rpc.SupportedUserFieldsRequest{
Instance: instance, Instance: instance,
Fqbn: fqbn, Fqbn: fqbn.String(),
Protocol: discoveryPort.Protocol, Protocol: discoveryPort.Protocol,
}) })
if err != nil { if err != nil {
...@@ -118,14 +107,14 @@ func run(command *cobra.Command, args []string) { ...@@ -118,14 +107,14 @@ func run(command *cobra.Command, args []string) {
if _, err := upload.Upload(context.Background(), &rpc.UploadRequest{ if _, err := upload.Upload(context.Background(), &rpc.UploadRequest{
Instance: instance, Instance: instance,
Fqbn: fqbn, Fqbn: fqbn.String(),
SketchPath: path, SketchPath: path,
Port: discoveryPort.ToRPC(), Port: discoveryPort.ToRPC(),
Verbose: verbose, Verbose: verbose,
Verify: verify, Verify: verify,
ImportFile: importFile, ImportFile: importFile,
ImportDir: importDir, ImportDir: importDir,
Programmer: programmer, Programmer: programmer.String(),
DryRun: dryRun, DryRun: dryRun,
UserFields: fields, UserFields: fields,
}, os.Stdout, os.Stderr); err != nil { }, 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