Skip to content

Commit

Permalink
Made 'board ...' command arguments variables local
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Apr 17, 2023
1 parent 4fcd7d9 commit 7876fd3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
1 change: 1 addition & 0 deletions internal/cli/board/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

func initAttachCommand() *cobra.Command {
var port arguments.Port
var fqbn arguments.Fqbn
attachCommand := &cobra.Command{
Use: fmt.Sprintf("attach [-p <%s>] [-b <%s>] [%s]", tr("port"), tr("FQBN"), tr("sketchPath")),
Short: tr("Attaches a sketch to a board."),
Expand Down
31 changes: 18 additions & 13 deletions internal/cli/board/details.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,19 @@ import (
"github.com/spf13/cobra"
)

var (
showFullDetails bool
listProgrammers bool
fqbn arguments.Fqbn
)

func initDetailsCommand() *cobra.Command {
var showFullDetails bool
var listProgrammers bool
var fqbn arguments.Fqbn
var detailsCommand = &cobra.Command{
Use: fmt.Sprintf("details -b <%s>", tr("FQBN")),
Short: tr("Print details about a board."),
Long: tr("Show information about a board, in particular if the board has options to be specified in the FQBN."),
Example: " " + os.Args[0] + " board details -b arduino:avr:nano",
Args: cobra.NoArgs,
Run: runDetailsCommand,
Run: func(cmd *cobra.Command, args []string) {
runDetailsCommand(fqbn.String(), showFullDetails, listProgrammers)
},
}

fqbn.AddToCommand(detailsCommand)
Expand All @@ -55,27 +54,33 @@ func initDetailsCommand() *cobra.Command {
return detailsCommand
}

func runDetailsCommand(cmd *cobra.Command, args []string) {
func runDetailsCommand(fqbn string, showFullDetails, listProgrammers bool) {
inst := instance.CreateAndInit()

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

res, err := board.Details(context.Background(), &rpc.BoardDetailsRequest{
Instance: inst,
Fqbn: fqbn.String(),
Fqbn: fqbn,
})

if err != nil {
feedback.Fatal(tr("Error getting board details: %v", err), feedback.ErrGeneric)
}

feedback.PrintResult(detailsResult{details: res})
feedback.PrintResult(detailsResult{
details: res,
listProgrammers: listProgrammers,
showFullDetails: showFullDetails,
})
}

// output from this command requires special formatting, let's create a dedicated
// feedback.Result implementation
type detailsResult struct {
details *rpc.BoardDetailsResponse
details *rpc.BoardDetailsResponse
listProgrammers bool
showFullDetails bool
}

func (dr detailsResult) Data() interface{} {
Expand All @@ -85,7 +90,7 @@ func (dr detailsResult) Data() interface{} {
func (dr detailsResult) String() string {
details := dr.details

if listProgrammers {
if dr.listProgrammers {
t := table.New()
t.AddRow(tr("Id"), tr("Programmer name"))
for _, programmer := range details.Programmers {
Expand Down Expand Up @@ -160,7 +165,7 @@ func (dr detailsResult) String() string {
tab.SetColumnWidthMode(1, table.Average)
for _, tool := range details.ToolsDependencies {
tab.AddRow(tr("Required tool:"), tool.Packager+":"+tool.Name, tool.Version)
if showFullDetails {
if dr.showFullDetails {
for _, sys := range tool.Systems {
tab.AddRow("", tr("OS:"), sys.Host)
tab.AddRow("", tr("File:"), sys.ArchiveFilename)
Expand Down
23 changes: 11 additions & 12 deletions internal/cli/board/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,43 +33,42 @@ import (
"github.com/spf13/cobra"
)

var (
timeoutArg arguments.DiscoveryTimeout
watch bool
)

func initListCommand() *cobra.Command {
var timeoutArg arguments.DiscoveryTimeout
var watch bool
var fqbn arguments.Fqbn
listCommand := &cobra.Command{
Use: "list",
Short: tr("List connected boards."),
Long: tr("Detects and displays a list of boards connected to the current computer."),
Example: " " + os.Args[0] + " board list --discovery-timeout 10s",
Args: cobra.NoArgs,
Run: runListCommand,
Run: func(cmd *cobra.Command, args []string) {
runListCommand(watch, timeoutArg.Get().Milliseconds(), fqbn.String())
},
}

timeoutArg.AddToCommand(listCommand)
fqbn.AddToCommand(listCommand)
listCommand.Flags().BoolVarP(&watch, "watch", "w", false, tr("Command keeps running and prints list of connected boards whenever there is a change."))

return listCommand
}

// runListCommand detects and lists the connected arduino boards
func runListCommand(cmd *cobra.Command, args []string) {
func runListCommand(watch bool, timeout int64, fqbn string) {
inst := instance.CreateAndInit()

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

if watch {
watchList(cmd, inst)
watchList(inst)
return
}

ports, discoveryErrors, err := board.List(&rpc.BoardListRequest{
Instance: inst,
Timeout: timeoutArg.Get().Milliseconds(),
Fqbn: fqbn.String(),
Timeout: timeout,
Fqbn: fqbn,
})
var invalidFQBNErr *arduino.InvalidFQBNError
if errors.As(err, &invalidFQBNErr) {
Expand All @@ -84,7 +83,7 @@ func runListCommand(cmd *cobra.Command, args []string) {
feedback.PrintResult(result{ports})
}

func watchList(cmd *cobra.Command, inst *rpc.Instance) {
func watchList(inst *rpc.Instance) {
eventsChan, closeCB, err := board.Watch(&rpc.BoardListWatchRequest{Instance: inst})
if err != nil {
feedback.Fatal(tr("Error detecting boards: %v", err), feedback.ErrNetwork)
Expand Down

0 comments on commit 7876fd3

Please sign in to comment.