Skip to content

Rearrange cli commands #272

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 8 commits into from
Jul 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 9 additions & 7 deletions cli/board/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import (
"context"
"os"

"github.com/arduino/arduino-cli/cli"
"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/instance"
"github.com/arduino/arduino-cli/cli/output"
"github.com/arduino/arduino-cli/commands/board"
"github.com/arduino/arduino-cli/common/formatter"
rpc "github.com/arduino/arduino-cli/rpc/commands"
Expand All @@ -33,9 +35,9 @@ func initAttachCommand() *cobra.Command {
Use: "attach <port>|<FQBN> [sketchPath]",
Short: "Attaches a sketch to a board.",
Long: "Attaches a sketch to a board.",
Example: " " + cli.VersionInfo.Application + " board attach serial:///dev/tty/ACM0\n" +
" " + cli.VersionInfo.Application + " board attach serial:///dev/tty/ACM0 HelloWorld\n" +
" " + cli.VersionInfo.Application + " board attach arduino:samd:mkr1000",
Example: " " + os.Args[0] + " board attach serial:///dev/tty/ACM0\n" +
" " + os.Args[0] + " board attach serial:///dev/tty/ACM0 HelloWorld\n" +
" " + os.Args[0] + " board attach arduino:samd:mkr1000",
Args: cobra.RangeArgs(1, 2),
Run: runAttachCommand,
}
Expand All @@ -49,7 +51,7 @@ var attachFlags struct {
}

func runAttachCommand(cmd *cobra.Command, args []string) {
instance := cli.CreateInstance()
instance := instance.CreateInstance()
var path string
if len(args) > 0 {
path = args[1]
Expand All @@ -59,9 +61,9 @@ func runAttachCommand(cmd *cobra.Command, args []string) {
BoardUri: args[0],
SketchPath: path,
SearchTimeout: attachFlags.searchTimeout,
}, cli.OutputTaskProgress())
}, output.TaskProgress())
if err != nil {
formatter.PrintError(err, "attach board error")
os.Exit(cli.ErrGeneric)
os.Exit(errorcodes.ErrGeneric)
}
}
17 changes: 10 additions & 7 deletions cli/board/board.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,27 @@
package board

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

"github.com/spf13/cobra"
)

