Skip to content

Commit 2d7dc52

Browse files
committed
apply suggestion from code review
1 parent 80be710 commit 2d7dc52

File tree

11 files changed

+211
-143
lines changed

11 files changed

+211
-143
lines changed

cli/arguments/arguments.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,25 @@
1515

1616
package arguments
1717

18-
import "github.com/arduino/arduino-cli/i18n"
18+
import (
19+
"os"
20+
"strings"
21+
22+
"github.com/arduino/arduino-cli/cli/errorcodes"
23+
"github.com/arduino/arduino-cli/cli/feedback"
24+
"github.com/arduino/arduino-cli/i18n"
25+
"github.com/spf13/cobra"
26+
)
1927

2028
var tr = i18n.Tr
29+
30+
// CheckFlagsConflicts is a helper function useful to report errors when more than one conflicting flag is used
31+
func CheckFlagsConflicts(command *cobra.Command, flagNames ...string) {
32+
for _, flagName := range flagNames {
33+
if !command.Flag(flagName).Changed {
34+
return
35+
}
36+
}
37+
feedback.Errorf(tr("Can't use %s flags at the same time.", "--"+strings.Join(flagNames, " "+tr("and")+" --")))
38+
os.Exit(errorcodes.ErrBadArgument)
39+
}

cli/arguments/fqbn.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to license@arduino.cc.
15+
16+
package arguments
17+
18+
import "github.com/spf13/cobra"
19+
20+
// Fqbn contains the fqbn flag data.
21+
// This is useful so all flags used by commands that need
22+
// this information are consistent with each other.
23+
type Fqbn struct {
24+
fqbn string
25+
}
26+
27+
// AddToCommand adds the flags used to set fqbn to the specified Command
28+
func (f *Fqbn) AddToCommand(cmd *cobra.Command) {
29+
cmd.Flags().StringVarP(&f.fqbn, "fqbn", "b", "", tr("Fully Qualified Board Name, e.g.: arduino:avr:uno"))
30+
cmd.RegisterFlagCompletionFunc("fqbn", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
31+
return GetInstalledBoards(), cobra.ShellCompDirectiveDefault
32+
})
33+
}
34+
35+
// GetFQBN returns the fqbn
36+
func (f *Fqbn) GetFQBN() string {
37+
return f.fqbn
38+
}
39+
40+
// SetFQBN sets the fqbn
41+
func (f *Fqbn) SetFQBN(fqbn string) {
42+
f.fqbn = fqbn
43+
}

cli/arguments/port.go

