diff --git a/internal/cli/config/get.go b/internal/cli/config/get.go index a0583fd0455..e09c73d1f84 100644 --- a/internal/cli/config/get.go +++ b/internal/cli/config/get.go @@ -16,6 +16,8 @@ package config import ( + "encoding/json" + "fmt" "os" "github.com/arduino/arduino-cli/commands/daemon" @@ -29,11 +31,11 @@ import ( func initGetCommand() *cobra.Command { getCommand := &cobra.Command{ Use: "get", - Short: tr("Gets settings key values."), - Long: tr("Gets settings key values."), + Short: tr("Gets a settings key value."), + Long: tr("Gets a settings key value."), Example: "" + " " + os.Args[0] + " config get logging\n" + - " " + os.Args[0] + " config get logging.level logging.file\n" + + " " + os.Args[0] + " config get daemon.port\n" + " " + os.Args[0] + " config get board_manager.additional_urls", Args: cobra.MinimumNArgs(1), Run: runGetCommand, @@ -53,7 +55,12 @@ func runGetCommand(cmd *cobra.Command, args []string) { if err != nil { feedback.Fatal(tr("Cannot get the key %[1]s: %[2]v", toGet, err), feedback.ErrGeneric) } - feedback.PrintResult(getResult{key: toGet, data: resp.GetJsonData()}) + var result getResult + err = json.Unmarshal([]byte(resp.GetJsonData()), &result.resp) + if err != nil { + feedback.Fatal(tr("Cannot parse JSON for key %[1]s: %[2]v", toGet, err), feedback.ErrGeneric) + } + feedback.PrintResult(result) } } @@ -61,19 +68,13 @@ func runGetCommand(cmd *cobra.Command, args []string) { // create a dedicated feedback.Result implementation to safely handle // any changes to the configuration.Settings struct. type getResult struct { - key string - data interface{} + resp interface{} } func (gr getResult) Data() interface{} { - return gr.data + return gr.resp } func (gr getResult) String() string { - gs, ok := gr.data.(string) - if !ok { - // Should never happen - panic(tr("Cannot get key %s value as string: %v", gr.key, gr.data)) - } - return gs + return fmt.Sprintf("%v", gr.resp) } diff --git a/internal/integrationtest/config/config_test.go b/internal/integrationtest/config/config_test.go index 1d109e09f5a..b8c5036e0da 100644 --- a/internal/integrationtest/config/config_test.go +++ b/internal/integrationtest/config/config_test.go @@ -834,31 +834,17 @@ func TestGet(t *testing.T) { // Get simple key value stdout, _, err = cli.Run("config", "get", "daemon.port", "--format", "json", "--config-file", "arduino-cli.yaml") require.NoError(t, err) - require.Equal(t, `"50051"`, stdout) + requirejson.Contains(t, stdout, `"50051"`) // Get structured key value stdout, _, err = cli.Run("config", "get", "daemon", "--format", "json", "--config-file", "arduino-cli.yaml") require.NoError(t, err) - require.Equal(t, `{"port":"50051"}`, stdout) - - // Get multiple key values - stdout, _, err = cli.Run("config", "get", "logging.format", "logging.level", "--format", "json", "--config-file", "arduino-cli.yaml") - require.NoError(t, err) - require.Equal(t, `"text"`+"\n"+`"info"`, stdout) + requirejson.Contains(t, stdout, `{"port":"50051"}`) // Get undefined key - stdout, _, err = cli.Run("config", "get", "foo", "--format", "json", "--config-file", "arduino-cli.yaml") - require.Empty(t, stdout) - require.Contains(t, err, "Cannot get key foo") - - // Set undefined key - _, _, err = cli.Run("config", "set", "foo", "bar", "--config-file", "arduino-cli.yaml") - require.NoError(t, err) - - // Get previously-undefined key - stdout, _, err = cli.Run("config", "get", "foo", "--format", "json", "--config-file", "arduino-cli.yaml") - require.NoError(t, err) - require.Equal(t, `"bar"`, stdout) + _, stderr, err := cli.Run("config", "get", "foo", "--format", "json", "--config-file", "arduino-cli.yaml") + require.Error(t, err) + requirejson.Contains(t, stderr, `{"error":"Cannot get the key foo: key not found in settings"}`) } func TestInitializationOrderOfConfigThroughFlagAndEnv(t *testing.T) {