github.com/gtmtechltd/terraform@v0.11.12-beta1/tools/terraform-bundle/config.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 7 "github.com/hashicorp/hcl" 8 "github.com/hashicorp/terraform/plugin/discovery" 9 ) 10 11 type Config struct { 12 Terraform TerraformConfig `hcl:"terraform"` 13 Providers map[string][]discovery.ConstraintStr `hcl:"providers"` 14 } 15 16 type TerraformConfig struct { 17 Version discovery.VersionStr `hcl:"version"` 18 } 19 20 func LoadConfig(src []byte, filename string) (*Config, error) { 21 config := &Config{} 22 err := hcl.Decode(config, string(src)) 23 if err != nil { 24 return config, err 25 } 26 27 err = config.validate() 28 return config, err 29 } 30 31 func LoadConfigFile(filename string) (*Config, error) { 32 src, err := ioutil.ReadFile(filename) 33 if err != nil { 34 return nil, err 35 } 36 37 return LoadConfig(src, filename) 38 } 39 40 func (c *Config) validate() error { 41 if c.Terraform.Version == "" { 42 return fmt.Errorf("terraform.version is required") 43 } 44 45 if _, err := c.Terraform.Version.Parse(); err != nil { 46 return fmt.Errorf("terraform.version: %s", err) 47 } 48 49 if c.Providers == nil { 50 c.Providers = map[string][]discovery.ConstraintStr{} 51 } 52 53 for k, cs := range c.Providers { 54 for _, c := range cs { 55 if _, err := c.Parse(); err != nil { 56 return fmt.Errorf("providers.%s: %s", k, err) 57 } 58 } 59 } 60 61 return nil 62 }