Skip to content

[breaking] uniform cli commands and flag #1542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
1a94fdf
uniform the usage of vars in board sub-commands
umbynos Nov 19, 2021
e394b76
remove double `CreateAndInit()`
umbynos Nov 19, 2021
e1d2c0b
factor out the fqbn flag handling
umbynos Nov 19, 2021
394fab7
uniform the usage of vars in burn-bootloader command & refactor fqbn
umbynos Nov 18, 2021
dd2c548
factor out the programmer flag handling
umbynos Nov 18, 2021
9771b99
refactor fqbn and programmer
umbynos Nov 18, 2021
be5022e
refactor sketch path calculation
umbynos Nov 19, 2021
d1a41f8
refactor config validation to remove some code duplication
umbynos Nov 19, 2021
47821ae
refactor function that checks for flags conflicts
umbynos Nov 19, 2021
ea0334c
refactor and reorganize core commands:
umbynos Nov 17, 2021
9131edb
refactor upload command to use code from the arguments package
umbynos Nov 18, 2021
8394280
move vars on top (like other files)
umbynos Nov 19, 2021
06e96b9
refactor debug command and use code from arguments, remove useless vars
umbynos Nov 18, 2021
4428a46
factor out init step for instance creation in `update`commands
umbynos Nov 19, 2021
0f25964
refactor lib command and use code from arguments, remove structs
umbynos Nov 19, 2021
aeb1528
refactor monitor command to be consistent with other commands
umbynos Nov 18, 2021
8ff498b
general improvements for consistency between commands
umbynos Nov 9, 2021
e642f72
remove useless break statements
umbynos Nov 17, 2021
c316b38
added info logging in every command for consistency
umbynos Nov 9, 2021
633c177
[breaking] remove `board details <fqbn>` in favour of `board details …
umbynos Nov 19, 2021
f85513c
[breaking] replace `board attach <port|fqbn> <sketch-path>` with `boa…
umbynos Nov 18, 2021
56add2e
[breaking] change `--timeout` flag to `--discovery-timeout` for consi…
umbynos Nov 10, 2021
294c7bc
update UPGRADING.md with breaking changes
umbynos Nov 18, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion cli/arguments/arguments.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@

package arguments

import "github.com/arduino/arduino-cli/i18n"
import (
"os"
"strings"

"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cli/i18n"
"github.com/spf13/cobra"
)

var tr = i18n.Tr

// CheckFlagsConflicts is a helper function useful to report errors when more than one conflicting flag is used
func CheckFlagsConflicts(command *cobra.Command, flagNames ...string) {
for _, flagName := range flagNames {
if !command.Flag(flagName).Changed {
return
}
}
feedback.Errorf(tr("Can't use %s flags at the same time.", "--"+strings.Join(flagNames, " "+tr("and")+" --")))
os.Exit(errorcodes.ErrBadArgument)
}
43 changes: 43 additions & 0 deletions cli/arguments/fqbn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// This file is part of arduino-cli.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc.

package arguments

import "github.com/spf13/cobra"

// Fqbn contains the fqbn flag data.
// This is useful so all flags used by commands that need
// this information are consistent with each other.
type Fqbn struct {
fqbn string
}

// AddToCommand adds the flags used to set fqbn to the specified Command
func (f *Fqbn) AddToCommand(cmd *cobra.Command) {
cmd.Flags().StringVarP(&f.fqbn, "fqbn", "b", "", tr("Fully Qualified Board Name, e.g.: arduino:avr:uno"))
cmd.RegisterFlagCompletionFunc("fqbn", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return GetInstalledBoards(), cobra.ShellCompDirectiveDefault
})
}

// String returns the fqbn
func (f *Fqbn) String() string {
return f.fqbn
}

// Set sets the fqbn
func (f *Fqbn) Set(fqbn string) {
f.fqbn = fqbn
}
17 changes: 17 additions & 0 deletions cli/arguments/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ package arguments
import (
"fmt"
"net/url"
"os"
"time"

"github.com/arduino/arduino-cli/arduino/discovery"
"github.com/arduino/arduino-cli/arduino/sketch"
"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cli/commands"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
Expand Down Expand Up @@ -144,3 +146,18 @@ func (p *Port) GetPort(instance *rpc.Instance, sk *sketch.Sketch) (*discovery.Po
}
}
}

