|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to [email protected]. |
| 15 | + |
| 16 | +package board |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "os" |
| 21 | + "sort" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "github.com/arduino/arduino-cli/cli/errorcodes" |
| 25 | + "github.com/arduino/arduino-cli/cli/feedback" |
| 26 | + "github.com/arduino/arduino-cli/cli/instance" |
| 27 | + "github.com/arduino/arduino-cli/commands/board" |
| 28 | + rpc "github.com/arduino/arduino-cli/rpc/commands" |
| 29 | + "github.com/arduino/arduino-cli/table" |
| 30 | + "github.com/spf13/cobra" |
| 31 | +) |
| 32 | + |
| 33 | +func initSearchCommand() *cobra.Command { |
| 34 | + var searchCommand = &cobra.Command{ |
| 35 | + Use: "search [boardname]", |
| 36 | + Short: "List all known boards and their corresponding FQBN.", |
| 37 | + Long: "" + |
| 38 | + "List all boards that have the support platform installed. You can search\n" + |
| 39 | + "for a specific board if you specify the board name", |
| 40 | + Example: "" + |
| 41 | + " " + os.Args[0] + " board search\n" + |
| 42 | + " " + os.Args[0] + " board search zero", |
| 43 | + Args: cobra.ArbitraryArgs, |
| 44 | + Run: runSearchCommand, |
| 45 | + } |
| 46 | + searchCommand.Flags().BoolVarP(&searchFlags.showHiddenBoard, "show-hidden", "a", false, "Show also boards marked as 'hidden' in the platform") |
| 47 | + return searchCommand |
| 48 | +} |
| 49 | + |
| 50 | +var searchFlags struct { |
| 51 | + showHiddenBoard bool |
| 52 | +} |
| 53 | + |
| 54 | +func runSearchCommand(cmd *cobra.Command, args []string) { |
| 55 | + inst, err := instance.CreateInstance() |
| 56 | + if err != nil { |
| 57 | + feedback.Errorf("Error searching boards: %v", err) |
| 58 | + os.Exit(errorcodes.ErrGeneric) |
| 59 | + } |
| 60 | + |
| 61 | + res, err := board.Search(context.Background(), &rpc.BoardSearchReq{ |
| 62 | + Instance: inst, |
| 63 | + SearchArgs: strings.Join(args, " "), |
| 64 | + IncludeHiddenBoards: searchFlags.showHiddenBoard, |
| 65 | + }) |
| 66 | + if err != nil { |
| 67 | + feedback.Errorf("Error searching boards: %v", err) |
| 68 | + os.Exit(errorcodes.ErrGeneric) |
| 69 | + } |
| 70 | + |
| 71 | + feedback.PrintResult(searchResults{res.Boards}) |
| 72 | +} |
| 73 | + |
| 74 | +// output from this command requires special formatting so we create a dedicated |
| 75 | +// feedback.Result implementation |
| 76 | +type searchResults struct { |
| 77 | + boards []*rpc.BoardListItem |
| 78 | +} |
| 79 | + |
| 80 | +func (r searchResults) Data() interface{} { |
| 81 | + return r.boards |
| 82 | +} |
| 83 | + |
| 84 | +func (r searchResults) String() string { |
| 85 | + sort.Slice(r.boards, func(i, j int) bool { |
| 86 | + return r.boards[i].GetName() < r.boards[j].GetName() |
| 87 | + }) |
| 88 | + |
| 89 | + t := table.New() |
| 90 | + t.SetHeader("Board Name", "FQBN", "Platform ID", "") |
| 91 | + for _, item := range r.boards { |
| 92 | + hidden := "" |
| 93 | + if item.IsHidden { |
| 94 | + hidden = "(hidden)" |
| 95 | + } |
| 96 | + t.AddRow(item.GetName(), item.GetFQBN(), item.Platform.ID, hidden) |
| 97 | + } |
| 98 | + return t.Render() |
| 99 | +} |
0 commit comments