Skip to content

Commit 2dee272

Browse files
committed
Add device list-fqbn command
1 parent d1e5b12 commit 2dee272

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

cli/device/device.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func NewCommand() *cobra.Command {
3737
deviceCommand.AddCommand(initListFrequencyPlansCommand())
3838
deviceCommand.AddCommand(initCreateLoraCommand())
3939
deviceCommand.AddCommand(initCreateGenericCommand())
40+
deviceCommand.AddCommand(initListFQBNCommand())
4041

4142
return deviceCommand
4243
}

cli/device/listfqbn.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// This file is part of arduino-cloud-cli.
2+
//
3+
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published
7+
// by the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
package device
19+
20+
import (
21+
"os"
22+
23+
"github.com/arduino/arduino-cli/cli/errorcodes"
24+
"github.com/arduino/arduino-cli/cli/feedback"
25+
"github.com/arduino/arduino-cli/table"
26+
"github.com/arduino/arduino-cloud-cli/command/device"
27+
"github.com/sirupsen/logrus"
28+
"github.com/spf13/cobra"
29+
)
30+
31+
func initListFQBNCommand() *cobra.Command {
32+
listFQBNCommand := &cobra.Command{
33+
Use: "list-fqbn",
34+
Short: "List supported FQBN",
35+
Long: "List all the FQBN supported by Arduino IoT Cloud",
36+
Run: runListFQBNCommand,
37+
}
38+
return listFQBNCommand
39+
}
40+
41+
func runListFQBNCommand(cmd *cobra.Command, args []string) {
42+
logrus.Info("Listing supported FQBN")
43+
44+
fqbn, err := device.ListFQBN()
45+
if err != nil {
46+
feedback.Errorf("Error during device list-fqbn: %v", err)
47+
os.Exit(errorcodes.ErrGeneric)
48+
}
49+
50+
feedback.PrintResult(listFQBNResult{fqbn})
51+
}
52+
53+
type listFQBNResult struct {
54+
fqbn []device.FQBNInfo
55+
}
56+
57+
func (r listFQBNResult) Data() interface{} {
58+
return r.fqbn
59+
}
60+
61+
func (r listFQBNResult) String() string {
62+
if len(r.fqbn) == 0 {
63+
return "No FQBN."
64+
}
65+
t := table.New()
66+
t.SetHeader("Name", "FQBN")
67+
for _, f := range r.fqbn {
68+
t.AddRow(
69+
f.Name,
70+
f.FQBN,
71+
)
72+
}
73+
return t.Render()
74+
}

command/device/listfqbn.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// This file is part of arduino-cloud-cli.
2+
//
3+
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published
7+
// by the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
package device
19+
20+
import (
21+
"encoding/json"
22+
"fmt"
23+
"io/ioutil"
24+
"net/http"
25+
)
26+
27+
// FrequencyPlanInfo describes a LoRa frequency plan.
28+
type FQBNInfo struct {
29+
Name string `json:"name"`
30+
FQBN string `json:"fqbn"`
31+
}
32+
33+
// ListFQBN command returns a list of the supported FQBN.
34+
func ListFQBN() ([]FQBNInfo, error) {
35+
resp, err := http.Get("https://builder.arduino.cc/v3/boards/")
36+
if err != nil {
37+
return nil, fmt.Errorf("cannot retrieve boards from builder.arduino.cc: %w", err)
38+
}
39+
defer resp.Body.Close()
40+
41+
body, err := ioutil.ReadAll(resp.Body)
42+
if err != nil {
43+
return nil, fmt.Errorf("reading boards from builder.arduino.cc: cannot read response's body: %w", err)
44+
}
45+
46+
var flist struct {
47+
FQBN []FQBNInfo `json:"items"`
48+
}
49+
if err = json.Unmarshal(body, &flist); err != nil {
50+
return nil, fmt.Errorf("cannot parse boards retrieved from builder.arduino.cc: %w", err)
51+
}
52+
return flist.FQBN, nil
53+
}

0 commit comments

Comments
 (0)