Lines changed: 0 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,12 @@ package arguments
1818
import (
1919
"fmt"
2020
"net/url"
21-
"os"
2221
"time"
2322

2423
"github.com/arduino/arduino-cli/arduino/discovery"
2524
"github.com/arduino/arduino-cli/arduino/sketch"
26-
"github.com/arduino/arduino-cli/cli/errorcodes"
2725
"github.com/arduino/arduino-cli/cli/feedback"
2826
"github.com/arduino/arduino-cli/commands"
29-
"github.com/arduino/arduino-cli/configuration"
3027
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
3128
"github.com/pkg/errors"
3229
"github.com/sirupsen/logrus"
@@ -42,26 +39,6 @@ type Port struct {
4239
timeout time.Duration
4340
}
4441

45-
// Fqbn contains the Fqbn arguments result.
46-
// This is useful so all flags used by commands that need
47-
// this information are consistent with each other.
48-
type Fqbn struct {
49-
fqbn string
50-
}
51-
52-
// Programmer contains the arguments result.
53-
// This is useful so all flags used by commands that need
54-
// this information are consistent with each other.
55-
type Programmer struct {
56-
programmer string
57-
}
58-
59-
// PostInstallFlags contains flags used by the
60-
type PostInstallFlags struct {
61-
runPostInstall bool // force the execution of installation scripts
62-
skipPostInstall bool //skip the execution of installation scripts
63-
}
64-
6542
// AddToCommand adds the flags used to set port and protocol to the specified Command
6643
func (p *Port) AddToCommand(cmd *cobra.Command) {
6744
cmd.Flags().StringVarP(&p.address, "port", "p", "", tr("Upload port address, e.g.: COM3 or /dev/ttyACM2"))
@@ -75,54 +52,6 @@ func (p *Port) AddToCommand(cmd *cobra.Command) {
7552
cmd.Flags().DurationVar(&p.timeout, "discovery-timeout", 5*time.Second, tr("Max time to wait for port discovery, e.g.: 30s, 1m"))
7653
}
7754

78-
// AddToCommand adds the flags used to set fqbn to the specified Command
79-
func (f *Fqbn) AddToCommand(cmd *cobra.Command) {
80-
cmd.Flags().StringVarP(&f.fqbn, "fqbn", "b", "", tr("Fully Qualified Board Name, e.g.: arduino:avr:uno"))
81-
cmd.RegisterFlagCompletionFunc("fqbn", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
82-
return GetInstalledBoards(), cobra.ShellCompDirectiveDefault
83-
})
84-
}
85-
86-
// AddToCommand adds the flags used to set the programmer to the specified Command
87-
func (p *Programmer) AddToCommand(cmd *cobra.Command) {
88-
cmd.Flags().StringVarP(&p.programmer, "programmer", "P", "", tr("Use the specified programmer to upload."))
89-
cmd.RegisterFlagCompletionFunc("programmer", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
90-
return GetInstalledProgrammers(), cobra.ShellCompDirectiveDefault
91-
})
92-
}
93-
94-
// AddToCommand adds flags that can be used to force running or skipping
95-
// of post installation scripts
96-
func (p *PostInstallFlags) AddToCommand(cmd *cobra.Command) {
97-
cmd.Flags().BoolVar(&p.runPostInstall, "run-post-install", false, tr("Force run of post-install scripts (if the CLI is not running interactively)."))
98-
cmd.Flags().BoolVar(&p.skipPostInstall, "skip-post-install", false, tr("Force skip of post-install scripts (if the CLI is running interactively)."))
99-
}
100-
101-
// GetRunPostInstall returns the runPostInstall flag
102-
func (p *PostInstallFlags) GetRunPostInstall() bool {
103-
return p.runPostInstall
104-
}
105-
106-
// GetSkipPostInstall returns the skipPostInstall
107-
func (p *PostInstallFlags) GetSkipPostInstall() bool {
108-
return p.skipPostInstall
109-
}
110-
111-
// GetFQBN returns the fqbn
112-
func (f *Fqbn) GetFQBN() string {
113-
return f.fqbn
114-
}
115-
116-
// SetFQBN sets the fqbn
117-
func (f *Fqbn) SetFQBN(fqbn string) {
118-
f.fqbn = fqbn
119-
}
120-
121-
// GetProgrammer returns the programmer
122-
func (p *Programmer) GetProgrammer() string {
123-
return p.programmer
124-
}
125-
12655
// GetPortAddressAndProtocol returns only the port address and the port protocol
12756
// without any other port metadata obtained from the discoveries. This method allows
12857
// to bypass the discoveries unless the protocol is not specified: in this
@@ -220,32 +149,3 @@ func (p *Port) GetPort(instance *rpc.Instance, sk *sketch.Sketch) (*discovery.Po
220149
func (p *Port) GetSearchTimeout() time.Duration {
221150
return p.timeout
222151
}
223-
224-
// CheckFlagsConflicts is a helper function useful to report errors when 2 conflicting flags are used
225-
func CheckFlagsConflicts(command *cobra.Command, firstFlagName, secondFlagName string) {
226-
flag1, _ := command.Flags().GetString(firstFlagName)
227-
flag2, _ := command.Flags().GetString(secondFlagName)
228-
if flag1 != "" && flag2 != "" {
229-
feedback.Errorf(tr("Can't use both --%s and --%s flags at the same time.", firstFlagName, secondFlagName))
230-
os.Exit(errorcodes.ErrBadArgument)
231-
}
232-
}
233-
234-
// DetectSkipPostInstallValue returns true if a post install script must be run
235-
func (p *PostInstallFlags) DetectSkipPostInstallValue() bool {
236-
if p.GetRunPostInstall() {
237-
logrus.Info("Will run post-install by user request")
238-
return false
239-
}
240-
if p.GetSkipPostInstall() {
241-
logrus.Info("Will skip post-install by user request")
242-
return true
243-
}
244-
245-
if !configuration.IsInteractive {
246-
logrus.Info("Not running from console, will skip post-install by default")
247-
return true
248-
}
249-
logrus.Info("Running from console, will run post-install by default")
250-
return false
251-
}

cli/arguments/post_install.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to license@arduino.cc.
15+
16+
package arguments
17+
18+
import (
19+
"github.com/arduino/arduino-cli/configuration"
20+
"github.com/sirupsen/logrus"
21+
"github.com/spf13/cobra"
22+
)
23+
24+
// PostInstallFlags contains flags data used by the core install and the upgrade command
25+
// This is useful so all flags used by commands that need
26+
// this information are consistent with each other.
27+
type PostInstallFlags struct {
28+
runPostInstall bool // force the execution of installation scripts
29+
skipPostInstall bool //skip the execution of installation scripts
30+
}
31+
32+
// AddToCommand adds flags that can be used to force running or skipping
33+
// of post installation scripts
34+
func (p *PostInstallFlags) AddToCommand(cmd *cobra.Command) {
35+
cmd.Flags().BoolVar(&p.runPostInstall, "run-post-install", false, tr("Force run of post-install scripts (if the CLI is not running interactively)."))
36+
cmd.Flags().BoolVar(&p.skipPostInstall, "skip-post-install", false, tr("Force skip of post-install scripts (if the CLI is running interactively)."))
37+
}
38+
39+
// GetRunPostInstall returns the run-post-install flag value
40+
func (p *PostInstallFlags) GetRunPostInstall() bool {
41+
return p.runPostInstall
42+
}
43+
44+
// GetSkipPostInstall returns the skip-post-install flag value
45+
func (p *PostInstallFlags) GetSkipPostInstall() bool {
46+
return p.skipPostInstall
47+
}
48+
49+
// DetectSkipPostInstallValue returns true if a post install script must be run
50+
func (p *PostInstallFlags) DetectSkipPostInstallValue() bool {
51+
if p.GetRunPostInstall() {
52+
logrus.Info("Will run post-install by user request")
53+
return false
54+
}
55+
if p.GetSkipPostInstall() {
56+
logrus.Info("Will skip post-install by user request")
57+
return true
58+
}
59+
60+
if !configuration.IsInteractive {
61+
logrus.Info("Not running from console, will skip post-install by default")
62+
return true
63+
}
64+
logrus.Info("Running from console, will run post-install by default")
65+
return false
66+
}

cli/arguments/programmer.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to license@arduino.cc.
15+
16+
package arguments
17+
18+
import "github.com/spf13/cobra"
19+
20+
// Programmer contains the programmer flag data.
21+
// This is useful so all flags used by commands that need
22+
// this information are consistent with each other.
23+
type Programmer struct {
24+
programmer string
25+
}
26+
27+
// AddToCommand adds the flags used to set the programmer to the specified Command
28+
func (p *Programmer) AddToCommand(cmd *cobra.Command) {
29+
cmd.Flags().StringVarP(&p.programmer, "programmer", "P", "", tr("Programmer to use, e.g: atmel_ice"))
30+
cmd.RegisterFlagCompletionFunc("programmer", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
31+
return GetInstalledProgrammers(), cobra.ShellCompDirectiveDefault
32+
})
33+
}
34+
35+
// GetProgrammer returns the programmer
36+
func (p *Programmer) GetProgrammer() string {
37+
return p.programmer
38+
}

