Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Switch to my goconfig to improve matters a little.

  • Loading branch information...
commit 0fce07e82ce2a117df5ca9d0be21e5a5a3ea11ff 1 parent ac99376
@robfig robfig authored
Showing with 21 additions and 41 deletions.
  1. +18 −39 config.go
  2. +2 −1  revel.go
  3. +1 −1  samples/chat/conf/app.conf
View
57 config.go
@@ -2,7 +2,7 @@ package rev
import (
"errors"
- "github.com/kless/goconfig/config"
+ "github.com/robfig/goconfig/config"
"path"
)
@@ -29,21 +29,21 @@ func LoadConfig(confName string) (*MergedConfig, error) {
return nil, err
}
-// The top-level keys are in a section called "DEFAULT". The DEFAULT is used as
-// a fall-back if the key is not found in the section that has been set.
-const defaultSection = "DEFAULT"
-
func (c *MergedConfig) SetSection(section string) {
c.section = section
}
func (c *MergedConfig) Int(option string) (result int, found bool) {
- if result, found = getInt(c.Config, c.section, option); found {
- return
+ result, err := c.Config.Int(c.section, option)
+ if err == nil {
+ return result, true
}
- if result, found = getInt(c.Config, defaultSection, option); found {
- return
+ if _, ok := err.(config.OptionError); ok {
+ return 0, false
}
+
+ // If it wasn't an OptionError, it must have failed to parse.
+ ERROR.Println("Failed to parse config option", option, "as int:", err)
return 0, false
}
@@ -55,12 +55,16 @@ func (c *MergedConfig) IntDefault(option string, dfault int) int {
}
func (c *MergedConfig) Bool(option string) (result, found bool) {
- if result, found = getBool(c.Config, c.section, option); found {
- return
+ result, err := c.Config.Bool(c.section, option)
+ if err == nil {
+ return result, true
}
- if result, found = getBool(c.Config, defaultSection, option); found {
- return
+ if _, ok := err.(config.OptionError); ok {
+ return false, false
}
+
+ // If it wasn't an OptionError, it must have failed to parse.
+ ERROR.Println("Failed to parse config option", option, "as bool:", err)
return false, false
}
@@ -75,8 +79,7 @@ func (c *MergedConfig) String(option string) (result string, found bool) {
if r, err := c.Config.String(c.section, option); err == nil {
return stripQuotes(r), true
}
- r, err := c.Config.String(defaultSection, option)
- return stripQuotes(r), err == nil
+ return "", false
}
func (c *MergedConfig) StringDefault(option, dfault string) string {
@@ -99,27 +102,3 @@ func stripQuotes(s string) string {
return s
}
-
-func getInt(config *config.Config, section, option string) (result int, found bool) {
- // TODO: config.HasOption checks the DEFAULT section, while config.Int does not.
- // File an issue to make them agree, and use HasOption instead of RawString.
- if _, err := config.RawString(section, option); err == nil {
- r, err := config.Int(section, option)
- if err == nil {
- return r, true
- }
- ERROR.Println("Failed to parse config option", option, "as int:", err)
- }
- return 0, false
-}
-
-func getBool(config *config.Config, section, option string) (result, found bool) {
- if config.HasOption(section, option) {
- r, err := config.Bool(section, option)
- if err == nil {
- return r, true
- }
- ERROR.Println("Failed to parse config option", option, "as bool:", err)
- }
- return false, false
-}
View
3  revel.go
@@ -1,6 +1,7 @@
package rev
import (
+ "github.com/robfig/goconfig/config"
"go/build"
"io"
"log"
@@ -91,7 +92,7 @@ func Init(mode, importPath, srcPath string) {
// Ensure that the selected runmode appears in app.conf.
// If empty string is passed as the mode, treat it as "DEFAULT"
if mode == "" {
- mode = defaultSection
+ mode = config.DEFAULT_SECTION
}
if !Config.HasSection(mode) {
log.Fatalln("app.conf: No mode found:", mode)
View
2  samples/chat/conf/app.conf
@@ -13,8 +13,8 @@ log.warn.output = stderr
log.error.output = stderr
[prod]
-server.watcher=false
results.pretty=false
+server.watcher=false
log.trace.output = off
log.info.output = off
Please sign in to comment.
Something went wrong with that request. Please try again.