// InitCommand prepares the command.
func InitCommand() *cobra.Command {
// NewCommand created a new `board` command
func NewCommand() *cobra.Command {
boardCommand := &cobra.Command{
Use: "board",
Short: "Arduino board commands.",
Long: "Arduino board commands.",
Example: " # Lists all connected boards.\n" +
" " + cli.VersionInfo.Application + " board list\n\n" +
" " + os.Args[0] + " board list\n\n" +
" # Attaches a sketch to a board.\n" +
" " + cli.VersionInfo.Application + " board attach serial:///dev/tty/ACM0 mySketch",
" " + os.Args[0] + " board attach serial:///dev/tty/ACM0 mySketch",
}

boardCommand.AddCommand(initAttachCommand())
boardCommand.AddCommand(initDetailsCommand())
boardCommand.AddCommand(detailsCommand)
boardCommand.AddCommand(initListCommand())
boardCommand.AddCommand(initListAllCommand())
boardCommand.AddCommand(listAllCommand)

return boardCommand
}
30 changes: 13 additions & 17 deletions cli/board/details.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,35 @@ import (
"fmt"
"os"

"github.com/arduino/arduino-cli/cli"
"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/instance"
"github.com/arduino/arduino-cli/cli/output"
"github.com/arduino/arduino-cli/commands/board"
"github.com/arduino/arduino-cli/common/formatter"
"github.com/arduino/arduino-cli/output"
rpc "github.com/arduino/arduino-cli/rpc/commands"
"github.com/spf13/cobra"
)

func initDetailsCommand() *cobra.Command {
detailsCommand := &cobra.Command{
Use: "details <FQBN>",
Short: "Print details about a board.",
Long: "Show information about a board, in particular if the board has options to be specified in the FQBN.",
Example: " " + cli.VersionInfo.Application + " board details arduino:avr:nano",
Args: cobra.ExactArgs(1),
Run: runDetailsCommand,
}
return detailsCommand
var detailsCommand = &cobra.Command{
Use: "details <FQBN>",
Short: "Print details about a board.",
Long: "Show information about a board, in particular if the board has options to be specified in the FQBN.",
Example: " " + os.Args[0] + " board details arduino:avr:nano",
Args: cobra.ExactArgs(1),
Run: runDetailsCommand,
}

func runDetailsCommand(cmd *cobra.Command, args []string) {
instance := cli.CreateInstance()

res, err := board.Details(context.Background(), &rpc.BoardDetailsReq{
Instance: instance,
Instance: instance.CreateInstance(),
Fqbn: args[0],
})

if err != nil {
formatter.PrintError(err, "Error getting board details")
os.Exit(cli.ErrGeneric)
os.Exit(errorcodes.ErrGeneric)
}
if cli.OutputJSONOrElse(res) {
if output.JSONOrElse(res) {
outputDetailsResp(res)
}
}
Expand Down
17 changes: 8 additions & 9 deletions cli/board/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ import (
"sort"
"time"

"github.com/arduino/arduino-cli/cli"
"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/instance"
"github.com/arduino/arduino-cli/cli/output"
"github.com/arduino/arduino-cli/commands/board"
"github.com/arduino/arduino-cli/common/formatter"
"github.com/arduino/arduino-cli/output"
rpc "github.com/arduino/arduino-cli/rpc/commands"
"github.com/spf13/cobra"
)
Expand All @@ -37,7 +38,7 @@ func initListCommand() *cobra.Command {
Use: "list",
Short: "List connected boards.",
Long: "Detects and displays a list of connected boards to the current computer.",
Example: " " + cli.VersionInfo.Application + " board list --timeout 10s",
Example: " " + os.Args[0] + " board list --timeout 10s",
Args: cobra.NoArgs,
Run: runListCommand,
}
Expand All @@ -53,22 +54,20 @@ var listFlags struct {

// runListCommand detects and lists the connected arduino boards
func runListCommand(cmd *cobra.Command, args []string) {
instance := cli.CreateInstance()

if timeout, err := time.ParseDuration(listFlags.timeout); err != nil {
formatter.PrintError(err, "Invalid timeout.")
os.Exit(cli.ErrBadArgument)
os.Exit(errorcodes.ErrBadArgument)
} else {
time.Sleep(timeout)
}

resp, err := board.List(context.Background(), &rpc.BoardListReq{Instance: instance})
resp, err := board.List(context.Background(), &rpc.BoardListReq{Instance: instance.CreateInstance()})
if err != nil {
formatter.PrintError(err, "Error detecting boards")
os.Exit(cli.ErrNetwork)
os.Exit(errorcodes.ErrNetwork)
}

if cli.OutputJSONOrElse(resp) {
if output.JSONOrElse(resp) {
outputListResp(resp)
}
}
Expand Down
37 changes: 18 additions & 19 deletions cli/board/listall.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,42 @@ import (
"os"
"sort"

"github.com/arduino/arduino-cli/cli"
"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/instance"
"github.com/arduino/arduino-cli/cli/output"
"github.com/arduino/arduino-cli/commands/board"
"github.com/arduino/arduino-cli/common/formatter"
"github.com/arduino/arduino-cli/output"
rpc "github.com/arduino/arduino-cli/rpc/commands"
"github.com/spf13/cobra"
)

func initListAllCommand() *cobra.Command {
listAllCommand := &cobra.Command{
Use: "listall [boardname]",
Short: "List all known boards and their corresponding FQBN.",
Long: "" +
"List all boards that have the support platform installed. You can search\n" +
"for a specific board if you specify the board name",
Example: "" +
" " + cli.VersionInfo.Application + " board listall\n" +
" " + cli.VersionInfo.Application + " board listall zero",
Args: cobra.ArbitraryArgs,
Run: runListAllCommand,
}
return listAllCommand
var listAllCommand = &cobra.Command{
Use: "listall [boardname]",
Short: "List all known boards and their corresponding FQBN.",
Long: "" +
"List all boards that have the support platform installed. You can search\n" +
"for a specific board if you specify the board name",
Example: "" +
" " + os.Args[0] + " board listall\n" +
" " + os.Args[0] + " board listall zero",
Args: cobra.ArbitraryArgs,
Run: runListAllCommand,
}

// runListAllCommand list all installed boards
func runListAllCommand(cmd *cobra.Command, args []string) {
instance := cli.CreateInstance()
instance := instance.CreateInstance()

list, err := board.ListAll(context.Background(), &rpc.BoardListAllReq{
Instance: instance,
SearchArgs: args,
})
if err != nil {
formatter.PrintError(err, "Error listing boards")
os.Exit(cli.ErrGeneric)
os.Exit(errorcodes.ErrGeneric)
}
if cli.OutputJSONOrElse(list) {

if output.JSONOrElse(list) {
outputBoardListAll(list)
}
}
Expand Down
Loading