From f56965b1c03d190bafb70455188803152268acff Mon Sep 17 00:00:00 2001 From: Brandon Philips Date: Mon, 27 Jan 2014 15:03:01 -0800 Subject: [PATCH] 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. --- {server => config}/config.go | 32 ++++++++++++++++--------------- {server => config}/config_test.go | 2 +- {server => config}/timeout.go | 2 +- etcd.go | 3 ++- pkg/strings/string.go | 16 ++++++++++++++++ 5 files changed, 37 insertions(+), 18 deletions(-) rename {server => config}/config.go (95%) rename {server => config}/config_test.go (99%) rename {server => config}/timeout.go (93%) create mode 100644 pkg/strings/string.go diff --git a/server/config.go b/config/config.go similarity index 95% rename from server/config.go rename to config/config.go index da4bd4cf2..8aa534317 100644 --- a/server/config.go +++ b/config/config.go @@ -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() } diff --git a/server/config_test.go b/config/config_test.go similarity index 99% rename from server/config_test.go rename to config/config_test.go index 0cbee1217..5eb390511 100644 --- a/server/config_test.go +++ b/config/config_test.go @@ -1,4 +1,4 @@ -package server +package config import ( "io/ioutil" diff --git a/server/timeout.go b/config/timeout.go similarity index 93% rename from server/timeout.go rename to config/timeout.go index 9d117f9a2..551635e07 100644 --- a/server/timeout.go +++ b/config/timeout.go @@ -1,4 +1,4 @@ -package server +package config const ( // The amount of time (in ms) to elapse without a heartbeat before becoming a candidate diff --git a/etcd.go b/etcd.go index 8a81ed23a..5edee4c16 100644 --- a/etcd.go +++ b/etcd.go @@ -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") diff --git a/pkg/strings/string.go b/pkg/strings/string.go new file mode 100644 index 000000000..5d898e43a --- /dev/null +++ b/pkg/strings/string.go @@ -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 +}