-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from hidevopsio/yaml
added func ReadYamlFromFile
- Loading branch information
Showing
4 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,6 @@ logging: | |
|
||
# added for test only | ||
fake: | ||
name: ${app.name} | ||
name: ${app.name} | ||
|
||
foo: foo name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
foo: foo name | ||
bar: bar name | ||
--- | ||
|
||
# test | ||
|
||
* test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package system | ||
|
||
import ( | ||
"bytes" | ||
"github.com/spf13/afero" | ||
"gopkg.in/yaml.v2" | ||
"strings" | ||
) | ||
|
||
// ReadYamlFromFile read yaml from file directly | ||
func ReadYamlFromFile(file string) (prop map[string]interface{}, err error) { | ||
fs := afero.NewOsFs() | ||
var fb []byte | ||
fb, err = afero.ReadFile(fs, file) | ||
if err == nil { | ||
buf := new(bytes.Buffer) | ||
r := bytes.NewReader(fb) | ||
buf.ReadFrom(r) | ||
str := string(buf.Bytes()) | ||
s := strings.Split(str, "---") | ||
var src []byte | ||
if len(s) == 1 { | ||
src = buf.Bytes() | ||
} else { | ||
src = []byte(s[1]) | ||
} | ||
|
||
err = yaml.Unmarshal(src, &prop) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package system | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestReadYamlProperties(t *testing.T) { | ||
|
||
res, err := ReadYamlFromFile("config/test-file.yml") | ||
|
||
assert.Equal(t, nil, err) | ||
assert.Equal(t, "foo name", res["foo"]) | ||
|
||
res, err = ReadYamlFromFile("config/application.yml") | ||
|
||
assert.Equal(t, nil, err) | ||
assert.Equal(t, "foo name", res["foo"]) | ||
} |