|
| 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 config |
| 17 | + |
| 18 | +import ( |
| 19 | + "encoding/json" |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + |
| 23 | + "github.com/arduino/arduino-cli/commands/daemon" |
| 24 | + "github.com/arduino/arduino-cli/internal/cli/configuration" |
| 25 | + "github.com/arduino/arduino-cli/internal/cli/feedback" |
| 26 | + rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" |
| 27 | + "github.com/sirupsen/logrus" |
| 28 | + "github.com/spf13/cobra" |
| 29 | + "gopkg.in/yaml.v3" |
| 30 | +) |
| 31 | + |
| 32 | +func initGetCommand() *cobra.Command { |
| 33 | + getCommand := &cobra.Command{ |
| 34 | + Use: "get", |
| 35 | + Short: tr("Gets a settings key value."), |
| 36 | + Long: tr("Gets a settings key value."), |
| 37 | + Example: "" + |
| 38 | + " " + os.Args[0] + " config get logging\n" + |
| 39 | + " " + os.Args[0] + " config get daemon.port\n" + |
| 40 | + " " + os.Args[0] + " config get board_manager.additional_urls", |
| 41 | + Args: cobra.MinimumNArgs(1), |
| 42 | + Run: runGetCommand, |
| 43 | + ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 44 | + return configuration.Settings.AllKeys(), cobra.ShellCompDirectiveDefault |
| 45 | + }, |
| 46 | + } |
| 47 | + return getCommand |
| 48 | +} |
| 49 | + |
| 50 | +func runGetCommand(cmd *cobra.Command, args []string) { |
| 51 | + logrus.Info("Executing `arduino-cli config get`") |
| 52 | + |
| 53 | + svc := daemon.ArduinoCoreServerImpl{} |
| 54 | + for _, toGet := range args { |
| 55 | + resp, err := svc.SettingsGetValue(cmd.Context(), &rpc.SettingsGetValueRequest{Key: toGet}) |
| 56 | + if err != nil { |
| 57 | + feedback.Fatal(tr("Cannot get the configuration key %[1]s: %[2]v", toGet, err), feedback.ErrGeneric) |
| 58 | + } |
| 59 | + var result getResult |
| 60 | + err = json.Unmarshal([]byte(resp.GetJsonData()), &result.resp) |
| 61 | + if err != nil { |
| 62 | + // Should never happen... |
| 63 | + panic(fmt.Sprintf("Cannot parse JSON for key %[1]s: %[2]v", toGet, err)) |
| 64 | + } |
| 65 | + feedback.PrintResult(result) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// output from this command may require special formatting. |
| 70 | +// create a dedicated feedback.Result implementation to safely handle |
| 71 | +// any changes to the configuration.Settings struct. |
| 72 | +type getResult struct { |
| 73 | + resp interface{} |
| 74 | +} |
| 75 | + |
| 76 | +func (gr getResult) Data() interface{} { |
| 77 | + return gr.resp |
| 78 | +} |
| 79 | + |
| 80 | +func (gr getResult) String() string { |
| 81 | + gs, err := yaml.Marshal(gr.resp) |
| 82 | + if err != nil { |
| 83 | + // Should never happen |
| 84 | + panic(tr("unable to marshal config to YAML: %v", err)) |
| 85 | + } |
| 86 | + return string(gs) |
| 87 | +} |
0 commit comments