Skip to content

Commit

Permalink
config/git: unmarshal JSON RPC response
Browse files Browse the repository at this point in the history
  • Loading branch information
ardnew committed Jan 25, 2024
1 parent aa6dee6 commit 1522588
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 32 deletions.
27 changes: 14 additions & 13 deletions internal/cli/config/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package config

import (
"encoding/json"
"fmt"
"os"

"github.com/arduino/arduino-cli/commands/daemon"
Expand All @@ -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,
Expand All @@ -53,27 +55,26 @@ 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)
}
}

// output from this command may require special formatting.
// 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)
}
24 changes: 5 additions & 19 deletions internal/integrationtest/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 1522588

Please sign in to comment.