mirror of
https://github.com/spf13/viper
synced 2025-04-28 16:27:17 +00:00
Merge 29bf0d0e8f
into 1508a7ba44
This commit is contained in:
commit
7a1ee5f701
2 changed files with 107 additions and 9 deletions
43
viper.go
43
viper.go
|
@ -1579,19 +1579,40 @@ func WriteConfig() error { return v.WriteConfig() }
|
||||||
func (v *Viper) WriteConfig() error {
|
func (v *Viper) WriteConfig() error {
|
||||||
filename, err := v.getConfigFile()
|
filename, err := v.getConfigFile()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
if _, ok := err.(ConfigFileNotFoundError); !ok {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
filename, err = v.getDefaultConfigFile()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return v.writeConfig(filename, true)
|
|
||||||
|
v.configFile = filename
|
||||||
|
|
||||||
|
return v.WriteConfigAs(v.configFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SafeWriteConfig writes current configuration to file only if the file does not exist.
|
// SafeWriteConfig writes current configuration to file only if the file does not exist.
|
||||||
func SafeWriteConfig() error { return v.SafeWriteConfig() }
|
func SafeWriteConfig() error { return v.SafeWriteConfig() }
|
||||||
|
|
||||||
func (v *Viper) SafeWriteConfig() error {
|
func (v *Viper) SafeWriteConfig() error {
|
||||||
if len(v.configPaths) < 1 {
|
filename, err := v.getConfigFile()
|
||||||
return errors.New("missing configuration for 'configPath'")
|
if err != nil {
|
||||||
|
if _, ok := err.(ConfigFileNotFoundError); !ok {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
filename, err = v.getDefaultConfigFile()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return v.SafeWriteConfigAs(filepath.Join(v.configPaths[0], v.configName+"."+v.configType))
|
|
||||||
|
v.configFile = filename
|
||||||
|
|
||||||
|
return v.SafeWriteConfigAs(v.configFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteConfigAs writes current configuration to a given filename.
|
// WriteConfigAs writes current configuration to a given filename.
|
||||||
|
@ -2021,6 +2042,18 @@ func (v *Viper) getConfigFile() (string, error) {
|
||||||
return v.configFile, nil
|
return v.configFile, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (v *Viper) getDefaultConfigFile() (string, error) {
|
||||||
|
if len(v.configPaths) < 1 {
|
||||||
|
return "", errors.New("missing configuration for 'configPath'")
|
||||||
|
}
|
||||||
|
|
||||||
|
if v.configType == "" {
|
||||||
|
v.configType = SupportedExts[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
return filepath.Join(v.configPaths[0], v.configName+"."+v.configType), nil
|
||||||
|
}
|
||||||
|
|
||||||
// Debug prints all configuration registries for debugging
|
// Debug prints all configuration registries for debugging
|
||||||
// purposes.
|
// purposes.
|
||||||
func Debug() { v.Debug() }
|
func Debug() { v.Debug() }
|
||||||
|
|
|
@ -1739,7 +1739,7 @@ var jsonWriteExpected = []byte(`{
|
||||||
// name: steve
|
// name: steve
|
||||||
// `)
|
// `)
|
||||||
|
|
||||||
func TestWriteConfig(t *testing.T) {
|
func TestWriteConfigAs(t *testing.T) {
|
||||||
fs := afero.NewMemMapFs()
|
fs := afero.NewMemMapFs()
|
||||||
testCases := map[string]struct {
|
testCases := map[string]struct {
|
||||||
configName string
|
configName string
|
||||||
|
@ -1817,7 +1817,7 @@ func TestWriteConfig(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWriteConfigTOML(t *testing.T) {
|
func TestWriteConfigAsTOML(t *testing.T) {
|
||||||
fs := afero.NewMemMapFs()
|
fs := afero.NewMemMapFs()
|
||||||
|
|
||||||
testCases := map[string]struct {
|
testCases := map[string]struct {
|
||||||
|
@ -1868,7 +1868,7 @@ func TestWriteConfigTOML(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWriteConfigDotEnv(t *testing.T) {
|
func TestWriteConfigAsDotEnv(t *testing.T) {
|
||||||
fs := afero.NewMemMapFs()
|
fs := afero.NewMemMapFs()
|
||||||
testCases := map[string]struct {
|
testCases := map[string]struct {
|
||||||
configName string
|
configName string
|
||||||
|
@ -1917,6 +1917,56 @@ func TestWriteConfigDotEnv(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWriteConfig(t *testing.T) {
|
||||||
|
v := New()
|
||||||
|
fs := afero.NewMemMapFs()
|
||||||
|
v.SetFs(fs)
|
||||||
|
v.AddConfigPath("/test")
|
||||||
|
v.SetConfigName("c")
|
||||||
|
v.SetConfigType("yaml")
|
||||||
|
require.NoError(t, v.ReadConfig(bytes.NewBuffer(yamlExample)))
|
||||||
|
require.NoError(t, v.WriteConfig())
|
||||||
|
read, err := afero.ReadFile(fs, "/test/c.yaml")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, yamlWriteExpected, read)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWriteConfigWithExplicitlySetFile(t *testing.T) {
|
||||||
|
v := New()
|
||||||
|
fs := afero.NewMemMapFs()
|
||||||
|
v.SetFs(fs)
|
||||||
|
v.AddConfigPath("/test1")
|
||||||
|
v.SetConfigName("c1")
|
||||||
|
v.SetConfigType("yaml")
|
||||||
|
v.SetConfigFile("/test2/c2.yaml")
|
||||||
|
require.NoError(t, v.ReadConfig(bytes.NewBuffer(yamlExample)))
|
||||||
|
require.NoError(t, v.WriteConfig())
|
||||||
|
read, err := afero.ReadFile(fs, "/test2/c2.yaml")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, yamlWriteExpected, read)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWriteConfigWithMissingConfigPath(t *testing.T) {
|
||||||
|
v := New()
|
||||||
|
fs := afero.NewMemMapFs()
|
||||||
|
v.SetFs(fs)
|
||||||
|
v.SetConfigName("c")
|
||||||
|
v.SetConfigType("yaml")
|
||||||
|
require.EqualError(t, v.WriteConfig(), "missing configuration for 'configPath'")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWriteConfigWithExistingFile(t *testing.T) {
|
||||||
|
v := New()
|
||||||
|
fs := afero.NewMemMapFs()
|
||||||
|
fs.Create("/test/c.yaml")
|
||||||
|
v.SetFs(fs)
|
||||||
|
v.AddConfigPath("/test")
|
||||||
|
v.SetConfigName("c")
|
||||||
|
v.SetConfigType("yaml")
|
||||||
|
err := v.WriteConfig()
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
func TestSafeWriteConfig(t *testing.T) {
|
func TestSafeWriteConfig(t *testing.T) {
|
||||||
v := New()
|
v := New()
|
||||||
fs := afero.NewMemMapFs()
|
fs := afero.NewMemMapFs()
|
||||||
|
@ -1931,6 +1981,21 @@ func TestSafeWriteConfig(t *testing.T) {
|
||||||
assert.YAMLEq(t, string(yamlWriteExpected), string(read))
|
assert.YAMLEq(t, string(yamlWriteExpected), string(read))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSafeWriteConfigWithExplicitlySetFile(t *testing.T) {
|
||||||
|
v := New()
|
||||||
|
fs := afero.NewMemMapFs()
|
||||||
|
v.SetFs(fs)
|
||||||
|
v.AddConfigPath("/test1")
|
||||||
|
v.SetConfigName("c1")
|
||||||
|
v.SetConfigType("yaml")
|
||||||
|
v.SetConfigFile("/test2/c2.yaml")
|
||||||
|
require.NoError(t, v.ReadConfig(bytes.NewBuffer(yamlExample)))
|
||||||
|
require.NoError(t, v.SafeWriteConfig())
|
||||||
|
read, err := afero.ReadFile(fs, "/test2/c2.yaml")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, yamlWriteExpected, read)
|
||||||
|
}
|
||||||
|
|
||||||
func TestSafeWriteConfigWithMissingConfigPath(t *testing.T) {
|
func TestSafeWriteConfigWithMissingConfigPath(t *testing.T) {
|
||||||
v := New()
|
v := New()
|
||||||
fs := afero.NewMemMapFs()
|
fs := afero.NewMemMapFs()
|
||||||
|
@ -1954,7 +2019,7 @@ func TestSafeWriteConfigWithExistingFile(t *testing.T) {
|
||||||
assert.True(t, ok, "Expected ConfigFileAlreadyExistsError")
|
assert.True(t, ok, "Expected ConfigFileAlreadyExistsError")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSafeWriteAsConfig(t *testing.T) {
|
func TestSafeWriteConfigAs(t *testing.T) {
|
||||||
v := New()
|
v := New()
|
||||||
fs := afero.NewMemMapFs()
|
fs := afero.NewMemMapFs()
|
||||||
v.SetFs(fs)
|
v.SetFs(fs)
|
||||||
|
|
Loading…
Add table
Reference in a new issue