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

View File

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

View File

@ -1,4 +1,4 @@
package server package config
const ( const (
// The amount of time (in ms) to elapse without a heartbeat before becoming a candidate // 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" "github.com/coreos/etcd/third_party/github.com/coreos/raft"
ehttp "github.com/coreos/etcd/http" ehttp "github.com/coreos/etcd/http"
"github.com/coreos/etcd/config"
"github.com/coreos/etcd/log" "github.com/coreos/etcd/log"
"github.com/coreos/etcd/metrics" "github.com/coreos/etcd/metrics"
"github.com/coreos/etcd/server" "github.com/coreos/etcd/server"
@ -35,7 +36,7 @@ import (
func main() { func main() {
// Load configuration. // Load configuration.
var config = server.NewConfig() var config = config.New()
if err := config.Load(os.Args[1:]); err != nil { if err := config.Load(os.Args[1:]); err != nil {
fmt.Println(server.Usage() + "\n") fmt.Println(server.Usage() + "\n")
fmt.Println(err.Error() + "\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
}