mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-27 03:31:42 +00:00
cli: WriteDemoConfig -> WriteConfigVals
This commit is contained in:
@ -1,5 +1,11 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 0.7.0 (TBD)
|
||||||
|
|
||||||
|
BREAKING:
|
||||||
|
|
||||||
|
- [cli] WriteDemoConfig -> WriteConfigValues
|
||||||
|
|
||||||
## 0.6.0 (December 29, 2017)
|
## 0.6.0 (December 29, 2017)
|
||||||
|
|
||||||
BREAKING:
|
BREAKING:
|
||||||
|
@ -9,21 +9,15 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
// WriteDemoConfig writes a toml file with the given values.
|
// WriteConfigVals writes a toml file with the given values.
|
||||||
// It returns the RootDir the config.toml file is stored in,
|
// It returns an error if writing was impossible.
|
||||||
// or an error if writing was impossible
|
func WriteConfigVals(dir string, vals map[string]string) error {
|
||||||
func WriteDemoConfig(vals map[string]string) (string, error) {
|
|
||||||
cdir, err := ioutil.TempDir("", "test-cli")
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
data := ""
|
data := ""
|
||||||
for k, v := range vals {
|
for k, v := range vals {
|
||||||
data = data + fmt.Sprintf("%s = \"%s\"\n", k, v)
|
data = data + fmt.Sprintf("%s = \"%s\"\n", k, v)
|
||||||
}
|
}
|
||||||
cfile := filepath.Join(cdir, "config.toml")
|
cfile := filepath.Join(dir, "config.toml")
|
||||||
err = ioutil.WriteFile(cfile, []byte(data), 0666)
|
return ioutil.WriteFile(cfile, []byte(data), 0666)
|
||||||
return cdir, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunWithArgs executes the given command with the specified command line args
|
// RunWithArgs executes the given command with the specified command line args
|
||||||
|
@ -3,6 +3,7 @@ package cli
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@ -131,6 +132,7 @@ func bindFlagsLoadViper(cmd *cobra.Command, args []string) error {
|
|||||||
viper.Set(HomeFlag, homeDir)
|
viper.Set(HomeFlag, homeDir)
|
||||||
viper.SetConfigName("config") // name of config file (without extension)
|
viper.SetConfigName("config") // name of config file (without extension)
|
||||||
viper.AddConfigPath(homeDir) // search root directory
|
viper.AddConfigPath(homeDir) // search root directory
|
||||||
|
viper.AddConfigPath(filepath.Join(homeDir, "config")) // search root directory /config
|
||||||
|
|
||||||
// If a config file is found, read it in.
|
// If a config file is found, read it in.
|
||||||
if err := viper.ReadInConfig(); err == nil {
|
if err := viper.ReadInConfig(); err == nil {
|
||||||
|
@ -2,6 +2,7 @@ package cli
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@ -54,11 +55,20 @@ func TestSetupEnv(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func tempDir() string {
|
||||||
|
cdir, err := ioutil.TempDir("", "test-cli")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return cdir
|
||||||
|
}
|
||||||
|
|
||||||
func TestSetupConfig(t *testing.T) {
|
func TestSetupConfig(t *testing.T) {
|
||||||
// we pre-create two config files we can refer to in the rest of
|
// we pre-create two config files we can refer to in the rest of
|
||||||
// the test cases.
|
// the test cases.
|
||||||
cval1 := "fubble"
|
cval1 := "fubble"
|
||||||
conf1, err := WriteDemoConfig(map[string]string{"boo": cval1})
|
conf1 := tempDir()
|
||||||
|
err := WriteConfigVals(conf1, map[string]string{"boo": cval1})
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
|
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
@ -116,10 +126,12 @@ func TestSetupUnmarshal(t *testing.T) {
|
|||||||
// we pre-create two config files we can refer to in the rest of
|
// we pre-create two config files we can refer to in the rest of
|
||||||
// the test cases.
|
// the test cases.
|
||||||
cval1, cval2 := "someone", "else"
|
cval1, cval2 := "someone", "else"
|
||||||
conf1, err := WriteDemoConfig(map[string]string{"name": cval1})
|
conf1 := tempDir()
|
||||||
|
err := WriteConfigVals(conf1, map[string]string{"name": cval1})
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
// even with some ignored fields, should be no problem
|
// even with some ignored fields, should be no problem
|
||||||
conf2, err := WriteDemoConfig(map[string]string{"name": cval2, "foo": "bar"})
|
conf2 := tempDir()
|
||||||
|
err = WriteConfigVals(conf2, map[string]string{"name": cval2, "foo": "bar"})
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
|
|
||||||
// unused is not declared on a flag and remains from base
|
// unused is not declared on a flag and remains from base
|
||||||
|
Reference in New Issue
Block a user