diff --git a/confd.go b/confd.go index 088ce5da5..0c042245c 100644 --- a/confd.go +++ b/confd.go @@ -8,9 +8,11 @@ import ( "os" "strings" "time" + "errors" "github.com/kelseyhightower/confd/config" "github.com/kelseyhightower/confd/consul" + "github.com/kelseyhightower/confd/env" "github.com/kelseyhightower/confd/etcd/etcdutil" "github.com/kelseyhightower/confd/log" "github.com/kelseyhightower/confd/resource/template" @@ -20,11 +22,13 @@ var ( configFile = "" defaultConfigFile = "/etc/confd/confd.toml" onetime bool + backend = "" ) func init() { flag.StringVar(&configFile, "config-file", "", "the confd config file") flag.BoolVar(&onetime, "onetime", false, "run once and exit") + flag.StringVar(&backend, "backend", "", "backend to use") } func main() { @@ -50,7 +54,7 @@ func main() { log.Notice("Starting confd") // Create the storage client - store, err := createStoreClient() + store, err := createStoreClient(backend) if err != nil { log.Fatal(err.Error()) } @@ -71,16 +75,28 @@ func main() { // createStoreClient is used to create a storage client based // on our configuration. Either an etcd or Consul client. -func createStoreClient() (template.StoreClient, error) { - if config.Consul() { +func createStoreClient(backend string) (template.StoreClient, error) { + if backend == "" { + if config.Consul() { + backend = "consul" + } else { + backend = "etcd" + } + } + log.Notice("Backend set to " + backend) + switch backend { + case "consul": log.Notice("Consul address set to " + config.ConsulAddr()) return consul.NewConsulClient(config.ConsulAddr()) - } else { + case "etcd": // Create the etcd client upfront and use it for the life of the process. // The etcdClient is an http.Client and designed to be reused. log.Notice("etcd nodes set to " + strings.Join(config.EtcdNodes(), ", ")) return etcdutil.NewEtcdClient(config.EtcdNodes(), config.ClientCert(), config.ClientKey(), config.ClientCaKeys()) + case "env": + return env.NewEnvClient() } + return nil, errors.New("Invalid backend") } // IsFileExist reports whether path exits. diff --git a/env/client.go b/env/client.go new file mode 100644 index 000000000..931b503d9 --- /dev/null +++ b/env/client.go @@ -0,0 +1,36 @@ +package env + +import ( + "os" + "strings" +) + +var replacer = strings.NewReplacer("/", "_") + +// Client provides a wrapper around the consulkv client +type Client struct {} + +// NewEnvClient returns a new client +func NewEnvClient() (*Client, error) { + return &Client{}, nil +} + +// GetValues queries the environment for keys +func (c *Client) GetValues(keys []string) (map[string]interface{}, error) { + vars := make(map[string]interface{}) + for _, key := range keys { + k := transform(key) + value := os.Getenv(k) + if value != "" { + vars[key] = value + } + } + return vars, nil +} + +func transform(key string) (string) { + k := strings.TrimPrefix(key, "/") + k = replacer.Replace(k) + k = strings.ToUpper(k) + return k +}