8000 add "config get" command to print settings values by ardnew · Pull Request #2307 · arduino/arduino-cli · GitHub
[go: up one dir, main page]

Skip to content

add "config get" command to print settings values #2307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Feb 14, 2024
Prev Previous commit
Next Next commit
config/get: unmarshal JSON RPC response
  • Loading branch information
ardnew committed Feb 8, 2024
commit d67f3d7689737dd10dc184ebdc8e3e90d5893108
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