Skip to content

Commit

Permalink
Add support for env backend and new -backend flag
Browse files Browse the repository at this point in the history
  • Loading branch information
kelseyhightower committed May 23, 2014
1 parent 9c384a3 commit e955a03
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
24 changes: 20 additions & 4 deletions confd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand All @@ -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())
}
Expand All @@ -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.
Expand Down
36 changes: 36 additions & 0 deletions env/client.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit e955a03

Please sign in to comment.