github.com/robgonnella/ardi/v2@v2.4.5-0.20230102052001-11a49de978c3/core/board.go (about)

     1  package core
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"sort"
     7  	"text/tabwriter"
     8  
     9  	"github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
    10  	log "github.com/sirupsen/logrus"
    11  
    12  	cli "github.com/robgonnella/ardi/v2/cli-wrapper"
    13  )
    14  
    15  // BoardCore module for board commands
    16  type BoardCore struct {
    17  	cli    *cli.Wrapper
    18  	logger *log.Logger
    19  }
    20  
    21  // BoardCoreOption represents options for the BoardCore
    22  type BoardCoreOption = func(c *BoardCore)
    23  
    24  // NewBoardCore module instance for board commands
    25  func NewBoardCore(logger *log.Logger, options ...BoardCoreOption) *BoardCore {
    26  	c := &BoardCore{
    27  		logger: logger,
    28  	}
    29  
    30  	for _, o := range options {
    31  		o(c)
    32  	}
    33  
    34  	return c
    35  }
    36  
    37  // WithBoardCliWrapper allows an injectable cli wrapper
    38  func WithBoardCliWrapper(wrapper *cli.Wrapper) BoardCoreOption {
    39  	return func(c *BoardCore) {
    40  		c.cli = wrapper
    41  	}
    42  }
    43  
    44  // FQBNS lists all available boards with associated fqbns
    45  func (c *BoardCore) FQBNS(query string) error {
    46  	platforms, err := c.cli.SearchPlatforms()
    47  
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	var boardList []*commands.Board
    53  
    54  	for _, plat := range platforms {
    55  		for _, board := range plat.GetBoards() {
    56  			if board.GetFqbn() != "" {
    57  				boardList = append(boardList, board)
    58  			}
    59  		}
    60  	}
    61  
    62  	if len(boardList) == 0 {
    63  		err := errors.New("you must install platforms with 'ardi add platform'")
    64  		return err
    65  	}
    66  
    67  	sort.Slice(boardList, func(i, j int) bool {
    68  		return boardList[i].GetName() < boardList[j].GetName()
    69  	})
    70  
    71  	w := tabwriter.NewWriter(c.logger.Out, 0, 0, 8, ' ', 0)
    72  	defer w.Flush()
    73  	w.Write([]byte("Board\tFQBN\n"))
    74  	for _, board := range boardList {
    75  		w.Write([]byte(fmt.Sprintf("%s\t%s\n", board.GetName(), board.GetFqbn())))
    76  	}
    77  	return nil
    78  }
    79  
    80  // Platforms lists all available boards with associated platorms
    81  func (c *BoardCore) Platforms(query string) error {
    82  	platforms, err := c.cli.SearchPlatforms()
    83  
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	type boardAndPlat struct {
    89  		boardName string
    90  		platform  string
    91  	}
    92  
    93  	var boardList []boardAndPlat
    94  	for _, plat := range platforms {
    95  		for _, board := range plat.GetBoards() {
    96  			boardList = append(
    97  				boardList,
    98  				boardAndPlat{
    99  					boardName: board.GetName(),
   100  					platform:  plat.GetId(),
   101  				},
   102  			)
   103  		}
   104  	}
   105  
   106  	sort.Slice(boardList, func(i, j int) bool {
   107  		return boardList[i].boardName < boardList[j].boardName
   108  	})
   109  
   110  	w := tabwriter.NewWriter(c.logger.Out, 0, 0, 8, ' ', 0)
   111  	defer w.Flush()
   112  	w.Write([]byte("Board\tPlatform\n"))
   113  	for _, board := range boardList {
   114  		w.Write([]byte(fmt.Sprintf("%s\t%s\n", board.boardName, board.platform)))
   115  	}
   116  
   117  	return nil
   118  }