Skip to content

Commit 015f0d7

Browse files
committed
Big refactoring for daemon mode
All the arduino-cli commands implementation has been moved from package `command` to package `cli`. The `board details` command has been choosen as a 'tracing bullet' to reimplement all the cli command using grpc/protoc to generate Request/Response (input/output) structures. We should probably abandon the idea to use a generic `formatter` module since it seems that it doesn't help to reduce boilerplate code (for example in the `board details` command there is no need for the `formatter` module nor it's clear how it can improve things). The `output` module now has only functions for pretty-printing data.
1 parent 94e5765 commit 015f0d7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1131
-542
lines changed

commands/board/attach.go renamed to cli/board/attach.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
"github.com/arduino/arduino-cli/arduino/cores"
2828
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2929
"github.com/arduino/arduino-cli/arduino/sketches"
30-
"github.com/arduino/arduino-cli/commands"
30+
"github.com/arduino/arduino-cli/cli"
3131
"github.com/arduino/arduino-cli/common/formatter"
3232
discovery "github.com/arduino/board-discovery"
3333
paths "github.com/arduino/go-paths-helper"
@@ -40,9 +40,9 @@ func initAttachCommand() *cobra.Command {
4040
Use: "attach <port>|<FQBN> [sketchPath]",
4141
Short: "Attaches a sketch to a board.",
4242
Long: "Attaches a sketch to a board.",
43-
Example: " " + commands.AppName + " board attach serial:///dev/tty/ACM0\n" +
44-
" " + commands.AppName + " board attach serial:///dev/tty/ACM0 HelloWorld\n" +
45-
" " + commands.AppName + " board attach arduino:samd:mkr1000",
43+
Example: " " + cli.AppName + " board attach serial:///dev/tty/ACM0\n" +
44+
" " + cli.AppName + " board attach serial:///dev/tty/ACM0 HelloWorld\n" +
45+
" " + cli.AppName + " board attach arduino:samd:mkr1000",
4646
Args: cobra.RangeArgs(1, 2),
4747
Run: runAttachCommand,
4848
}
@@ -64,10 +64,10 @@ func runAttachCommand(cmd *cobra.Command, args []string) {
6464
if len(args) > 1 {
6565
sketchPath = paths.New(args[1])
6666
}
67-
sketch, err := commands.InitSketch(sketchPath)
67+
sketch, err := cli.InitSketch(sketchPath)
6868
if err != nil {
6969
formatter.PrintError(err, "Error opening sketch.")
70-
os.Exit(commands.ErrGeneric)
70+
os.Exit(cli.ErrGeneric)
7171
}
7272

7373
logrus.WithField("fqbn", boardURI).Print("Parsing FQBN")
@@ -76,7 +76,7 @@ func runAttachCommand(cmd *cobra.Command, args []string) {
7676
boardURI = "serial://" + boardURI
7777
}
7878

79-
pm := commands.InitPackageManager()
79+
pm := cli.InitPackageManager()
8080

8181
if fqbn != nil {
8282
sketch.Metadata.CPU = sketches.BoardMetadata{
@@ -86,7 +86,7 @@ func runAttachCommand(cmd *cobra.Command, args []string) {
8686
deviceURI, err := url.Parse(boardURI)
8787
if err != nil {
8888
formatter.PrintError(err, "The provided Device URL is not in a valid format.")
89-
os.Exit(commands.ErrBadCall)
89+
os.Exit(cli.ErrBadCall)
9090
}
9191

9292
var findBoardFunc func(*packagemanager.PackageManager, *discovery.Monitor, *url.URL) *cores.Board
@@ -97,7 +97,7 @@ func runAttachCommand(cmd *cobra.Command, args []string) {
9797
findBoardFunc = findNetworkConnectedBoard
9898
default:
9999
formatter.PrintErrorMessage("Invalid device port type provided. Accepted types are: serial://, tty://, http://, https://, tcp://, udp://.")
100-
os.Exit(commands.ErrBadCall)
100+
os.Exit(cli.ErrBadCall)
101101
}
102102

103103
duration, err := time.ParseDuration(attachFlags.searchTimeout)
@@ -115,7 +115,7 @@ func runAttachCommand(cmd *cobra.Command, args []string) {
115115
board := findBoardFunc(pm, monitor, deviceURI)
116116
if board == nil {
117117
formatter.PrintErrorMessage("No supported board has been found at " + deviceURI.String() + ", try either install new cores or check your board URI.")
118-
os.Exit(commands.ErrGeneric)
118+
os.Exit(cli.ErrGeneric)
119119
}
120120
formatter.Print("Board found: " + board.Name())
121121

@@ -152,7 +152,7 @@ func findSerialConnectedBoard(pm *packagemanager.PackageManager, monitor *discov
152152

153153
boards := pm.FindBoardsWithVidPid(serialDevice.VendorID, serialDevice.ProductID)
154154
if len(boards) == 0 {
155-
os.Exit(commands.ErrGeneric)
155+
os.Exit(cli.ErrGeneric)
156156
}
157157

158158
return boards[0]

cli/board/board.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* This file is part of arduino-cli.
3+
*
4+
* Copyright 2018 ARDUINO SA (http://www.arduino.cc/)
5+
*
6+
* This software is released under the GNU General Public License version 3,
7+
* which covers the main part of arduino-cli.
8+
* The terms of this license can be found at:
9+
* https://www.gnu.org/licenses/gpl-3.0.en.html
10+
*
11+
* You can be released from the requirements of the above licenses by purchasing
12+
* a commercial license. Buying such a license is mandatory if you want to modify or
13+
* otherwise use the software for commercial activities involving the Arduino
14+
* software without disclosing the source code of your own applications. To purchase
15+
* a commercial license, send an email to license@arduino.cc.
16+
*/
17+
18+
package board
19+
20+
import (
21+
"github.com/arduino/arduino-cli/cli"
22+
"github.com/spf13/cobra"
23+
)
24+
25+
// InitCommand prepares the command.
26+
func InitCommand() *cobra.Command {
27+
boardCommand := &cobra.Command{
28+
Use: "board",
29+
Short: "Arduino board commands.",
30+
Long: "Arduino board commands.",
31+
Example: " # Lists all connected boards.\n" +
32+
" " + cli.AppName + " board list\n\n" +
33+
" # Attaches a sketch to a board.\n" +
34+
" " + cli.AppName + " board attach serial:///dev/tty/ACM0 mySketch",
35+
}
36+
boardCommand.AddCommand(initAttachCommand())
37+
boardCommand.AddCommand(initDetailsCommand())
38+
boardCommand.AddCommand(initListCommand())
39+
boardCommand.AddCommand(initListAllCommand())
40+
return boardCommand
41+
}

cli/board/details.go

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* This file is part of arduino-cli.
3+
*
4+
* Copyright 2018 ARDUINO SA (http://www.arduino.cc/)
5+
*
6+
* This software is released under the GNU General Public License version 3,
7+
* which covers the main part of arduino-cli.
8+
* The terms of this license can be found at:
9+
* https://www.gnu.org/licenses/gpl-3.0.en.html
10+
*
11+
* You can be released from the requirements of the above licenses by purchasing
12+
* a commercial license. Buying such a license is mandatory if you want to modify or
13+
* otherwise use the software for commercial activities involving the Arduino
14+
* software without disclosing the source code of your own applications. To purchase
15+
* a commercial license, send an email to license@arduino.cc.
16+
*/
17+
18+
package board
19+
20+
import (
21+
"fmt"
22+
23+
"github.com/arduino/arduino-cli/cli"
24+
b "github.com/arduino/arduino-cli/commands/board"
25+
"github.com/arduino/arduino-cli/output"
26+
"github.com/spf13/cobra"
27+
)
28+
29+
func initDetailsCommand() *cobra.Command {
30+
detailsCommand := &cobra.Command{
31+
Use: "details <FQBN>",
32+
Short: "Print details about a board.",
33+
Long: "Show information about a board, in particular if the board has options to be specified in the FQBN.",
34+
Example: " " + cli.AppName + " board details arduino:avr:nano",
35+
Args: cobra.ExactArgs(1),
36+
Run: runDetailsCommand,
37+
}
38+
return detailsCommand
39+
}
40+
41+
func runDetailsCommand(cmd *cobra.Command, args []string) {
42+
pm := cli.InitPackageManager()
43+
res := b.Details(pm, &b.DetailsReq{Fqbn: args[0]})
44+
45+
if !cli.OutputJSON(res) {
46+
outputDetailsResp(res)
47+
}
48+
}
49+
50+
func outputDetailsResp(details *b.DetailsResp) {
51+
table := output.NewTable()
52+
table.SetColumnWidthMode(1, output.Average)
53+
table.AddRow("Board name:", details.Name)
54+
for i, tool := range details.RequiredTools {
55+
head := ""
56+
if i == 0 {
57+
table.AddRow()
58+
head = "Required tools:"
59+
}
60+
table.AddRow(head, tool.Packager+":"+tool.Name, "", tool.Version)
61+
}
62+
for _, option := range details.ConfigOptions {
63+
table.AddRow()
64+
table.AddRow("Option:",
65+
option.OptionLabel,
66+
"", option.Option)
67+
for _, value := range option.Values {
68+
if value.Selected {
69+
table.AddRow("",
70+
output.Green(value.ValueLabel),
71+
output.Green("✔"), output.Green(option.Option+"="+value.Value))
72+
} else {
73+
table.AddRow("",
74+
value.ValueLabel,
75+
"", option.Option+"="+value.Value)
76+
}
77+
}
78+
}
79+
fmt.Print(table.Render())
80+
}

commands/board/list.go renamed to cli/board/list.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ import (
2222
"time"
2323

2424
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
25-
"github.com/arduino/arduino-cli/commands"
25+
"github.com/arduino/arduino-cli/cli"
2626
"github.com/arduino/arduino-cli/common/formatter"
2727
"github.com/arduino/arduino-cli/common/formatter/output"
28-
"github.com/arduino/board-discovery"
28+
discovery "github.com/arduino/board-discovery"
2929
"github.com/codeclysm/cc"
3030
"github.com/spf13/cobra"
3131
)
@@ -35,7 +35,7 @@ func initListCommand() *cobra.Command {
3535
Use: "list",
3636
Short: "List connected boards.",
3737
Long: "Detects and displays a list of connected boards to the current computer.",
38-
Example: " " + commands.AppName + " board list --timeout 10s",
38+
Example: " " + cli.AppName + " board list --timeout 10s",
3939
Args: cobra.NoArgs,
4040
Run: runListCommand,
4141
}
@@ -52,7 +52,7 @@ var listFlags struct {
5252
// runListCommand detects and lists the connected arduino boards
5353
// (either via serial or network ports).
5454
func runListCommand(cmd *cobra.Command, args []string) {
55-
pm := commands.InitPackageManager()
55+
pm := cli.InitPackageManager()
5656

5757
monitor := discovery.New(time.Millisecond)
5858
monitor.Start()

commands/board/listall.go renamed to cli/board/listall.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"sort"
2222
"strings"
2323

24-
"github.com/arduino/arduino-cli/commands"
24+
"github.com/arduino/arduino-cli/cli"
2525
"github.com/arduino/arduino-cli/common/formatter"
2626
"github.com/arduino/arduino-cli/common/formatter/output"
2727
"github.com/spf13/cobra"
@@ -35,8 +35,8 @@ func initListAllCommand() *cobra.Command {
3535
"List all boards that have the support platform installed. You can search\n" +
3636
"for a specific board if you specify the board name",
3737
Example: "" +
38-
" " + commands.AppName + " board listall\n" +
39-
" " + commands.AppName + " board listall zero",
38+
" " + cli.AppName + " board listall\n" +
39+
" " + cli.AppName + " board listall zero",
4040
Args: cobra.ArbitraryArgs,
4141
Run: runListAllCommand,
4242
}
@@ -45,7 +45,7 @@ func initListAllCommand() *cobra.Command {
4545

4646
// runListAllCommand list all installed boards
4747
func runListAllCommand(cmd *cobra.Command, args []string) {
48-
pm := commands.InitPackageManager()
48+
pm := cli.InitPackageManager()
4949

5050
match := func(name string) bool {
5151
name = strings.ToLower(name)

0 commit comments

Comments
 (0)