cli/arguments/sketch.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,26 @@ func InitSketchPath(sketchPath string) *paths.Path {
4343
return wd
4444
}
4545

46-
// SketchPathCalculation creates the path of a sketch, optionally it warns the user when using pde files.
47-
// it also gets the port of a board
48-
// it returns the created sketch and the discovery port
46+
// SketchPathCalculation returns the path and instance of the sketch and the discovery port.
47+
// Optionally, it warns the user when the sketch contains .pde files.
4948
func SketchPathCalculation(path string, port Port, instance *rpc.Instance, warn bool) (*paths.Path, *sketch.Sketch, *discovery.Port) {
5049
sketchPath := InitSketch(path, warn)
5150
sketch := CreateSketch(sketchPath)
5251
discoveryPort := GetDiscoveryPort(instance, port, sketch)
5352
return sketchPath, sketch, discoveryPort
5453
}
5554

56-
// InitSketch is a helper function useful to create the sketch path, optionally it warns the user when using pde files.
55+
// InitSketch is a helper function that determines the sketch path. Optionally, it warns the user when the sketch contains .pde files.
5756
func InitSketch(path string, warn bool) *paths.Path {
5857
sketchPath := InitSketchPath(path)
5958

6059
if warn {
61-
WarnDeprecatedPdeFiles(sketchPath)
60+
WarnDeprecatedFiles(sketchPath)
6261
}
6362
return sketchPath
6463
}
6564

