Skip to content
This repository was archived by the owner on Mar 9, 2022. It is now read-only.

Commit 4e8fcf8

Browse files
committed
move serialize package to config
License: MIT Signed-off-by: Łukasz Magiera <[email protected]>
1 parent 3a59d79 commit 4e8fcf8

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

serialize/serialize.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package fsrepo
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
"io"
8+
"os"
9+
"path/filepath"
10+
11+
"github.com/ipfs/go-ipfs/repo/config"
12+
13+
"gx/ipfs/QmPdKqUcHGFdeSpvjVoaTRPPstGif9GBZb5Q56RVw9o69A/go-ipfs-util"
14+
"gx/ipfs/QmdYwCmx8pZRkzdcd8MhmLJqYVoVTC1aGsy5Q4reMGLNLg/atomicfile"
15+
)
16+
17+
// ReadConfigFile reads the config from `filename` into `cfg`.
18+
func ReadConfigFile(filename string, cfg interface{}) error {
19+
f, err := os.Open(filename)
20+
if err != nil {
21+
return err
22+
}
23+
defer f.Close()
24+
if err := json.NewDecoder(f).Decode(cfg); err != nil {
25+
return fmt.Errorf("failure to decode config: %s", err)
26+
}
27+
return nil
28+
}
29+
30+
// WriteConfigFile writes the config from `cfg` into `filename`.
31+
func WriteConfigFile(filename string, cfg interface{}) error {
32+
err := os.MkdirAll(filepath.Dir(filename), 0775)
33+
if err != nil {
34+
return err
35+
}
36+
37+
f, err := atomicfile.New(filename, 0660)
38+
if err != nil {
39+
return err
40+
}
41+
defer f.Close()
42+
43+
return encode(f, cfg)
44+
}
45+
46+
// encode configuration with JSON
47+
func encode(w io.Writer, value interface{}) error {
48+
// need to prettyprint, hence MarshalIndent, instead of Encoder
49+
buf, err := config.Marshal(value)
50+
if err != nil {
51+
return err
52+
}
53+
_, err = w.Write(buf)
54+
return err
55+
}
56+
57+
// Load reads given file and returns the read config, or error.
58+
func Load(filename string) (*config.Config, error) {
59+
// if nothing is there, fail. User must run 'ipfs init'
60+
if !util.FileExists(filename) {
61+
return nil, errors.New("ipfs not initialized, please run 'ipfs init'")
62+
}
63+
64+
var cfg config.Config
65+
err := ReadConfigFile(filename, &cfg)
66+
if err != nil {
67+
return nil, err
68+
}
69+
70+
return &cfg, err
71+
}

serialize/serialize_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package fsrepo
2+
3+
import (
4+
"os"
5+
"runtime"
6+
"testing"
7+
8+
config "github.com/ipfs/go-ipfs/repo/config"
9+
)
10+
11+
func TestConfig(t *testing.T) {
12+
const filename = ".ipfsconfig"
13+
cfgWritten := new(config.Config)
14+
cfgWritten.Identity.PeerID = "faketest"
15+
16+
err := WriteConfigFile(filename, cfgWritten)
17+
if err != nil {
18+
t.Fatal(err)
19+
}
20+
cfgRead, err := Load(filename)
21+
if err != nil {
22+
t.Fatal(err)
23+
}
24+
if cfgWritten.Identity.PeerID != cfgRead.Identity.PeerID {
25+
t.Fatal()
26+
}
27+
st, err := os.Stat(filename)
28+
if err != nil {
29+
t.Fatalf("cannot stat config file: %v", err)
30+
}
31+
32+
if runtime.GOOS != "windows" { // see https://golang.org/src/os/types_windows.go
33+
if g := st.Mode().Perm(); g&0117 != 0 {
34+
t.Fatalf("config file should not be executable or accessible to world: %v", g)
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)