main: add main config

This commit is contained in:
Mitchell Hashimoto 2014-05-25 17:39:44 -07:00
parent 81bfa8970a
commit 5d40725139
3 changed files with 77 additions and 0 deletions

45
config.go Normal file
View File

@ -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
}

28
config_test.go Normal file
View File

@ -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)
}
}

4
test-fixtures/config Normal file
View File

@ -0,0 +1,4 @@
providers {
"aws" = "foo"
"do" = "bar"
}