Skip to content

Commit 808d394

Browse files
authored
ref(config): centralize standard config loading (#137)
into a package that has a simple convenience function and the standard app name
1 parent 46e8f26 commit 808d394

File tree

5 files changed

+29
-32
lines changed

5 files changed

+29
-32
lines changed

config.go

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
package main
22

33
import (
4-
"fmt"
5-
4+
"github.com/deis/steward/config"
65
"github.com/juju/loggo"
7-
"github.com/kelseyhightower/envconfig"
86
)
97

10-
type errModeUnsupported struct {
11-
mode string
12-
}
13-
14-
func (e errModeUnsupported) Error() string {
15-
return fmt.Sprintf("mode '%s' is unsupported", e.mode)
16-
}
17-
18-
type config struct {
8+
type rootConfig struct {
199
Mode string `envconfig:"MODE" default:"cf"`
2010
LogLevel string `envconfig:"LOG_LEVEL" default:"info"`
2111
WatchNamespaces []string `envconfig:"WATCH_NAMESPACES" default:"default"`
2212
}
2313

24-
func (c config) logLevel() loggo.Level {
14+
func getRootConfig() (*rootConfig, error) {
15+
ret := new(rootConfig)
16+
if err := config.Load(ret); err != nil {
17+
return nil, err
18+
}
19+
return ret, nil
20+
}
21+
22+
func (c rootConfig) logLevel() loggo.Level {
2523
switch c.LogLevel {
2624
case "trace":
2725
return loggo.TRACE
@@ -39,11 +37,3 @@ func (c config) logLevel() loggo.Level {
3937
return loggo.INFO
4038
}
4139
}
42-
43-
func getConfig(appName string) (*config, error) {
44-
spec := new(config)
45-
if err := envconfig.Process(appName, spec); err != nil {
46-
return nil, err
47-
}
48-
return spec, nil
49-
}

config/common.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package config
2+
3+
import (
4+
"github.com/kelseyhightower/envconfig"
5+
)
6+
7+
const (
8+
// AppName is the standard app name to use in fetching configs
9+
AppName = "steward"
10+
)
11+
12+
// Load is a convenience function for calling envconfig.Process(AppName, ret) (godoc.org/github.com/kelseyhightower/envconfig#Process)
13+
func Load(ret interface{}) error {
14+
return envconfig.Process(AppName, ret)
15+
}

main.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ import (
99
"github.com/juju/loggo"
1010
)
1111

12-
const (
13-
appName = "steward"
14-
)
15-
1612
var (
1713
logger = loggo.GetLogger("")
1814
version = "dev"
@@ -25,7 +21,7 @@ func exitWithCode(cancelFn func(), exitCode int) {
2521

2622
func main() {
2723
logger.Infof("steward version %s started", version)
28-
cfg, err := getConfig(appName)
24+
cfg, err := getRootConfig()
2925
if err != nil {
3026
logger.Criticalf("error getting config (%s)", err)
3127
os.Exit(1)

mode/utils/cf_config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package utils
22

33
import (
4+
"github.com/deis/steward/config"
45
"github.com/deis/steward/web"
5-
"github.com/kelseyhightower/envconfig"
66
)
77

88
type cfConfig struct {
@@ -15,7 +15,7 @@ type cfConfig struct {
1515

1616
func getCfConfig() (*cfConfig, error) {
1717
ret := new(cfConfig)
18-
if err := envconfig.Process(appName, ret); err != nil {
18+
if err := config.Load(ret); err != nil {
1919
return nil, err
2020
}
2121
return ret, nil

mode/utils/common.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ import (
44
"github.com/juju/loggo"
55
)
66

7-
const (
8-
appName = "steward"
9-
)
10-
117
var (
128
logger = loggo.GetLogger("mode.utils")
139
)

0 commit comments

Comments
 (0)