Skip to content

Commit

Permalink
Extract config parsing in ParseConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislas-m committed Dec 28, 2018
1 parent f7726b4 commit bb5ef70
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 16 deletions.
39 changes: 23 additions & 16 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/gobuffalo/envy"
"github.com/gobuffalo/pop/logging"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"
)

// ErrConfigFileNotFound is returned when the pop config file can't be found,
Expand Down Expand Up @@ -75,6 +75,24 @@ func findConfigPath() (string, error) {
// LoadFrom reads a configuration from the reader and sets up the connections
func LoadFrom(r io.Reader) error {
envy.Load()
deets, err := ParseConfig(r)
if err != nil {
return err
}
for n, d := range deets {
con, err := NewConnection(d)
if err != nil {
log(logging.Warn, "unable to load connection %s: %v", n, err)
continue
}
Connections[n] = con
}
return nil
}

// ParseConfig reads the pop config from the given io.Reader and returns
// the parsed ConnectionDetails map.
func ParseConfig(r io.Reader) (map[string]*ConnectionDetails, error) {
tmpl := template.New("test")
tmpl.Funcs(map[string]interface{}{
"envOr": func(s1, s2 string) string {
Expand All @@ -86,31 +104,20 @@ func LoadFrom(r io.Reader) error {
})
b, err := ioutil.ReadAll(r)
if err != nil {
return errors.WithStack(err)
return nil, errors.WithStack(err)
}
t, err := tmpl.Parse(string(b))
if err != nil {
return errors.Wrap(err, "couldn't parse config template")
return nil, errors.Wrap(err, "couldn't parse config template")
}

var bb bytes.Buffer
err = t.Execute(&bb, nil)
if err != nil {
return errors.Wrap(err, "couldn't execute config template")
return nil, errors.Wrap(err, "couldn't execute config template")
}

deets := map[string]*ConnectionDetails{}
err = yaml.Unmarshal(bb.Bytes(), &deets)
if err != nil {
return errors.Wrap(err, "couldn't unmarshal config to yaml")
}
for n, d := range deets {
con, err := NewConnection(d)
if err != nil {
log(logging.Warn, "unable to load connection %s: %v", n, err)
continue
}
Connections[n] = con
}
return nil
return deets, errors.Wrap(err, "couldn't unmarshal config to yaml")
}
25 changes: 25 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pop

import (
"strings"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -18,3 +19,27 @@ func Test_AddLookupPaths(t *testing.T) {
AddLookupPaths("./foo")
r.Contains(LookupPaths(), "./foo")
}

func Test_ParseConfig(t *testing.T) {
r := require.New(t)
config := strings.NewReader(`
mysql:
dialect: "mysql"
database: "pop_test"
host: {{ envOr "MYSQL_HOST" "127.0.0.1" }}
port: {{ envOr "MYSQL_PORT" "3306" }}
user: {{ envOr "MYSQL_USER" "root" }}
password: {{ envOr "MYSQL_PASSWORD" "root" }}
options:
readTimeout: 5s`)
conns, err := ParseConfig(config)
r.NoError(err)
r.Equal(1, len(conns))
r.NotNil(conns["mysql"])
r.Equal("mysql", conns["mysql"].Dialect)
r.Equal("pop_test", conns["mysql"].Database)
r.Equal("127.0.0.1", conns["mysql"].Host)
r.Equal("3306", conns["mysql"].Port)
r.Equal("root", conns["mysql"].Password)
r.Equal("5s", conns["mysql"].Options["readTimeout"])
}

0 comments on commit bb5ef70

Please sign in to comment.