mirror of
https://github.com/ipfs/kubo.git
synced 2025-09-10 14:34:24 +08:00
move utility method
This commit is contained in:
@ -2,9 +2,11 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@ -205,3 +207,15 @@ func Marshal(value interface{}) ([]byte, error) {
|
|||||||
// need to prettyprint, hence MarshalIndent, instead of Encoder
|
// need to prettyprint, hence MarshalIndent, instead of Encoder
|
||||||
return json.MarshalIndent(value, "", " ")
|
return json.MarshalIndent(value, "", " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func FromMap(v map[string]interface{}) (*Config, error) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
if err := json.NewEncoder(&buf).Encode(v); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var conf Config
|
||||||
|
if err := json.NewDecoder(&buf).Decode(&conf); err != nil {
|
||||||
|
return nil, fmt.Errorf("Failure to decode config: %s", err)
|
||||||
|
}
|
||||||
|
return &conf, nil
|
||||||
|
}
|
||||||
|
@ -149,7 +149,7 @@ func (r *FSRepo) SetConfigKey(key string, value interface{}) error {
|
|||||||
if err := writeConfigFile(filename, mapconf); err != nil {
|
if err := writeConfigFile(filename, mapconf); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
conf, err := convertMapToConfig(mapconf)
|
conf, err := config.FromMap(mapconf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package fsrepo
|
package fsrepo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@ -73,18 +72,6 @@ func encode(w io.Writer, value interface{}) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func convertMapToConfig(v map[string]interface{}) (*config.Config, error) {
|
|
||||||
var buf bytes.Buffer
|
|
||||||
if err := json.NewEncoder(&buf).Encode(v); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var conf config.Config
|
|
||||||
if err := json.NewDecoder(&buf).Decode(&conf); err != nil {
|
|
||||||
return nil, fmt.Errorf("Failure to decode config: %s", err)
|
|
||||||
}
|
|
||||||
return &conf, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// load reads given file and returns the read config, or error.
|
// load reads given file and returns the read config, or error.
|
||||||
func load(filename string) (*config.Config, error) {
|
func load(filename string) (*config.Config, error) {
|
||||||
// if nothing is there, fail. User must run 'ipfs init'
|
// if nothing is there, fail. User must run 'ipfs init'
|
||||||
|
Reference in New Issue
Block a user