2016-08-23 08:31:50 +08:00
// Package config provides functions for reading and parsing the provider credentials json file.
// It cleans nonstandard json features (comments and trailing commas), as well as replaces environment variable placeholders with
// their environment variable equivalents. To reference an environment variable in your json file, simply use values in this format:
// "key"="$ENV_VAR_NAME"
package config
import (
"encoding/json"
2018-12-12 00:56:06 +08:00
"fmt"
2016-08-23 08:31:50 +08:00
"os"
"strings"
"github.com/DisposaBoy/JsonConfigReader"
"github.com/TomOnTime/utfutil"
)
// LoadProviderConfigs will open the specified file name, and parse its contents. It will replace environment variables it finds if any value matches $[A-Za-z_-0-9]+
func LoadProviderConfigs ( fname string ) ( map [ string ] map [ string ] string , error ) {
var results = map [ string ] map [ string ] string { }
dat , err := utfutil . ReadFile ( fname , utfutil . POSIX )
if err != nil {
2018-01-10 01:53:16 +08:00
// no creds file is ok. Bind requires nothing for example. Individual providers will error if things not found.
2017-03-23 00:38:08 +08:00
if os . IsNotExist ( err ) {
2018-12-12 00:56:06 +08:00
fmt . Printf ( "INFO: Config file %q does not exist. Skipping.\n" , fname )
2017-03-23 00:38:08 +08:00
return results , nil
}
Switch to Go 1.13 error wrapping (#604)
* Replaced errors.Wrap with fmt.Errorf (#589)
* Find: errors\.Wrap\(([^,]+),\s+(["`][^"`]*)(["`])\)
Replace: fmt.Errorf($2: %w$3, $1)
* Replaced errors.Wrapf with fmt.Errorf (#589)
* Find: errors\.Wrapf\(([^,]+),\s+(["`][^"`]*)(["`])\)
Replace: fmt.Errorf($2: %w$3, $1)
* Find: errors\.Wrapf\(([^,]+),\s+(["`][^"`]*)(["`])(,[^)]+)\)
* Replace: fmt.Errorf($2: %w$3$4, $1)
* Replaced errors.Errorf with fmt.Errorf (#589)
* Find: errors\.Errorf
Replace: fmt.Errorf
* Cleaned up remaining imports
* Cleanup
* Regenerate provider support matrix
This was broken by #533 ... and it's now the third time this has been missed.
2020-01-29 00:06:56 +08:00
return nil , fmt . Errorf ( "While reading provider credentials file %v: %v" , fname , err )
2016-08-23 08:31:50 +08:00
}
s := string ( dat )
r := JsonConfigReader . New ( strings . NewReader ( s ) )
err = json . NewDecoder ( r ) . Decode ( & results )
if err != nil {
Switch to Go 1.13 error wrapping (#604)
* Replaced errors.Wrap with fmt.Errorf (#589)
* Find: errors\.Wrap\(([^,]+),\s+(["`][^"`]*)(["`])\)
Replace: fmt.Errorf($2: %w$3, $1)
* Replaced errors.Wrapf with fmt.Errorf (#589)
* Find: errors\.Wrapf\(([^,]+),\s+(["`][^"`]*)(["`])\)
Replace: fmt.Errorf($2: %w$3, $1)
* Find: errors\.Wrapf\(([^,]+),\s+(["`][^"`]*)(["`])(,[^)]+)\)
* Replace: fmt.Errorf($2: %w$3$4, $1)
* Replaced errors.Errorf with fmt.Errorf (#589)
* Find: errors\.Errorf
Replace: fmt.Errorf
* Cleaned up remaining imports
* Cleanup
* Regenerate provider support matrix
This was broken by #533 ... and it's now the third time this has been missed.
2020-01-29 00:06:56 +08:00
return nil , fmt . Errorf ( "While parsing provider credentials file %v: %v" , fname , err )
2016-08-23 08:31:50 +08:00
}
if err = replaceEnvVars ( results ) ; err != nil {
return nil , err
}
return results , nil
}
func replaceEnvVars ( m map [ string ] map [ string ] string ) error {
2017-03-17 13:42:53 +08:00
for _ , keys := range m {
2016-08-23 08:31:50 +08:00
for k , v := range keys {
if strings . HasPrefix ( v , "$" ) {
env := v [ 1 : ]
newVal := os . Getenv ( env )
keys [ k ] = newVal
}
}
}
return nil
}