Commit 59436ca6 authored by Cristian Maglie's avatar Cristian Maglie Committed by Cristian Maglie

Added --input flag to 'upload' command.

Also simplified configuration creation for upload command.
parent 9170d1fa
...@@ -45,22 +45,30 @@ func InitCommand() *cobra.Command { ...@@ -45,22 +45,30 @@ func InitCommand() *cobra.Command {
Args: cobra.MaximumNArgs(1), Args: cobra.MaximumNArgs(1),
Run: run, Run: run,
} }
uploadCommand.Flags().StringVarP(&flags.fqbn, "fqbn", "b", "", uploadCommand.Flags().StringVarP(
&flags.fqbn, "fqbn", "b", "",
"Fully Qualified Board Name, e.g.: arduino:avr:uno") "Fully Qualified Board Name, e.g.: arduino:avr:uno")
uploadCommand.Flags().StringVarP(&flags.port, "port", "p", "", uploadCommand.Flags().StringVarP(
&flags.port, "port", "p", "",
"Upload port, e.g.: COM10 or /dev/ttyACM0") "Upload port, e.g.: COM10 or /dev/ttyACM0")
uploadCommand.Flags().BoolVarP(&flags.verify, "verify", "t", false, uploadCommand.Flags().StringVarP(
&flags.importFile, "input", "i", "",
"Input file to be uploaded.")
uploadCommand.Flags().BoolVarP(
&flags.verify, "verify", "t", false,
"Verify uploaded binary after the upload.") "Verify uploaded binary after the upload.")
uploadCommand.Flags().BoolVarP(&flags.verbose, "verbose", "v", false, uploadCommand.Flags().BoolVarP(
&flags.verbose, "verbose", "v", false,
"Optional, turns on verbose mode.") "Optional, turns on verbose mode.")
return uploadCommand return uploadCommand
} }
var flags struct { var flags struct {
fqbn string fqbn string
port string port string
verbose bool verbose bool
verify bool verify bool
importFile string
} }
func run(command *cobra.Command, args []string) { func run(command *cobra.Command, args []string) {
...@@ -81,42 +89,28 @@ func run(command *cobra.Command, args []string) { ...@@ -81,42 +89,28 @@ func run(command *cobra.Command, args []string) {
os.Exit(commands.ErrBadCall) os.Exit(commands.ErrBadCall)
} }
fqbn := flags.fqbn if flags.fqbn == "" && sketch != nil {
if fqbn == "" && sketch != nil { flags.fqbn = sketch.Metadata.CPU.Fqbn
fqbn = sketch.Metadata.CPU.Fqbn
} }
if fqbn == "" { if flags.fqbn == "" {
formatter.PrintErrorMessage("No Fully Qualified Board Name provided.") formatter.PrintErrorMessage("No Fully Qualified Board Name provided.")
os.Exit(commands.ErrBadCall) os.Exit(commands.ErrBadCall)
} }
fqbnParts := strings.Split(fqbn, ":") fqbn, err := cores.ParseFQBN(flags.fqbn)
if len(fqbnParts) < 3 || len(fqbnParts) > 4 { if err != nil {
formatter.PrintErrorMessage("Fully Qualified Board Name has incorrect format.") formatter.PrintError(err, "Invalid FQBN.")
os.Exit(commands.ErrBadCall) os.Exit(commands.ErrBadCall)
} }
pm := commands.InitPackageManager() pm := commands.InitPackageManager()
// Find target board // Find target board and board properties
board, err := pm.FindBoardWithFQBN(fqbn) _, _, board, boardProperties, _, err := pm.ResolveFQBN(fqbn)
if err != nil { if err != nil {
formatter.PrintError(err, "Invalid FQBN.") formatter.PrintError(err, "Invalid FQBN.")
os.Exit(commands.ErrBadCall) os.Exit(commands.ErrBadCall)
} }
// Create board configuration
var boardProperties *properties.Map
if len(fqbnParts) == 3 {
boardProperties = board.Properties
} else {
if props, err := board.GeneratePropertiesForConfiguration(fqbnParts[3]); err != nil {
formatter.PrintError(err, "Invalid FQBN.")
os.Exit(commands.ErrBadCall)
} else {
boardProperties = props
}
}
// Load programmer tool // Load programmer tool
uploadToolID, have := boardProperties.GetOk("upload.tool") uploadToolID, have := boardProperties.GetOk("upload.tool")
if !have || uploadToolID == "" { if !have || uploadToolID == "" {
...@@ -194,12 +188,27 @@ func run(command *cobra.Command, args []string) { ...@@ -194,12 +188,27 @@ func run(command *cobra.Command, args []string) {
} }
// Set path to compiled binary // Set path to compiled binary
// FIXME: refactor this should be made into a function // Make the filename without the FQBN configs part
fqbn = strings.Replace(fqbn, ":", ".", -1) fqbn.Configs = properties.NewMap()
uploadProperties.Set("build.path", sketch.FullPath) fqbnSuffix := strings.Replace(fqbn.String(), ":", ".", -1)
uploadProperties.Set("build.project_name", sketch.Name+"."+fqbn)
ext := filepath.Ext(uploadProperties.ExpandPropsInString("{recipe.output.tmp_file}")) ext := filepath.Ext(uploadProperties.ExpandPropsInString("{recipe.output.tmp_file}"))
if _, err := os.Stat(filepath.Join(sketch.FullPath, sketch.Name+"."+fqbn+ext)); err != nil {
var importPath *paths.Path
var importFile string
if flags.importFile == "" {
importPath = paths.New(sketch.FullPath)
importFile = sketch.Name + "." + fqbnSuffix
} else {
importPath = paths.New(flags.importFile).Parent()
importFile = paths.New(flags.importFile).Base()
if strings.HasSuffix(importFile, ext) {
importFile = importFile[:len(importFile)-len(ext)]
}
}
uploadProperties.SetPath("build.path", importPath)
uploadProperties.Set("build.project_name", importFile)
if _, err := os.Stat(filepath.Join(sketch.FullPath, importFile+ext)); err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
formatter.PrintErrorMessage("Compiled sketch not found. Please compile first.") formatter.PrintErrorMessage("Compiled sketch not found. Please compile first.")
} else { } else {
......
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