Skip to content

Commit 8314f87

Browse files
ardnewcmaglie
andauthored
Add config get command to print settings values (#2307)
* add "config get" command to print settings values * use RPC for "config get" and add test cases * update imports for changed internal layout * config/get: do not wrap JSON output in YAML * fix formatting with gofmt * config/get: unmarshal JSON RPC response * Apply suggestions from code review Co-authored-by: Cristian Maglie <[email protected]> * use same default format as "config dump" * fix import missing after merging review changes remotely * test (config): fix expected error message with "get <unknown-key>" --------- Co-authored-by: Cristian Maglie <[email protected]>
1 parent bd10a5c commit 8314f87

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

Diff for: internal/cli/config/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func NewCommand() *cobra.Command {
3737
configCommand.AddCommand(initAddCommand())
3838
configCommand.AddCommand(initDeleteCommand())
3939
configCommand.AddCommand(initDumpCommand())
40+
configCommand.AddCommand(initGetCommand())
4041
configCommand.AddCommand(initInitCommand())
4142
configCommand.AddCommand(initRemoveCommand())
4243
configCommand.AddCommand(initSetCommand())

Diff for: internal/cli/config/get.go

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
}

Diff for: internal/integrationtest/config/config_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,35 @@ func TestDelete(t *testing.T) {
818818
require.NotContains(t, configLines, "board_manager")
819819
}
820820

821+
func TestGet(t *testing.T) {
822+
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
823+
defer env.CleanUp()
824+
825+
// Create a config file
826+
_, _, err := cli.Run("config", "init", "--dest-dir", ".")
827+
require.NoError(t, err)
828+
829+
// Verifies default state
830+
stdout, _, err := cli.Run("config", "dump", "--format", "json", "--config-file", "arduino-cli.yaml")
831+
require.NoError(t, err)
832+
requirejson.Query(t, stdout, ".config | .daemon | .port", `"50051"`)
833+
834+
// Get simple key value
835+
stdout, _, err = cli.Run("config", "get", "daemon.port", "--format", "json", "--config-file", "arduino-cli.yaml")
836+
require.NoError(t, err)
837+
requirejson.Contains(t, stdout, `"50051"`)
838+
839+
// Get structured key value
840+
stdout, _, err = cli.Run("config", "get", "daemon", "--format", "json", "--config-file", "arduino-cli.yaml")
841+
require.NoError(t, err)
842+
requirejson.Contains(t, stdout, `{"port":"50051"}`)
843+
844+
// Get undefined key
845+
_, stderr, err := cli.Run("config", "get", "foo", "--format", "json", "--config-file", "arduino-cli.yaml")
846+
require.Error(t, err)
847+
requirejson.Contains(t, stderr, `{"error":"Cannot get the configuration key foo: key not found in settings"}`)
848+
}
849+
821850
func TestInitializationOrderOfConfigThroughFlagAndEnv(t *testing.T) {
822851
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
823852
defer env.CleanUp()

0 commit comments

Comments
 (0)