|
| 1 | +/* |
| 2 | + * This file is part of arduino-cli. |
| 3 | + * |
| 4 | + * Copyright 2018 ARDUINO SA (http://www.arduino.cc/) |
| 5 | + * |
| 6 | + * This software is released under the GNU General Public License version 3, |
| 7 | + * which covers the main part of arduino-cli. |
| 8 | + * The terms of this license can be found at: |
| 9 | + * https://www.gnu.org/licenses/gpl-3.0.en.html |
| 10 | + * |
| 11 | + * You can be released from the requirements of the above licenses by purchasing |
| 12 | + * a commercial license. Buying such a license is mandatory if you want to modify or |
| 13 | + * otherwise use the software for commercial activities involving the Arduino |
| 14 | + * software without disclosing the source code of your own applications. To purchase |
| 15 | + * a commercial license, send an email to [email protected]. |
| 16 | + */ |
| 17 | + |
| 18 | +package board |
| 19 | + |
| 20 | +import ( |
| 21 | + "encoding/json" |
| 22 | + "os" |
| 23 | + |
| 24 | + "github.com/arduino/arduino-cli/output" |
| 25 | + |
| 26 | + "github.com/arduino/arduino-cli/commands" |
| 27 | + "github.com/arduino/arduino-cli/common/formatter" |
| 28 | + "github.com/spf13/cobra" |
| 29 | +) |
| 30 | + |
| 31 | +func initDetailsCommand() *cobra.Command { |
| 32 | + detailsCommand := &cobra.Command{ |
| 33 | + Use: "details <FQBN>", |
| 34 | + Short: "Print details about a board.", |
| 35 | + Long: "Show information about a board, in particular if the board has options to be specified in the FQBN.", |
| 36 | + Example: " " + commands.AppName + " board details arduino:avr:nano", |
| 37 | + Args: cobra.ExactArgs(1), |
| 38 | + Run: runDetailsCommand, |
| 39 | + } |
| 40 | + return detailsCommand |
| 41 | +} |
| 42 | + |
| 43 | +func runDetailsCommand(cmd *cobra.Command, args []string) { |
| 44 | + pm := commands.InitPackageManager() |
| 45 | + fqbnIn := args[0] |
| 46 | + board, err := pm.FindBoardWithFQBN(fqbnIn) |
| 47 | + if err != nil { |
| 48 | + formatter.PrintError(err, "Error loading board data") |
| 49 | + os.Exit(commands.ErrBadArgument) |
| 50 | + } |
| 51 | + |
| 52 | + details := &boardDetails{} |
| 53 | + details.Name = board.Name() |
| 54 | + details.ConfigOptions = []*boardConfigOption{} |
| 55 | + options := board.GetConfigOptions() |
| 56 | + for _, option := range options.Keys() { |
| 57 | + configOption := &boardConfigOption{} |
| 58 | + configOption.Option = option |
| 59 | + configOption.OptionLabel = options.Get(option) |
| 60 | + values := board.GetConfigOptionValues(option) |
| 61 | + for i, value := range values.Keys() { |
| 62 | + configValue := &boardConfigValue{} |
| 63 | + if i == 0 { |
| 64 | + t := true |
| 65 | + configValue.Default = &t |
| 66 | + } |
| 67 | + configValue.Value = value |
| 68 | + configValue.ValueLabel = values.Get(value) |
| 69 | + configOption.Values = append(configOption.Values, configValue) |
| 70 | + } |
| 71 | + |
| 72 | + details.ConfigOptions = append(details.ConfigOptions, configOption) |
| 73 | + } |
| 74 | + |
| 75 | + output.Emit(details) |
| 76 | +} |
| 77 | + |
| 78 | +type boardDetails struct { |
| 79 | + Name string |
| 80 | + ConfigOptions []*boardConfigOption |
| 81 | +} |
| 82 | + |
| 83 | +type boardConfigOption struct { |
| 84 | + Option string |
| 85 | + OptionLabel string |
| 86 | + Values []*boardConfigValue |
| 87 | +} |
| 88 | + |
| 89 | +type boardConfigValue struct { |
| 90 | + Value string |
| 91 | + ValueLabel string |
| 92 | + Default *bool `json:",omitempty"` |
| 93 | +} |
| 94 | + |
| 95 | +func (details *boardDetails) EmitJSON() string { |
| 96 | + d, err := json.MarshalIndent(details, "", " ") |
| 97 | + if err != nil { |
| 98 | + formatter.PrintError(err, "Error encoding json") |
| 99 | + os.Exit(commands.ErrGeneric) |
| 100 | + } |
| 101 | + return string(d) |
| 102 | +} |
| 103 | + |
| 104 | +func (details *boardDetails) EmitTerminal() string { |
| 105 | + table := output.NewTable() |
| 106 | + table.AddRow("Board name:", output.Red(details.Name)) |
| 107 | + table.SetColumnWidthMode(1, output.Average) |
| 108 | + for _, option := range details.ConfigOptions { |
| 109 | + table.AddRow("Option:", option.OptionLabel) |
| 110 | + for _, value := range option.Values { |
| 111 | + table.AddRow("", value.ValueLabel, option.Option+"="+value.Value) |
| 112 | + } |
| 113 | + } |
| 114 | + return table.Render() |
| 115 | +} |
0 commit comments