Auto-assert setconfig value to predefined struct
License: MIT Signed-off-by: rht <rhtbot@gmail.com>
rht committed
Jul 2, 2015 at 20:49 UTC
541836c4a32c6d6c412762130b1110f9f98084ed
3 files changed
+43
-8
core/commands/config.go
+1
-1
@@ -181,7 +181,7 @@ variable set to your preferred text editor.
181
182
var configReplaceCmd = &cmds.Command{
183
Helptext: cmds.HelpText{
184
- Tagline: "Replaces the config with `file>",
184
+ Tagline: "Replaces the config with <file>",
185
ShortDescription: `
186
Make sure to back up the config file first if neccessary, this operation
187
can't be undone.
docs/fuse.md
+1
-1
@@ -65,7 +65,7 @@ ipfs daemon --mount
65
If you wish to allow other users to use the mount points, use the following:
66
67
```sh
68
-ipfs config Mounts.FuseAllowOther --bool true
68
+ipfs config Mounts.FuseAllowOther true
69
ipfs daemon --mount
70
```
71
repo/fsrepo/fsrepo.go
+41
-6
@@ -495,19 +495,54 @@ func (r *FSRepo) SetConfigKey(key string, value interface{}) error {
495
if err != nil {
496
return err
497
}
498
- switch v := value.(type) {
499
- case string:
500
- if i, err := strconv.Atoi(v); err == nil {
501
- value = i
502
- }
503
- }
498
var mapconf map[string]interface{}
499
if err := serialize.ReadConfigFile(filename, &mapconf); err != nil {
500
return err
501
}
502
+
503
+ // Get the type of the value associated with the key
504
+ oldValue, err := common.MapGetKV(mapconf, key)
505
+ ok := true
506
+ if err != nil {
507
+ // key-value does not exist yet
508
+ switch v := value.(type) {
509
+ case string:
510
+ value, err = strconv.ParseBool(v)
511
+ if err != nil {
512
+ value, err = strconv.Atoi(v)
513
+ if err != nil {
514
+ value, err = strconv.ParseFloat(v, 32)
515
+ if err != nil {
516
+ value = v
517
+ }
518
+ }
519
+ }
520
+ default:
521
+ }
522
+ } else {
523
+ switch oldValue.(type) {
524
+ case bool:
525
+ value, ok = value.(bool)
526
+ case int:
527
+ value, ok = value.(int)
528
+ case float32:
529
+ value, ok = value.(float32)
530
+ case string:
531
+ value, ok = value.(string)
532
+ default:
533
+ value = value
534
+ }
535
+ if !ok {
536
+ return fmt.Errorf("Wrong config type, expected %T", oldValue)
537
+ }
538
+ }
539
+
540
if err := common.MapSetKV(mapconf, key, value); err != nil {
541
return err
542
}
543
+
544
+ // This step doubles as to validate the map against the struct
545
+ // before serialization
546
conf, err := config.FromMap(mapconf)
547
if err != nil {
548
return err