Skip to content
This repository has been archived by the owner on Dec 19, 2022. It is now read-only.

Commit

Permalink
Merge pull request #182 from filecoin-project/feat/market-node-cfg-co…
Browse files Browse the repository at this point in the history
…mpatible

feat: configuration: compatible
  • Loading branch information
zl03jsj authored Feb 14, 2022
2 parents 1ddbde5 + 5a790b2 commit cd92c4f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
23 changes: 21 additions & 2 deletions config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,32 @@ func UpdateConfig(path string, cfg interface{}) error {
}

// MinerFromReader loads config from a reader instance.
func MinerFromReader(reader io.Reader) (*StorageMiner, error) {
func MinerFromReader(reader interface {
io.Reader
io.Seeker
}) (*StorageMiner, error) {
cfg := DefaultMainnetStorageMiner()
_, err := toml.DecodeReader(reader, cfg)
meta, err := toml.DecodeReader(reader, cfg)
if err != nil {
return nil, err
}

// MarketNodeConfig of venus-sealer v1 in configuration file is define as 'Market',
// but in v1.14.0 is 'MarketNode', the following code is just for compatible,
// TODO: may there be a more graceful approach to do that.
if meta.IsDefined("Market") && !meta.IsDefined("MarketNode") {
if _, err := reader.Seek(0, io.SeekStart); err != nil {
return nil, xerrors.Errorf("read 'Market' as 'MarketNode' failed, seek file failed:%w", err)
}
var tmpMinerCfg = &struct {
Market MarketNodeConfig `toml:"Market"`
}{}
if _, err := toml.DecodeReader(reader, tmpMinerCfg); err != nil {
return nil, xerrors.Errorf("read 'Market' as 'MarketNode' failed, DecodeReader failed:%w", err)
}
cfg.MarketNode = tmpMinerCfg.Market
}

err = envconfig.Process("MINER", cfg)
if err != nil {
return nil, fmt.Errorf("processing env vars overrides: %s", err)
Expand Down
17 changes: 17 additions & 0 deletions config/load_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
package config

import (
"bytes"
"fmt"
"github.com/stretchr/testify/require"
"testing"
)

func TestMinerConfigCompatible(t *testing.T) {
var exampleCfg = MarketNodeConfig{
Url: "/ip4/192.168.200.19/tcp/41235",
Token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiMS0xMjUiLCJwZXJtIjoic2lnbiIsImV4dCI6IiJ9.JenwgK0JZcxFDin3cyhBUN41VXNvYw-_0UUT2ZOohM0"}
var configToml = fmt.Sprintf("[Market]\n\tUrl = \"%s\"\n\tToken = \"%s\"\n", exampleCfg.Url, exampleCfg.Token)
cfg, err := MinerFromReader(bytes.NewReader([]byte(configToml)))
require.NoError(t, err)
require.Equal(t, cfg.MarketNode, exampleCfg)
}

0 comments on commit cd92c4f

Please sign in to comment.