refactor(config): make config its own package

Refactor config into its own package. Trying to tease the config from
the server so that all of the control surfaces are exposed in the Server
for easier testing.
release-0.4
Brandon Philips 2014-01-27 15:03:01 -08:00
parent 69922340f6
commit f56965b1c0
5 changed files with 37 additions and 18 deletions

View File

@ -1,4 +1,4 @@
package server
package config
import (
"encoding/json"
@ -15,6 +15,8 @@ import (
"github.com/coreos/etcd/third_party/github.com/BurntSushi/toml"
"github.com/coreos/etcd/log"
"github.com/coreos/etcd/server"
ustrings "github.com/coreos/etcd/pkg/strings"
)
// The default location for the etcd configuration file.
@ -82,8 +84,8 @@ type Config struct {
GraphiteHost string `toml:"graphite_host" env:"ETCD_GRAPHITE_HOST"`
}
// NewConfig returns a Config initialized with default values.
func NewConfig() *Config {
// New returns a Config initialized with default values.
func New() *Config {
c := new(Config)
c.SystemPath = DefaultSystemConfigPath
c.Addr = "127.0.0.1:4001"
@ -197,7 +199,7 @@ func (c *Config) loadEnv(target interface{}) error {
case reflect.String:
value.Field(i).SetString(v)
case reflect.Slice:
value.Field(i).Set(reflect.ValueOf(trimsplit(v, ",")))
value.Field(i).Set(reflect.ValueOf(ustrings.TrimSplit(v, ",")))
}
}
return nil
@ -293,10 +295,10 @@ func (c *Config) LoadFlags(arguments []string) error {
// Convert some parameters to lists.
if peers != "" {
c.Peers = trimsplit(peers, ",")
c.Peers = ustrings.TrimSplit(peers, ",")
}
if cors != "" {
c.CorsOrigins = trimsplit(cors, ",")
c.CorsOrigins = ustrings.TrimSplit(cors, ",")
}
return nil
@ -312,7 +314,7 @@ func (c *Config) LoadPeersFile() error {
if err != nil {
return fmt.Errorf("Peers file error: %s", err)
}
c.Peers = trimsplit(string(b), ",")
c.Peers = ustrings.TrimSplit(string(b), ",")
return nil
}
@ -355,8 +357,8 @@ func (c *Config) Reset() error {
}
// Reads the info file from the file system or initializes it based on the config.
func (c *Config) Info() (*Info, error) {
info := &Info{}
func (c *Config) Info() (*server.Info, error) {
info := &server.Info{}
path := filepath.Join(c.DataDir, "info")
// Open info file and read it out.
@ -434,8 +436,8 @@ func (c *Config) Sanitize() error {
}
// TLSInfo retrieves a TLSInfo object for the client server.
func (c *Config) TLSInfo() TLSInfo {
return TLSInfo{
func (c *Config) TLSInfo() server.TLSInfo {
return server.TLSInfo{
CAFile: c.CAFile,
CertFile: c.CertFile,
KeyFile: c.KeyFile,
@ -443,13 +445,13 @@ func (c *Config) TLSInfo() TLSInfo {
}
// ClientTLSConfig generates the TLS configuration for the client server.
func (c *Config) TLSConfig() (TLSConfig, error) {
func (c *Config) TLSConfig() (server.TLSConfig, error) {
return c.TLSInfo().Config()
}
// PeerTLSInfo retrieves a TLSInfo object for the peer server.
func (c *Config) PeerTLSInfo() TLSInfo {
return TLSInfo{
func (c *Config) PeerTLSInfo() server.TLSInfo {
return server.TLSInfo{
CAFile: c.Peer.CAFile,
CertFile: c.Peer.CertFile,
KeyFile: c.Peer.KeyFile,
@ -457,7 +459,7 @@ func (c *Config) PeerTLSInfo() TLSInfo {
}
// PeerTLSConfig generates the TLS configuration for the peer server.
func (c *Config) PeerTLSConfig() (TLSConfig, error) {
func (c *Config) PeerTLSConfig() (server.TLSConfig, error) {
return c.PeerTLSInfo().Config()
}

View File

@ -1,4 +1,4 @@
package server
package config
import (
"io/ioutil"

View File

@ -1,4 +1,4 @@
package server
package config
const (
// The amount of time (in ms) to elapse without a heartbeat before becoming a candidate

View File

@ -27,6 +27,7 @@ import (
"github.com/coreos/etcd/third_party/github.com/coreos/raft"
ehttp "github.com/coreos/etcd/http"
"github.com/coreos/etcd/config"
"github.com/coreos/etcd/log"
"github.com/coreos/etcd/metrics"
"github.com/coreos/etcd/server"
@ -35,7 +36,7 @@ import (
func main() {
// Load configuration.
var config = server.NewConfig()
var config = config.New()
if err := config.Load(os.Args[1:]); err != nil {
fmt.Println(server.Usage() + "\n")
fmt.Println(err.Error() + "\n")

16
pkg/strings/string.go Normal file
View File

@ -0,0 +1,16 @@
package string
import (
"strings"
)
// TrimSplit slices s into all substrings separated by sep and returns a
// slice of the substrings between the separator with all leading and trailing
// white space removed, as defined by Unicode.
func TrimSplit(s, sep string) []string {
trimmed := strings.Split(s, sep)
for i := range trimmed {
trimmed[i] = strings.TrimSpace(trimmed[i])
}
return trimmed
}