Skip to content

Commit

Permalink
Merge pull request #27 from acoshift/rename-config
Browse files Browse the repository at this point in the history
rename config
  • Loading branch information
acoshift committed May 18, 2018
2 parents c59f63a + 95d8c74 commit c4b2195
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
25 changes: 14 additions & 11 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package hime

import (
"os"
"io/ioutil"
"time"

yaml "gopkg.in/yaml.v2"
Expand Down Expand Up @@ -42,7 +42,7 @@ func parseDuration(s string, t *time.Duration) {
}
}

// Load loads config
// Config merges config into app's config
//
// Example:
//
Expand Down Expand Up @@ -72,7 +72,7 @@ func parseDuration(s string, t *time.Duration) {
// gracefulShutdown:
// timeout: 1m
// wait: 5s
func (app *App) Load(config Config) *App {
func (app *App) Config(config Config) *App {
app.Globals(config.Globals)
app.Routes(config.Routes)

Expand Down Expand Up @@ -110,19 +110,22 @@ func (app *App) Load(config Config) *App {
return app
}

// LoadFromFile loads config from file
func (app *App) LoadFromFile(filename string) *App {
fs, err := os.Open(filename)
// ParseConfig parses config data
func (app *App) ParseConfig(data []byte) *App {
var config Config
err := yaml.Unmarshal(data, &config)
if err != nil {
panic(err)
}
defer fs.Close()

var config Config
err = yaml.NewDecoder(fs).Decode(&config)
return app.Config(config)
}

// ParseConfigFile parses config from file
func (app *App) ParseConfigFile(filename string) *App {
data, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}

return app.Load(config)
return app.ParseConfig(data)
}
12 changes: 6 additions & 6 deletions config_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestConfig(t *testing.T) {

t.Run("Config1", func(t *testing.T) {
assert.NotPanics(t, func() {
app := New().LoadFromFile("testdata/config1.yaml")
app := New().ParseConfigFile("testdata/config1.yaml")

// globals
assert.Len(t, app.globals, 1)
Expand Down Expand Up @@ -44,7 +44,7 @@ func TestConfig(t *testing.T) {

t.Run("Config2", func(t *testing.T) {
assert.NotPanics(t, func() {
app := New().LoadFromFile("testdata/config2.yaml")
app := New().ParseConfigFile("testdata/config2.yaml")

assert.Len(t, app.globals, 0)
assert.Len(t, app.routes, 0)
Expand All @@ -65,7 +65,7 @@ func TestConfig(t *testing.T) {

t.Run("Config3", func(t *testing.T) {
assert.NotPanics(t, func() {
app := New().LoadFromFile("testdata/config3.yaml")
app := New().ParseConfigFile("testdata/config3.yaml")

assert.Len(t, app.globals, 0)
assert.Len(t, app.routes, 0)
Expand All @@ -84,19 +84,19 @@ func TestConfig(t *testing.T) {

t.Run("ConfigNotFound", func(t *testing.T) {
assert.Panics(t, func() {
New().LoadFromFile("testdata/notexists.yaml")
New().ParseConfigFile("testdata/notexists.yaml")
})
})

t.Run("Invalid1", func(t *testing.T) {
assert.Panics(t, func() {
New().LoadFromFile("testdata/invalid1.yaml")
New().ParseConfigFile("testdata/invalid1.yaml")
})
})

t.Run("Invalid2", func(t *testing.T) {
assert.Panics(t, func() {
New().LoadFromFile("testdata/invalid2.yaml")
New().ParseConfigFile("testdata/invalid2.yaml")
})
})
}

0 comments on commit c4b2195

Please sign in to comment.