main: add main config
This commit is contained in:
parent
81bfa8970a
commit
5d40725139
|
@ -0,0 +1,45 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/mitchellh/go-libucl"
|
||||
)
|
||||
|
||||
// Config is the structure of the configuration for the Terraform CLI.
|
||||
//
|
||||
// This is not the configuration for Terraform itself. That is in the
|
||||
// "config" package.
|
||||
type Config struct {
|
||||
Providers map[string]string
|
||||
}
|
||||
|
||||
// Put the parse flags we use for libucl in a constant so we can get
|
||||
// equally behaving parsing everywhere.
|
||||
const libuclParseFlags = libucl.ParserKeyLowercase
|
||||
|
||||
// LoadConfig loads the CLI configuration from ".terraformrc" files.
|
||||
func LoadConfig(path string) (*Config, error) {
|
||||
var obj *libucl.Object
|
||||
|
||||
// Parse the file and get the root object.
|
||||
parser := libucl.NewParser(libuclParseFlags)
|
||||
err := parser.AddFile(path)
|
||||
if err == nil {
|
||||
obj = parser.Object()
|
||||
defer obj.Close()
|
||||
}
|
||||
defer parser.Close()
|
||||
|
||||
// If there was an error parsing, return now.
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Build up the result
|
||||
var result Config
|
||||
|
||||
if err := obj.Decode(&result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// This is the directory where our test fixtures are.
|
||||
const fixtureDir = "./test-fixtures"
|
||||
|
||||
func TestLoadConfig(t *testing.T) {
|
||||
c, err := LoadConfig(filepath.Join(fixtureDir, "config"))
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
expected := &Config{
|
||||
Providers: map[string]string{
|
||||
"aws": "foo",
|
||||
"do": "bar",
|
||||
},
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(c, expected) {
|
||||
t.Fatalf("bad: %#v", c)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
providers {
|
||||
"aws" = "foo"
|
||||
"do" = "bar"
|
||||
}
|
Loading…
Reference in New Issue