From b1fdc47b0d05b6af898a3d50aefd62c5825a17fe Mon Sep 17 00:00:00 2001 From: Jim Razmus II Date: Wed, 13 May 2020 12:15:28 -0500 Subject: [PATCH] Recognize tfvars files as hcl by default. Signed-off-by: Mark Sagi-Kazar --- viper.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/viper.go b/viper.go index 6c1bcae..9e2e353 100644 --- a/viper.go +++ b/viper.go @@ -105,6 +105,9 @@ func init() { encoderRegistry.RegisterEncoder("hcl", codec) decoderRegistry.RegisterDecoder("hcl", codec) + + encoderRegistry.RegisterEncoder("tfvars", codec) + decoderRegistry.RegisterDecoder("tfvars", codec) } } @@ -329,7 +332,7 @@ func NewWithOptions(opts ...Option) *Viper { // can use it in their testing as well. func Reset() { v = New() - SupportedExts = []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl", "dotenv", "env", "ini"} + SupportedExts = []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl", "tfvars", "dotenv", "env", "ini"} SupportedRemoteProviders = []string{"etcd", "consul", "firestore"} } @@ -368,7 +371,7 @@ type RemoteProvider interface { } // SupportedExts are universally supported extensions. -var SupportedExts = []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl", "dotenv", "env", "ini"} +var SupportedExts = []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl", "tfvars", "dotenv", "env", "ini"} // SupportedRemoteProviders are universally supported remote providers. var SupportedRemoteProviders = []string{"etcd", "consul", "firestore"} @@ -1624,7 +1627,7 @@ func (v *Viper) unmarshalReader(in io.Reader, c map[string]interface{}) error { buf.ReadFrom(in) switch format := strings.ToLower(v.getConfigType()); format { - case "yaml", "yml", "json", "toml", "hcl": + case "yaml", "yml", "json", "toml", "hcl", "tfvars": err := decoderRegistry.Decode(format, buf.Bytes(), &c) if err != nil { return ConfigParseError{err} @@ -1681,7 +1684,7 @@ func (v *Viper) unmarshalReader(in io.Reader, c map[string]interface{}) error { func (v *Viper) marshalWriter(f afero.File, configType string) error { c := v.AllSettings() switch configType { - case "yaml", "yml", "json", "toml", "hcl": + case "yaml", "yml", "json", "toml", "hcl", "tfvars": b, err := encoderRegistry.Encode(configType, c) if err != nil { return ConfigMarshalError{err}