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