66-
// CreateSketch is a helper function usefull to create a sketch
65+
// CreateSketch is a helper function useful to create a sketch instance
6766
func CreateSketch(sketchPath *paths.Path) *sketch.Sketch {
6867
sketch, err := sketch.New(sketchPath)
6968
if err != nil {
@@ -73,8 +72,8 @@ func CreateSketch(sketchPath *paths.Path) *sketch.Sketch {
7372
return sketch
7473
}
7574

76-
// WarnDeprecatedPdeFiles warns the user that .pde files are deprecated
77-
func WarnDeprecatedPdeFiles(sketchPath *paths.Path) {
75+
// WarnDeprecatedFiles warns the user that a type of sketch files are deprecated
76+
func WarnDeprecatedFiles(sketchPath *paths.Path) {
7877
// .pde files are still supported but deprecated, this warning urges the user to rename them
7978
if files := sketch.CheckForPdeFiles(sketchPath); len(files) > 0 {
8079
feedback.Error(tr("Sketches with .pde extension are deprecated, please rename the following files to .ino:"))

cli/instance/instance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func FirstUpdate(instance *rpc.Instance) error {
142142
}
143143

144144
// CreateInstanceForUpdate creates an instance and runs `FirstUpdate`.
145-
// This is a mandatory to run all `update-index` commands
145+
// It is mandatory for all `update-index` commands to call this
146146
func CreateInstanceForUpdate() *rpc.Instance {
147147
// We don't initialize any CoreInstance when updating indexes since we don't need to.
148148
// Also meaningless errors might be returned when calling this command with --additional-urls

cli/lib/list.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
var (
3636
all bool
3737
updatable bool
38-
// fqbn arguments.Fqbn //already defined in examples.go
3938
)
4039

4140
func initListCommand() *cobra.Command {

cli/sketch/archive.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func runArchiveCommand(cmd *cobra.Command, args []string) {
6161
sketchPath = paths.New(args[0])
6262
}
6363

64-
arguments.WarnDeprecatedPdeFiles(sketchPath)
64+
arguments.WarnDeprecatedFiles(sketchPath)
6565

6666
archivePath := ""
6767
if len(args) == 2 {

0 commit comments

Comments
 (0)