// GetSearchTimeout returns the timeout
func (p *Port) GetSearchTimeout() time.Duration {
return p.timeout
}

// GetDiscoveryPort is a helper function useful to get the port and handle possible errors
func (p *Port) GetDiscoveryPort(instance *rpc.Instance, sk *sketch.Sketch) *discovery.Port {
discoveryPort, err := p.GetPort(instance, sk)
if err != nil {
feedback.Errorf(tr("Error discovering port: %v"), err)
os.Exit(errorcodes.ErrGeneric)
}
return discoveryPort
}
66 changes: 66 additions & 0 deletions cli/arguments/post_install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// This file is part of arduino-cli.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc.

package arguments

import (
"github.com/arduino/arduino-cli/configuration"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

// PostInstallFlags contains flags data used by the core install and the upgrade command
// This is useful so all flags used by commands that need
// this information are consistent with each other.
type PostInstallFlags struct {
runPostInstall bool // force the execution of installation scripts
skipPostInstall bool //skip the execution of installation scripts
}

// AddToCommand adds flags that can be used to force running or skipping
// of post installation scripts
func (p *PostInstallFlags) AddToCommand(cmd *cobra.Command) {
cmd.Flags().BoolVar(&p.runPostInstall, "run-post-install", false, tr("Force run of post-install scripts (if the CLI is not running interactively)."))
cmd.Flags().BoolVar(&p.skipPostInstall, "skip-post-install", false, tr("Force skip of post-install scripts (if the CLI is running interactively)."))
}

// GetRunPostInstall returns the run-post-install flag value
func (p *PostInstallFlags) GetRunPostInstall() bool {
return p.runPostInstall
}

// GetSkipPostInstall returns the skip-post-install flag value
func (p *PostInstallFlags) GetSkipPostInstall() bool {
return p.skipPostInstall
}

// DetectSkipPostInstallValue returns true if a post install script must be run
func (p *PostInstallFlags) DetectSkipPostInstallValue() bool {
if p.GetRunPostInstall() {
logrus.Info("Will run post-install by user request")
return false
}
if p.GetSkipPostInstall() {
logrus.Info("Will skip post-install by user request")
return true
}

if !configuration.IsInteractive {
logrus.Info("Not running from console, will skip post-install by default")
return true
}
logrus.Info("Running from console, will run post-install by default")
return false
}
38 changes: 38 additions & 0 deletions cli/arguments/programmer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// This file is part of arduino-cli.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc.

package arguments

import "github.com/spf13/cobra"

// Programmer contains the programmer flag data.
// This is useful so all flags used by commands that need
// this information are consistent with each other.
type Programmer struct {
programmer string
}

// AddToCommand adds the flags used to set the programmer to the specified Command
func (p *Programmer) AddToCommand(cmd *cobra.Command) {
cmd.Flags().StringVarP(&p.programmer, "programmer", "P", "", tr("Programmer to use, e.g: atmel_ice"))
cmd.RegisterFlagCompletionFunc("programmer", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return GetInstalledProgrammers(), cobra.ShellCompDirectiveDefault
})
}

