Skip to content
This repository has been archived by the owner on Dec 7, 2020. It is now read-only.

Commit

Permalink
Merge pull request #10 from 030/gh9-nested-yaml
Browse files Browse the repository at this point in the history
[GH-9] extract values from nested hash
  • Loading branch information
030 authored Nov 3, 2018
2 parents 4c90558 + 9bca090 commit 89f50ef
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func scanFile(file string, key string) string {

for scanner.Scan() {
logrus.Debug("Check whether file: ", file, " and line: ", scanner.Text(), " contains key: ", key)
if strings.Contains(scanner.Text(), key+":") {
if strings.Contains(scanner.Text(), key+": ") {
val = value(scanner.Text())
}
}
Expand All @@ -38,7 +38,7 @@ func scanFile(file string, key string) string {
}

func value(keyValue string) string {
re := regexp.MustCompile("^[a-z-_]+: (.*)$")
re := regexp.MustCompile("[A-Z-a-z-_]+: (.*)$")
match := re.FindStringSubmatch(keyValue)

if len(match) == 0 {
Expand Down
12 changes: 7 additions & 5 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ import "testing"

func TestScanFile(t *testing.T) {
keyValue := map[string]string{
"a": "abc",
"b": "def",
"c": "ghi",
"under_scores": "ensure that keys may contain an underscore",
"a": "abc",
"b": "def",
"c": "ghi",
"under_scores": "ensure that keys may contain an underscore",
"image": "someimage",
"MYSQL_ROOT_PASSWORD": "somewordpress",
}

for key, value := range keyValue {
expected := value
actual := scanFile("test.yaml", key)
if expected != actual {
t.Errorf("Value was incorrect, got: %s, want: %s.", actual, expected)
t.Errorf("Value was incorrect 'Check whether het key '%s' resides in the test yaml file', got value: %s, want: %s.", key, actual, expected)
}
}
}
9 changes: 8 additions & 1 deletion test.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
a: abc
b: def
c: ghi
under_scores: ensure that keys may contain an underscore
under_scores: ensure that keys may contain an underscore
hihi: world
services:
db:
image: someimage
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress

0 comments on commit 89f50ef

Please sign in to comment.