Commit 6dd8aeb0 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

config set: allow arbitrary json input

This commit allows arbitrary json input to set.
It also tests this with sharness.
parent d6fc414b
......@@ -59,6 +59,7 @@ Set the value of the 'datastore.path' key:
},
Options: []cmds.Option{
cmds.BoolOption("bool", "Set a boolean value"),
cmds.BoolOption("json", "Parse stringified JSON"),
},
Run: func(req cmds.Request, res cmds.Response) {
args := req.Arguments()
......@@ -74,7 +75,17 @@ Set the value of the 'datastore.path' key:
var output *ConfigField
if len(args) == 2 {
value := args[1]
if isbool, _, _ := req.Option("bool").Bool(); isbool {
if parseJson, _, _ := req.Option("json").Bool(); parseJson {
var jsonVal interface{}
if err := json.Unmarshal([]byte(value), &jsonVal); err != nil {
err = fmt.Errorf("failed to unmarshal json. %s", err)
res.SetError(err, cmds.ErrNormal)
return
}
output, err = setConfig(r, key, jsonVal)
} else if isbool, _, _ := req.Option("bool").Bool(); isbool {
output, err = setConfig(r, key, value == "true")
} else {
output, err = setConfig(r, key, value)
......@@ -217,7 +228,7 @@ func getConfig(r repo.Repo, key string) (*ConfigField, error) {
func setConfig(r repo.Repo, key string, value interface{}) (*ConfigField, error) {
err := r.SetConfigKey(key, value)
if err != nil {
return nil, fmt.Errorf("Failed to set config value: %s", err)
return nil, fmt.Errorf("Failed to set config value: %s (maybe use --json?)", err)
}
return getConfig(r, key)
}
......
......@@ -36,6 +36,17 @@ test_config_cmd_set() {
'
}
# this is a bit brittle. the problem is we need to test
# with something that will be forced to unmarshal as a struct.
# (i.e. just setting 'ipfs config --json foo "[1, 2, 3]"') may
# set it as astring instead of proper json. We leverage the
# unmarshalling that has to happen.
CONFIG_SET_JSON_TEST='{
"MDNS": {
"Enabled": true,
"Interval": 10
}
}'
test_config_cmd() {
test_config_cmd_set "beep" "boop"
......@@ -43,6 +54,9 @@ test_config_cmd() {
test_config_cmd_set "beep1" "boop2"
test_config_cmd_set "--bool" "beep2" "true"
test_config_cmd_set "--bool" "beep2" "false"
test_config_cmd_set "--json" "beep3" "true"
test_config_cmd_set "--json" "beep3" "false"
test_config_cmd_set "--json" "Discovery" "$CONFIG_SET_JSON_TEST"
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment