Skip to content
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

Assume config value to be json-formatted in cli #1355

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions commands/cli/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ func TestOptionParsing(t *testing.T) {
test("-bs foo", kvs{"b": "", "s": "foo"}, words{})
test("-sb", kvs{"s": "b"}, words{})
test("-b foo", kvs{"b": ""}, words{"foo"})
test("--bool foo", kvs{"bool": ""}, words{"foo"})
testFail("--bool=foo")
testFail("--string")
test("--string foo", kvs{"string": "foo"}, words{})
test("--string=foo", kvs{"string": "foo"}, words{})
Expand Down
25 changes: 9 additions & 16 deletions core/commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,14 @@ Set the value of the 'datastore.path' key:
var output *ConfigField
if len(args) == 2 {
value := args[1]

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)
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so always default to json? that makes sense, but does that mean that we now have to do:

# used to work
> ipfs config Addresses.API /ip4/127.0.0.1/tcp/5001

# must now be
> ipfs config Addresses.API "/ip4/127.0.0.1/tcp/5001"

?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Thu, Jun 11, 2015 at 01:34:07PM -0700, Juan Batiz-Benet wrote:

must now be

ipfs config Addresses.API "/ip4/127.0.0.1/tcp/5001"

Implementations differ in how strictly they enforce this, but I've
seen a number of folks suggest that strings are not valid JSON root
objects (e.g. 1). That's also how it's written up in RFC 4627 2.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ipfs config Addresses.API /ip4/127.0.0.1/tcp/5001 still works in this case (golang/pkg/encoding/json).

So I will have to check which implementation (RFC 4627 vs ECMA-262 5 and newer) is among the majority today.

edit: from here on 'ECMA-262 5' is renamed RFC 7159 https://tools.ietf.org/html/rfc7159#section-2

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ipfs config Addresses.API /ip4/127.0.0.1/tcp/5001 still works

ok great.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

accept stand-alone strings, numbers (RFC 7159):

  • Go 1.3+
  • Major browsers (IE 8+, everything else not IE)[1]
  • Python 2.7+[1]

else (RFC 4627):

  • Ruby 2.1.1 stdlib "json" [2]

Pending:
http://json.org/ (150-ish of those)

[1] http://stackoverflow.com/questions/18202532/can-a-single-stand-alone-literal-form-a-valid-json-document
[2] manual check & "JSON.generate only allows objects or arrays to be converted to JSON syntax." in http://ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html


Or since RFC 7159 supersedes RFC 4627, any legacy system will move toward it anyway.

}

output, err = setConfig(r, key, jsonVal)
} else {
output, err = getConfig(r, key)
}
Expand Down Expand Up @@ -181,7 +174,7 @@ variable set to your preferred text editor.

var configReplaceCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Replaces the config with `file>",
Tagline: "Replaces the config with <file>",
ShortDescription: `
Make sure to back up the config file first if neccessary, this operation
can't be undone.
Expand Down Expand Up @@ -228,7 +221,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 (maybe use --json?)", err)
return nil, fmt.Errorf("Failed to set config value: %s", err)
}
return getConfig(r, key)
}
Expand Down
2 changes: 1 addition & 1 deletion docs/fuse.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ ipfs daemon --mount
If you wish to allow other users to use the mount points, use the following:

```sh
ipfs config Mounts.FuseAllowOther --bool true
ipfs config Mounts.FuseAllowOther true
ipfs daemon --mount
```

Expand Down
4 changes: 2 additions & 2 deletions test/sharness/lib/test-lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ test_wait_open_tcp_port_10_sec() {
# was setting really weird things and am not sure why.
test_config_set() {

# grab flags (like --bool in "ipfs config --bool")
# grab flags (like --json in "ipfs config --json")
test_cfg_flags="" # unset in case.
test "$#" = 3 && { test_cfg_flags=$1; shift; }

Expand Down Expand Up @@ -186,7 +186,7 @@ test_config_ipfs_gateway_writable() {
test_config_ipfs_gateway_readonly $1

test_expect_success "prepare config -- gateway writable" '
test_config_set --bool Gateway.Writable true ||
test_config_set Gateway.Writable true ||
test_fsh cat "\"$IPFS_PATH/config\""
'
}
Expand Down
10 changes: 4 additions & 6 deletions test/sharness/t0021-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test_description="Test config command"
# we use a function so that we can run it both offline + online
test_config_cmd_set() {

# flags (like --bool in "ipfs config --bool")
# flags (like --json in "ipfs config --json")
cfg_flags="" # unset in case.
test "$#" = 3 && { cfg_flags=$1; shift; }

Expand Down Expand Up @@ -52,11 +52,9 @@ test_config_cmd() {
test_config_cmd_set "beep" "boop"
test_config_cmd_set "beep1" "boop2"
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"
test_config_cmd_set "beep2" "true"
test_config_cmd_set "beep2" "false"
test_config_cmd_set "Discovery" "$CONFIG_SET_JSON_TEST"

}

Expand Down