// String returns the programmer
func (p *Programmer) String() string {
return p.programmer
}
39 changes: 32 additions & 7 deletions cli/arguments/sketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package arguments
import (
"os"

"github.com/arduino/arduino-cli/arduino/sketch"
"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/go-paths-helper"
Expand All @@ -26,16 +27,40 @@ import (

// InitSketchPath returns an instance of paths.Path pointing to sketchPath.
// If sketchPath is an empty string returns the current working directory.
func InitSketchPath(sketchPath string) *paths.Path {
if sketchPath != "" {
return paths.New(sketchPath)
// In both cases it warns the user if he's using deprecated files
func InitSketchPath(path string) (sketchPath *paths.Path) {
if path != "" {
sketchPath = paths.New(path)
} else {
wd, err := paths.Getwd()
if err != nil {
feedback.Errorf(tr("Couldn't get current working directory: %v"), err)
os.Exit(errorcodes.ErrGeneric)
}
logrus.Infof("Reading sketch from dir: %s", wd)
sketchPath = wd
}
WarnDeprecatedFiles(sketchPath)
return sketchPath
}

wd, err := paths.Getwd()
// NewSketch is a helper function useful to create a sketch instance
func NewSketch(sketchPath *paths.Path) *sketch.Sketch {
sketch, err := sketch.New(sketchPath)
if err != nil {
feedback.Errorf(tr("Couldn't get current working directory: %v"), err)
feedback.Errorf(tr("Error creating sketch: %v"), err)
os.Exit(errorcodes.ErrGeneric)
}
logrus.Infof("Reading sketch from dir: %s", wd)
return wd
return sketch
}

// WarnDeprecatedFiles warns the user that a type of sketch files are deprecated
func WarnDeprecatedFiles(sketchPath *paths.Path) {
// .pde files are still supported but deprecated, this warning urges the user to rename them
if files := sketch.CheckForPdeFiles(sketchPath); len(files) > 0 {
feedback.Error(tr("Sketches with .pde extension are deprecated, please rename the following files to .ino:"))
for _, f := range files {
feedback.Error(f)
}
}
}
43 changes: 28 additions & 15 deletions cli/board/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,43 +27,56 @@ import (
"github.com/arduino/arduino-cli/cli/output"
"github.com/arduino/arduino-cli/commands/board"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var (
port arguments.Port
)

func initAttachCommand() *cobra.Command {
attachCommand := &cobra.Command{
Use: fmt.Sprintf("attach <%s>|<%s> [%s]", tr("port"), tr("FQBN"), tr("sketchPath")),
Use: fmt.Sprintf("attach -p <%s>|-b <%s> [%s]", tr("port"), tr("FQBN"), tr("sketchPath")),
Short: tr("Attaches a sketch to a board."),
Long: tr("Attaches a sketch to a board."),
Example: " " + os.Args[0] + " board attach serial:///dev/ttyACM0\n" +
" " + os.Args[0] + " board attach serial:///dev/ttyACM0 HelloWorld\n" +
" " + os.Args[0] + " board attach arduino:samd:mkr1000",
Args: cobra.RangeArgs(1, 2),
Example: " " + os.Args[0] + " board attach -p /dev/ttyACM0\n" +
" " + os.Args[0] + " board attach -p /dev/ttyACM0 HelloWorld\n" +
" " + os.Args[0] + " board attach -b arduino:samd:mkr1000",
Args: cobra.MaximumNArgs(1),
Run: runAttachCommand,
}
attachCommand.Flags().StringVar(&attachFlags.searchTimeout, "timeout", "5s",
tr("The connected devices search timeout, raise it if your board doesn't show up (e.g. to %s).", "10s"))
return attachCommand
}
fqbn.AddToCommand(attachCommand)
port.AddToCommand(attachCommand)

var attachFlags struct {
searchTimeout string // Expressed in a parsable duration, is the timeout for the list and attach commands.
return attachCommand
}

func runAttachCommand(cmd *cobra.Command, args []string) {
instance := instance.CreateAndInit()

logrus.Info("Executing `arduino-cli board attach`")

path := ""
if len(args) > 1 {
path = args[1]
if len(args) > 0 {
path = args[0]
}
sketchPath := arguments.InitSketchPath(path)

// ugly hack to allow user to specify fqbn and port as flags (consistency)
// a more meaningful fix would be to fix board.Attach
var boardURI string
discoveryPort, _ := port.GetPort(instance, nil)
if fqbn.String() != "" {
boardURI = fqbn.String()
} else if discoveryPort != nil {
boardURI = discoveryPort.Address
}
if _, err := board.Attach(context.Background(), &rpc.BoardAttachRequest{
Instance: instance,
BoardUri: args[0],
BoardUri: boardURI,
SketchPath: sketchPath.String(),
SearchTimeout: attachFlags.searchTimeout,
SearchTimeout: port.GetSearchTimeout().String(),
}, output.TaskProgress()); err != nil {
feedback.Errorf(tr("Attach board error: %v"), err)
os.Exit(errorcodes.ErrGeneric)
Expand Down
3 changes: 3 additions & 0 deletions cli/board/board.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ package board
import (
"os"

"github.com/arduino/arduino-cli/i18n"
"github.com/spf13/cobra"
)

var tr = i18n.Tr

// NewCommand created a new `board` command
func NewCommand() *cobra.Command {
boardCommand := &cobra.Command{
Expand Down
Loading