Skip to content

Commit

Permalink
Use go-ucfg based variable expansion support
Browse files Browse the repository at this point in the history
  • Loading branch information
urso committed Jul 7, 2016
1 parent 3972206 commit a380783
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 453 deletions.
18 changes: 1 addition & 17 deletions libbeat/cfgfile/cfgfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cfgfile
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -60,22 +59,7 @@ func Load(path string) (*common.Config, error) {
if path == "" {
path = *configfile
}

fileContent, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read %s: %v", path, err)
}
fileContent, err = expandEnv(filepath.Base(path), fileContent)
if err != nil {
return nil, err
}

config, err := common.NewConfigWithYAML(fileContent, path)
if err != nil {
return nil, fmt.Errorf("YAML config parsing failed on %s: %v", path, err)
}

return config, nil
return common.LoadFile(path)
}

// IsTestConfig returns whether or not this is configuration used for testing
Expand Down
74 changes: 7 additions & 67 deletions libbeat/cfgfile/cfgfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
)

type TestConfig struct {
Output ElasticsearchConfig
Output ElasticsearchConfig
Env string `config:"env.test_key"`
EnvDefault string `config:"env.default"`
}

type ElasticsearchConfig struct {
Expand All @@ -25,6 +27,7 @@ type Connection struct {

func TestRead(t *testing.T) {
absPath, err := filepath.Abs("../tests/files/")
os.Setenv("TEST_KEY", "test_value")

assert.NotNil(t, absPath)
assert.Nil(t, err)
Expand All @@ -34,72 +37,9 @@ func TestRead(t *testing.T) {
err = Read(config, absPath+"/config.yml")
assert.Nil(t, err)

// Access config
// validate
assert.Equal(t, "localhost", config.Output.Elasticsearch.Host)

// Chat that it is integer
assert.Equal(t, 9200, config.Output.Elasticsearch.Port)
}

func TestExpandEnv(t *testing.T) {
var tests = []struct {
in string
out string
err string
}{
// Environment variables can be specified as ${env} only.
{"${y}", "y", ""},
{"$y", "$y", ""},

// Environment variables are case-sensitive.
{"${Y}", "", ""},

// Defaults can be specified.
{"x${Z:D}", "xD", ""},
{"x${Z:A B C D}", "xA B C D", ""}, // Spaces are allowed in the default.
{"x${Z:}", "x", ""},

// Un-matched braces cause an error.
{"x${Y ${Z:Z}", "", "unexpected character in variable expression: " +
"U+0020 ' ', expected a default value or closing brace"},

// Special environment variables are not replaced.
{"$*", "$*", ""},
{"${*}", "", "shell variable cannot start with U+002A '*'"},
{"$@", "$@", ""},
{"${@}", "", "shell variable cannot start with U+0040 '@'"},
{"$1", "$1", ""},
{"${1}", "", "shell variable cannot start with U+0031 '1'"},

{"", "", ""},
{"$$", "$$", ""},

{"${a_b}", "", ""}, // Underscores are allowed in variable names.

// ${} cannot be split across newlines.
{"hello ${name: world\n}", "", "unterminated brace"},

// To use a literal '${' you write '$${'.
{`password: "abc$${!"`, `password: "abc${!"`, ""},

// The full error contains the line number.
{"test:\n name: ${var", "", "failure while expanding environment " +
"variables in config.yml at line=2, unterminated brace"},
}

for _, test := range tests {
os.Setenv("y", "y")
output, err := expandEnv("config.yml", []byte(test.in))

switch {
case test.err != "" && err == nil:
t.Errorf("Expected an error for test case %+v", test)
case test.err == "" && err != nil:
t.Errorf("Unexpected error for test case %+v, %v", test, err)
case err != nil:
assert.Contains(t, err.Error(), test.err)
default:
assert.Equal(t, test.out, string(output), "Input: %s", test.in)
}
}
assert.Equal(t, "test_value", config.Env)
assert.Equal(t, "default", config.EnvDefault)
}
Loading

0 comments on commit a380783

Please sign in to comment.