@ -35,10 +35,9 @@ type ConfigProvider interface {
}
type iniFileConfigProvider struct {
opts * Options
* ini . File
filepath string // the ini file path
newFile bool // whether the file has not existed previously
allowEmpty bool // whether not finding configuration files is allowed (only true for the tests)
newFile bool // whether the file has not existed previously
}
// NewEmptyConfigProvider create a new empty config provider
@ -66,41 +65,47 @@ func newConfigProviderFromData(configContent string) (ConfigProvider, error) {
} , nil
}
type Options struct {
CustomConf string // the ini file path
AllowEmpty bool // whether not finding configuration files is allowed (only true for the tests)
ExtraConfig string
DisableLoadCommonSettings bool
}
// newConfigProviderFromFile load configuration from file.
// NOTE: do not print any log except error.
func newConfigProviderFromFile ( customConf string , allowEmpty bool , extraConfig string ) ( * iniFileConfigProvider , error ) {
func newConfigProviderFromFile ( opts * Options ) ( * iniFileConfigProvider , error ) {
cfg := ini . Empty ( )
newFile := true
if customConf != "" {
isFile , err := util . IsFile ( c ustomConf)
if opts . C ustomConf != "" {
isFile , err := util . IsFile ( opts . C ustomConf)
if err != nil {
return nil , fmt . Errorf ( "unable to check if %s is a file. Error: %v" , c ustomConf, err )
return nil , fmt . Errorf ( "unable to check if %s is a file. Error: %v" , opts . C ustomConf, err )
}
if isFile {
if err := cfg . Append ( c ustomConf) ; err != nil {
return nil , fmt . Errorf ( "failed to load custom conf '%s': %v" , c ustomConf, err )
if err := cfg . Append ( opts . C ustomConf) ; err != nil {
return nil , fmt . Errorf ( "failed to load custom conf '%s': %v" , opts . C ustomConf, err )
}
newFile = false
}
}
if newFile && ! a llowEmpty {
if newFile && ! opts . A llowEmpty {
return nil , fmt . Errorf ( "unable to find configuration file: %q, please ensure you are running in the correct environment or set the correct configuration file with -c" , CustomConf )
}
if e xtraConfig != "" {
if err := cfg . Append ( [ ] byte ( e xtraConfig) ) ; err != nil {
if opts . E xtraConfig != "" {
if err := cfg . Append ( [ ] byte ( opts . E xtraConfig) ) ; err != nil {
return nil , fmt . Errorf ( "unable to append more config: %v" , err )
}
}
cfg . NameMapper = ini . SnackCase
return & iniFileConfigProvider {
File : cfg ,
filepath : customConf ,
newFile : newFile ,
allowEmpty : allowEmpty ,
opts : opts ,
File : cfg ,
newFile : newFile ,
} , nil
}
@ -123,8 +128,8 @@ func (p *iniFileConfigProvider) DeleteSection(name string) error {
// Save save the content into file
func ( p * iniFileConfigProvider ) Save ( ) error {
if p . filepath == "" {
if ! p . a llowEmpty {
if p . opts . CustomConf == "" {
if ! p . opts . A llowEmpty {
return fmt . Errorf ( "custom config path must not be empty" )
}
return nil
@ -135,8 +140,8 @@ func (p *iniFileConfigProvider) Save() error {
return fmt . Errorf ( "failed to create '%s': %v" , CustomConf , err )
}
}
if err := p . SaveTo ( p . filepath ) ; err != nil {
return fmt . Errorf ( "failed to save '%s': %v" , p . filepath , err )
if err := p . SaveTo ( p . opts . CustomConf ) ; err != nil {
return fmt . Errorf ( "failed to save '%s': %v" , p . opts . CustomConf , err )
}
// Change permissions to be more restrictive