|
| 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 license@arduino.cc. |
| 15 | + |
| 16 | +package config |
| 17 | + |
| 18 | +import ( |
| 19 | + "os" |
| 20 | + "reflect" |
| 21 | + |
| 22 | + "github.com/arduino/arduino-cli/configuration" |
| 23 | + "github.com/arduino/arduino-cli/internal/cli/feedback" |
| 24 | + "github.com/sirupsen/logrus" |
| 25 | + "github.com/spf13/cobra" |
| 26 | + "gopkg.in/yaml.v2" |
| 27 | +) |
| 28 | + |
| 29 | +func initGetCommand() *cobra.Command { |
| 30 | + getCommand := &cobra.Command{ |
| 31 | + Use: "get", |
| 32 | + Short: tr("Gets a setting value."), |
| 33 | + Long: tr("Gets a setting value."), |
| 34 | + Example: "" + |
| 35 | + " " + os.Args[0] + " config get logging.level\n" + |
| 36 | + " " + os.Args[0] + " config get logging.file\n" + |
| 37 | + " " + os.Args[0] + " config get sketch.always_export_binaries\n" + |
| 38 | + " " + os.Args[0] + " config get board_manager.additional_urls", |
| 39 | + Args: cobra.MinimumNArgs(1), |
| 40 | + Run: runGetCommand, |
| 41 | + ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 42 | + return configuration.Settings.AllKeys(), cobra.ShellCompDirectiveDefault |
| 43 | + }, |
| 44 | + } |
| 45 | + return getCommand |
| 46 | +} |
| 47 | + |
| 48 | +func runGetCommand(cmd *cobra.Command, args []string) { |
| 49 | + logrus.Info("Executing `arduino-cli config get`") |
| 50 | + key := args[0] |
| 51 | + kind := validateKey(key) |
| 52 | + |
| 53 | + if kind != reflect.Slice && len(args) > 1 { |
| 54 | + feedback.Fatal(tr("Can't get multiple key values"), feedback.ErrGeneric) |
| 55 | + } |
| 56 | + |
| 57 | + var value interface{} |
| 58 | + switch kind { |
| 59 | + case reflect.Slice: |
| 60 | + value = configuration.Settings.GetStringSlice(key) |
| 61 | + case reflect.String: |
| 62 | + value = configuration.Settings.GetString(key) |
| 63 | + case reflect.Bool: |
| 64 | + value = configuration.Settings.GetBool(key) |
| 65 | + } |
| 66 | + |
| 67 | + feedback.PrintResult(getResult{value}) |
| 68 | +} |
| 69 | + |
| 70 | +// output from this command may require special formatting. |
| 71 | +// create a dedicated feedback.Result implementation to safely handle |
| 72 | +// any changes to the configuration.Settings struct. |
| 73 | +type getResult struct { |
| 74 | + data interface{} |
| 75 | +} |
| 76 | + |
| 77 | +func (gr getResult) Data() interface{} { |
| 78 | + return gr.data |
| 79 | +} |
| 80 | + |
| 81 | +func (gr getResult) String() string { |
| 82 | + gs, err := yaml.Marshal(gr.data) |
| 83 | + if err != nil { |
| 84 | + // Should never happen |
| 85 | + panic(tr("unable to marshal config to YAML: %v", err)) |
| 86 | + } |
| 87 | + return string(gs) |
| 88 | +} |
0 commit comments