Skip to content

Commit 03d593c

Browse files
committed
Made cobra initialization less static and less dependent on init()
This turns out to be super useful for testing since, otherwise, cobra will remember the flags settings if a batch of test is runned in sequence.
1 parent 6d37d8d commit 03d593c

31 files changed

+357
-378
lines changed

commands/board/attach.go

+10-11
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,25 @@ var validSerialBoardURIRegexp = regexp.MustCompile("(serial|tty)://.+")
5050
var validNetworkBoardURIRegexp = regexp.MustCompile("(http(s)?|(tc|ud)p)://[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}:[0-9]{1,5}")
5151
var validFQBN = regexp.MustCompile(".+:.+:.+")
5252

53-
func init() {
54-
command.AddCommand(attachCommand)
53+
func initAttachCommand() *cobra.Command {
54+
attachCommand := &cobra.Command{
55+
Use: "attach <port>|<FQBN> [sketchPath]",
56+
Short: "Attaches a sketch to a board.",
57+
Long: "Attaches a sketch to a board.",
58+
Example: "arduino board attach serial:///dev/tty/ACM0",
59+
Args: cobra.RangeArgs(1, 2),
60+
Run: runAttachCommand,
61+
}
5562
attachCommand.Flags().StringVar(&attachFlags.boardFlavour, "flavour", "default", "The Name of the CPU flavour, it is required for some boards (e.g. Arduino Nano).")
5663
attachCommand.Flags().StringVar(&attachFlags.searchTimeout, "timeout", "5s", "The timeout of the search of connected devices, try to high it if your board is not found (e.g. to 10s).")
64+
return attachCommand
5765
}
5866

5967
var attachFlags struct {
6068
boardFlavour string // The flavour of the chipset of the cpu of the connected board, if not specified it is set to "default".
6169
searchTimeout string // Expressed in a parsable duration, is the timeout for the list and attach commands.
6270
}
6371

64-
var attachCommand = &cobra.Command{
65-
Use: "attach <port>|<FQBN> [sketchPath]",
66-
Short: "Attaches a sketch to a board.",
67-
Long: "Attaches a sketch to a board.",
68-
Example: "arduino board attach serial:///dev/tty/ACM0",
69-
Args: cobra.RangeArgs(1, 2),
70-
Run: runAttachCommand,
71-
}
72-
7372
func runAttachCommand(cmd *cobra.Command, args []string) {
7473
boardURI := args[0]
7574
sketchPath := ""

commands/board/board.go

+14-13
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,18 @@ import (
3333
"github.com/spf13/cobra"
3434
)
3535

36-
// Init prepares the command.
37-
func Init(rootCommand *cobra.Command) {
38-
rootCommand.AddCommand(command)
39-
}
40-
41-
var command = &cobra.Command{
42-
Use: "board",
43-
Short: "Arduino board commands.",
44-
Long: "Arduino board commands.",
45-
Example: "" +
46-
"arduino board list # Lists all connected boards.\n" +
47-
"arduino board attach --board serial:///dev/tty/ACM0 \\\n" +
48-
" --sketch mySketch # Attaches a sketch to a board.",
36+
// InitCommand prepares the command.
37+
func InitCommand() *cobra.Command {
38+
boardCommand := &cobra.Command{
39+
Use: "board",
40+
Short: "Arduino board commands.",
41+
Long: "Arduino board commands.",
42+
Example: "" +
43+
"arduino board list # Lists all connected boards.\n" +
44+
"arduino board attach --board serial:///dev/tty/ACM0 \\\n" +
45+
" --sketch mySketch # Attaches a sketch to a board.",
46+
}
47+
boardCommand.AddCommand(initAttachCommand())
48+
boardCommand.AddCommand(initListCommand())
49+
return boardCommand
4950
}

commands/board/list.go

+10-11
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,23 @@ import (
1717
"github.com/spf13/cobra"
1818
)
1919

20-
func init() {
21-
command.AddCommand(listCommand)
20+
func initListCommand() *cobra.Command {
21+
listCommand := &cobra.Command{
22+
Use: "list",
23+
Short: "List connected boards.",
24+
Long: "Detects and displays a list of connected boards to the current computer.",
25+
Example: "arduino board list --timeout 10s",
26+
Args: cobra.NoArgs,
27+
Run: runListCommand,
28+
}
2229
listCommand.Flags().StringVar(&listFlags.timeout, "timeout", "5s", "The timeout of the search of connected devices, try to high it if your board is not found (e.g. to 10s).")
30+
return listCommand
2331
}
2432

2533
var listFlags struct {
2634
timeout string // Expressed in a parsable duration, is the timeout for the list and attach commands.
2735
}
2836

29-
var listCommand = &cobra.Command{
30-
Use: "list",
31-
Short: "List connected boards.",
32-
Long: "Detects and displays a list of connected boards to the current computer.",
33-
Example: "arduino board list --timeout 10s",
34-
Args: cobra.NoArgs,
35-
Run: runListCommand,
36-
}
37-
3837
// runListCommand detects and lists the connected arduino boards
3938
// (either via serial or network ports).
4039
func runListCommand(cmd *cobra.Command, args []string) {

commands/commands_test.go

+3-7
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
"io/ioutil"
3636
"os"
3737
"strconv"
38-
"strings"
3938
"testing"
4039

4140
"github.com/bcmi-labs/arduino-cli/commands"
@@ -88,11 +87,6 @@ func executeWithArgs(t *testing.T, args ...string) (exitCode int, output []byte)
8887

8988
t.Logf("Running: %s", args)
9089

91-
// Init only once.
92-
if !root.Command.HasFlags() {
93-
root.Init()
94-
}
95-
root.Command.SetArgs(args)
9690

9791
// Mock the os.Exit function, so that we can use the
9892
// error result for the test and prevent the test from exiting
@@ -114,7 +108,9 @@ func executeWithArgs(t *testing.T, args ...string) (exitCode int, output []byte)
114108
}()
115109

116110
// Execute the CLI command, in this process
117-
root.Command.Execute()
111+
cmd := root.Init()
112+
cmd.SetArgs(args)
113+
cmd.Execute()
118114

119115
exitCode = 0
120116
output = redirect.GetOutput()

commands/compile/compile.go

+11-12
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,16 @@ import (
5050
"github.com/spf13/cobra"
5151
)
5252

53-
// Init prepares the command.
54-
func Init(rootCommand *cobra.Command) {
55-
rootCommand.AddCommand(command)
53+
// InitCommand prepares the command.
54+
func InitCommand() *cobra.Command {
55+
command := &cobra.Command{
56+
Use: "compile",
57+
Short: "Compiles Arduino sketches.",
58+
Long: "Compiles Arduino sketches.",
59+
Example: "arduino compile [sketchPath]",
60+
Args: cobra.MaximumNArgs(1),
61+
Run: run,
62+
}
5663
command.Flags().StringVarP(&flags.fqbn, "fqbn", "b", "", "Fully Qualified Board Name, e.g.: arduino:avr:uno")
5764
command.Flags().BoolVar(&flags.showProperties, "show-properties", false, "Show all build properties used instead of compiling.")
5865
command.Flags().BoolVar(&flags.preprocess, "preprocess", false, "Print preprocessed code to stdout instead of compiling.")
@@ -64,6 +71,7 @@ func Init(rootCommand *cobra.Command) {
6471
command.Flags().BoolVar(&flags.quiet, "quiet", false, "Optional, supresses almost every output.")
6572
command.Flags().IntVar(&flags.debugLevel, "debug-level", 5, "Optional, defaults to 5. Used for debugging. Set it to 10 when submitting an issue.")
6673
command.Flags().StringVar(&flags.vidPid, "vid-pid", "", "When specified, VID/PID specific build properties are used, if boards supports them.")
74+
return command
6775
}
6876

6977
var flags struct {
@@ -80,15 +88,6 @@ var flags struct {
8088
vidPid string // VID/PID specific build properties.
8189
}
8290

83-
var command = &cobra.Command{
84-
Use: "compile",
85-
Short: "Compiles Arduino sketches.",
86-
Long: "Compiles Arduino sketches.",
87-
Example: "arduino compile [sketchPath]",
88-
Args: cobra.MaximumNArgs(1),
89-
Run: run,
90-
}
91-
9291
func run(cmd *cobra.Command, args []string) {
9392
logrus.Info("Executing `arduino compile`")
9493
sketchPath := ""

commands/config/config.go

+11-12
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,16 @@
2929

3030
package config
3131

32-
import (
33-
"github.com/spf13/cobra"
34-
)
32+
import "github.com/spf13/cobra"
3533

36-
// Init prepares the command.
37-
func Init(rootCommand *cobra.Command) {
38-
rootCommand.AddCommand(command)
39-
}
40-
41-
var command = &cobra.Command{
42-
Use: "config",
43-
Short: "Arduino Configuration Commands.",
44-
Example: "arduino config init",
34+
// InitCommand prepares the command.
35+
func InitCommand() *cobra.Command {
36+
configCommand := &cobra.Command{
37+
Use: "config",
38+
Short: "Arduino Configuration Commands.",
39+
Example: "arduino config init",
40+
}
41+
configCommand.AddCommand(initDumpCommand())
42+
configCommand.AddCommand(initInitCommand())
43+
return configCommand
4544
}

commands/config/dump.go

+9-11
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,22 @@ import (
4040
"github.com/spf13/cobra"
4141
)
4242

43-
func init() {
44-
command.AddCommand(dumpCommand)
43+
func initDumpCommand() *cobra.Command {
44+
return &cobra.Command{
45+
Use: "dump",
46+
Short: "Prints the current configuration",
47+
Long: "Prints the current configuration.",
48+
Example: "arduino config dump",
49+
Args: cobra.NoArgs,
50+
Run: runDumpCommand,
51+
}
4552
}
4653

4754
var dumpFlags struct {
4855
_default bool // If false, ask questions to the user about setting configuration properties, otherwise use default configuration.
4956
location string // The custom location of the file to create.
5057
}
5158

52-
var dumpCommand = &cobra.Command{
53-
Use: "dump",
54-
Short: "Prints the current configuration",
55-
Long: "Prints the current configuration.",
56-
Example: "arduino config dump",
57-
Args: cobra.NoArgs,
58-
Run: runDumpCommand,
59-
}
60-
6159
func runDumpCommand(cmd *cobra.Command, args []string) {
6260
logrus.Info("Executing `arduino config dump`")
6361

commands/config/init.go

+12-13
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,27 @@ import (
3939
"github.com/spf13/cobra"
4040
)
4141

42-
func init() {
43-
command.AddCommand(initCommand)
42+
func initInitCommand() *cobra.Command {
43+
initCommand := &cobra.Command{
44+
Use: "init",
45+
Short: "Initializes a new config file into the default location.",
46+
Long: "Initializes a new config file into the default location ($EXE_DIR/cli-config.yml).",
47+
Example: "" +
48+
"arduino config init # Creates a config file by asking questions to the user into the default location.\n" +
49+
"arduino config init --default # Creates a config file with default configuration into default location.",
50+
Args: cobra.NoArgs,
51+
Run: runInitCommand,
52+
}
4453
initCommand.Flags().BoolVar(&initFlags._default, "default", false, "If omitted, ask questions to the user about setting configuration properties, otherwise use default configuration.")
4554
initCommand.Flags().StringVar(&initFlags.location, "save-as", "", "Sets where to save the configuration file [default is ./.cli-config.yml].")
55+
return initCommand
4656
}
4757

4858
var initFlags struct {
4959
_default bool // If false, ask questions to the user about setting configuration properties, otherwise use default configuration.
5060
location string // The custom location of the file to create.
5161
}
5262

53-
var initCommand = &cobra.Command{
54-
Use: "init",
55-
Short: "Initializes a new config file into the default location.",
56-
Long: "Initializes a new config file into the default location ($EXE_DIR/cli-config.yml).",
57-
Example: "" +
58-
"arduino config init # Creates a config file by asking questions to the user into the default location.\n" +
59-
"arduino config init --default # Creates a config file with default configuration into default location.",
60-
Args: cobra.NoArgs,
61-
Run: runInitCommand,
62-
}
63-
6463
func runInitCommand(cmd *cobra.Command, args []string) {
6564
logrus.Info("Executing `arduino config init`")
6665

commands/core/core.go

+13-10
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,17 @@ import (
3333
"github.com/spf13/cobra"
3434
)
3535

36-
// Init prepares the command.
37-
func Init(rootCommand *cobra.Command) {
38-
rootCommand.AddCommand(command)
39-
}
40-
41-
var command = &cobra.Command{
42-
Use: "core",
43-
Short: "Arduino Core operations.",
44-
Long: "Arduino Core operations.",
45-
Example: "arduino core update-index # to update the package index file.",
36+
// InitCommand prepares the command.
37+
func InitCommand() *cobra.Command {
38+
coreCommand := &cobra.Command{
39+
Use: "core",
40+
Short: "Arduino Core operations.",
41+
Long: "Arduino Core operations.",
42+
Example: "arduino core update-index # to update the package index file.",
43+
}
44+
coreCommand.AddCommand(initDownloadCommand())
45+
coreCommand.AddCommand(initInstallCommand())
46+
coreCommand.AddCommand(initListCommand())
47+
coreCommand.AddCommand(initUpdateIndexCommand())
48+
return coreCommand
4649
}

commands/core/download.go

+12-13
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,18 @@ import (
4040
"github.com/spf13/cobra"
4141
)
4242

43-
func init() {
44-
command.AddCommand(downloadCommand)
45-
}
46-
47-
var downloadCommand = &cobra.Command{
48-
Use: "download [PACKAGER:ARCH[=VERSION]](S)",
49-
Short: "Downloads one or more cores and corresponding tool dependencies.",
50-
Long: "Downloads one or more cores and corresponding tool dependencies.",
51-
Example: "" +
52-
"arduino core download arduino:samd # to download the latest version of arduino SAMD core.\n" +
53-
"arduino core download arduino:samd=1.6.9 # for a specific version (in this case 1.6.9).",
54-
Args: cobra.MinimumNArgs(1),
55-
Run: runDownloadCommand,
43+
func initDownloadCommand() *cobra.Command {
44+
downloadCommand := &cobra.Command{
45+
Use: "download [PACKAGER:ARCH[=VERSION]](S)",
46+
Short: "Downloads one or more cores and corresponding tool dependencies.",
47+
Long: "Downloads one or more cores and corresponding tool dependencies.",
48+
Example: "" +
49+
"arduino core download arduino:samd # to download the latest version of arduino SAMD core.\n" +
50+
"arduino core download arduino:samd=1.6.9 # for a specific version (in this case 1.6.9).",
51+
Args: cobra.MinimumNArgs(1),
52+
Run: runDownloadCommand,
53+
}
54+
return downloadCommand
5655
}
5756

5857
func runDownloadCommand(cmd *cobra.Command, args []string) {

commands/core/install.go

+12-13
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,18 @@ import (
3939
"github.com/spf13/cobra"
4040
)
4141

42-
func init() {
43-
command.AddCommand(installCommand)
44-
}
45-
46-
var installCommand = &cobra.Command{
47-
Use: "install [PACKAGER:ARCH[=VERSION]](S)",
48-
Short: "Installs one or more cores and corresponding tool dependencies.",
49-
Long: "Installs one or more cores and corresponding tool dependencies.",
50-
Example: "" +
51-
"arduino core install arduino:samd # to download the latest version of arduino SAMD core." +
52-
"arduino core install arduino:samd=1.6.9 # for a specific version (in this case 1.6.9).",
53-
Args: cobra.MinimumNArgs(1),
54-
Run: runInstallCommand,
42+
func initInstallCommand() *cobra.Command {
43+
installCommand := &cobra.Command{
44+
Use: "install [PACKAGER:ARCH[=VERSION]](S)",
45+
Short: "Installs one or more cores and corresponding tool dependencies.",
46+
Long: "Installs one or more cores and corresponding tool dependencies.",
47+
Example: "" +
48+
"arduino core install arduino:samd # to download the latest version of arduino SAMD core." +
49+
"arduino core install arduino:samd=1.6.9 # for a specific version (in this case 1.6.9).",
50+
Args: cobra.MinimumNArgs(1),
51+
Run: runInstallCommand,
52+
}
53+
return installCommand
5554
}
5655

5756
func runInstallCommand(cmd *cobra.Command, args []string) {

0 commit comments

Comments
 (0)