Add API for setting file permissions

This commit is contained in:
Kris 2019-02-22 10:54:48 -08:00 committed by Steve Francia
parent 9e56dacc08
commit fccfc2c271
1 changed files with 13 additions and 5 deletions

View File

@ -183,6 +183,7 @@ type Viper struct {
configName string configName string
configFile string configFile string
configType string configType string
configPermissions os.FileMode
envPrefix string envPrefix string
automaticEnvApplied bool automaticEnvApplied bool
@ -210,6 +211,7 @@ func New() *Viper {
v := new(Viper) v := new(Viper)
v.keyDelim = "." v.keyDelim = "."
v.configName = "config" v.configName = "config"
v.configPermissions = os.FileMode(0644)
v.fs = afero.NewOsFs() v.fs = afero.NewOsFs()
v.config = make(map[string]interface{}) v.config = make(map[string]interface{})
v.override = make(map[string]interface{}) v.override = make(map[string]interface{})
@ -1328,7 +1330,7 @@ func (v *Viper) writeConfig(filename string, force bool) error {
return fmt.Errorf("File: %s exists. Use WriteConfig to overwrite.", filename) return fmt.Errorf("File: %s exists. Use WriteConfig to overwrite.", filename)
} }
} }
f, err := v.fs.OpenFile(filename, flags, os.FileMode(0644)) f, err := v.fs.OpenFile(filename, flags, v.configPermissions)
if err != nil { if err != nil {
return err return err
} }
@ -1765,6 +1767,12 @@ func (v *Viper) SetConfigType(in string) {
} }
} }
// SetConfigPermissions sets the permissions for the config file.
func SetConfigPermissions(perm os.FileMode) { v.SetConfigPermissions(perm) }
func (v *Viper) SetConfigPermissions(perm os.FileMode) {
v.configPermissions = perm.Perm()
}
func (v *Viper) getConfigType() string { func (v *Viper) getConfigType() string {
if v.configType != "" { if v.configType != "" {
return v.configType return v.configType