Skip to content

Commit

Permalink
Added 'upload' flags to set upload fields value
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Sep 29, 2023
1 parent c004b42 commit 00b2a3a
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions internal/cli/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ var (

// NewCommand created a new `upload` command
func NewCommand() *cobra.Command {
var uploadFields []string
var parsedUploadFields map[string]string
uploadCommand := &cobra.Command{
Use: "upload",
Short: tr("Upload Arduino sketches."),
Expand All @@ -59,8 +61,20 @@ func NewCommand() *cobra.Command {
Args: cobra.MaximumNArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
arguments.CheckFlagsConflicts(cmd, "input-file", "input-dir")
if len(uploadFields) > 0 {
parsedUploadFields = map[string]string{}
for _, field := range uploadFields {
split := strings.SplitN(field, "=", 2)
if len(split) != 2 {
feedback.Fatal(tr("Invalid upload field: %s", field), feedback.ErrBadArgument)
}
parsedUploadFields[split[0]] = split[1]
}
}
},
Run: func(cmd *cobra.Command, args []string) {
runUploadCommand(args, parsedUploadFields)
},
Run: runUploadCommand,
}

fqbnArg.AddToCommand(uploadCommand)
Expand All @@ -73,10 +87,11 @@ func NewCommand() *cobra.Command {
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")
uploadCommand.Flags().StringArrayVar(&uploadFields, "upload-field", uploadFields, tr("Set a value for a field required to upload.")+" (field=value)")
return uploadCommand
}

func runUploadCommand(command *cobra.Command, args []string) {
func runUploadCommand(args []string, uploadFieldsArgs map[string]string) {
logrus.Info("Executing `arduino-cli upload`")

path := ""
Expand Down Expand Up @@ -147,12 +162,24 @@ func runUploadCommand(command *cobra.Command, args []string) {

fields := map[string]string{}
if len(userFieldRes.UserFields) > 0 {
feedback.Print(tr("Uploading to specified board using %s protocol requires the following info:", port.Protocol))
if f, err := arguments.AskForUserFields(userFieldRes.UserFields); err != nil {
msg := fmt.Sprintf("%s: %s", tr("Error getting user input"), err)
feedback.Fatal(msg, feedback.ErrGeneric)
if len(uploadFieldsArgs) > 0 {
// If the user has specified some fields via cmd-line, we don't ask for them
for _, field := range userFieldRes.UserFields {
if value, ok := uploadFieldsArgs[field.Name]; ok {
fields[field.Name] = value
} else {
feedback.Fatal(tr("Missing required upload field: %s", field.Name), feedback.ErrBadArgument)
}
}
} else {
fields = f
// Otherwise prompt the user for them
feedback.Print(tr("Uploading to specified board using %s protocol requires the following info:", port.Protocol))
if f, err := arguments.AskForUserFields(userFieldRes.UserFields); err != nil {
msg := fmt.Sprintf("%s: %s", tr("Error getting user input"), err)
feedback.Fatal(msg, feedback.ErrGeneric)
} else {
fields = f
}
}
}

Expand Down

0 comments on commit 00b2a3a

Please sign in to comment.