Skip to content

Commit

Permalink
feat(node/config): provide functions to write/read Config
Browse files Browse the repository at this point in the history
  • Loading branch information
Wondertan committed Sep 21, 2021
1 parent 68fe49d commit 923f6cd
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
3 changes: 2 additions & 1 deletion go.mod
Expand Up @@ -18,7 +18,8 @@ require (
github.com/libp2p/go-libp2p-routing-helpers v0.2.3
github.com/multiformats/go-base32 v0.0.4
github.com/multiformats/go-multiaddr v0.4.0
github.com/stretchr/testify v1.7.0
github.com/pelletier/go-toml/v2 v2.0.0-beta.3
github.com/stretchr/testify v1.7.1-0.20210427113832-6241f9ab9942
go.uber.org/fx v1.14.2
go.uber.org/zap v1.19.0
)
6 changes: 5 additions & 1 deletion go.sum
Expand Up @@ -969,7 +969,10 @@ github.com/pact-foundation/pact-go v1.0.4/go.mod h1:uExwJY4kCzNPcHRj+hCR/HBbOOIw
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pelletier/go-toml v1.9.3 h1:zeC5b1GviRUyKYd6OJPvBU/mcVDVoL1OhT17FCt5dSQ=
github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pelletier/go-toml/v2 v2.0.0-beta.3 h1:PNCTU4naEJ8mKal97P3A2qDU74QRQGlv4FXiL1XDqi4=
github.com/pelletier/go-toml/v2 v2.0.0-beta.3/go.mod h1:aNseLYu/uKskg0zpr/kbr2z8yGuWtotWf/0BpGIAL2Y=
github.com/performancecopilot/speed v3.0.0+incompatible/go.mod h1:/CLtqpZ5gBg1M9iaPbIdPPGyKcA8hKdoy6hAWba7Yac=
github.com/peterh/liner v0.0.0-20170317030525-88609521dc4b/go.mod h1:xIteQHvHuaLYG9IFj6mSxM0fCKrs34IrEQUhOYuGPHc=
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 h1:q2e307iGHPdTGp0hoxKjt1H5pDo6utceo3dQVK3I5XQ=
Expand Down Expand Up @@ -1111,8 +1114,9 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1-0.20210427113832-6241f9ab9942 h1:t0lM6y/M5IiUZyvbBTcngso8SZEZICH7is9B6g/obVU=
github.com/stretchr/testify v1.7.1-0.20210427113832-6241f9ab9942/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=
Expand Down
18 changes: 18 additions & 0 deletions node/config/config.go
@@ -1,6 +1,10 @@
package config

import (
"io"

"github.com/pelletier/go-toml/v2"

"github.com/celestiaorg/celestia-node/node/core"
"github.com/celestiaorg/celestia-node/node/p2p"
)
Expand All @@ -19,3 +23,17 @@ func DefaultConfig() *Config {
Core: core.DefaultConfig(),
}
}

// TODO(@Wondertan): We should have a description for each field written into w,
// so users can instantly understand purpose of each field. Ideally, we should have a utility program to parse comments
// from actual sources(*.go files) and generate docs from comments. Hint: use 'ast' package.
// WriteTo flushes a given Config into w.
func WriteTo(cfg *Config, w io.Writer) error {
return toml.NewEncoder(w).Encode(cfg)
}

// ReadFrom pulls a Config from a given reader r.
func ReadFrom(r io.Reader) (*Config, error) {
var cfg Config
return &cfg, toml.NewDecoder(r).Decode(&cfg)
}
21 changes: 21 additions & 0 deletions node/config/config_test.go
@@ -0,0 +1,21 @@
package config

import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestConfigWriteRead(t *testing.T) {
buf := bytes.NewBuffer(nil)
in := DefaultConfig()

err := WriteTo(in, buf)
require.NoError(t, err)

out, err := ReadFrom(buf)
require.NoError(t, err)
assert.EqualValues(t, in, out)
}

0 comments on commit 923f6cd

Please sign in to comment.