Skip to content

Commit

Permalink
Move prefix processing out of client and into template resource
Browse files Browse the repository at this point in the history
  • Loading branch information
armon committed Apr 27, 2014
1 parent a6aa6d8 commit b2af066
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
12 changes: 1 addition & 11 deletions etcd/etcdutil/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"strings"
)

var replacer = strings.NewReplacer("/", "_")

// Client is a wrapper around the etcd client
type Client struct {
client *etcd.Client
Expand Down Expand Up @@ -59,7 +57,7 @@ func (c *Client) GetValues(prefix string, keys []string) (map[string]interface{}
// nodeWalk recursively descends nodes, updating vars.
func nodeWalk(node *etcd.Node, prefix string, vars map[string]interface{}) error {
if node != nil {
key := pathToKey(node.Key, prefix)
key := node.Key
if !node.Dir {
vars[key] = node.Value
} else {
Expand All @@ -71,11 +69,3 @@ func nodeWalk(node *etcd.Node, prefix string, vars map[string]interface{}) error
}
return nil
}

// pathToKey translates etcd key paths into something more suitable for use
// in Golang templates. Turn /prefix/key/subkey into key_subkey.
func pathToKey(key, prefix string) string {
key = strings.TrimPrefix(key, prefix)
key = strings.TrimPrefix(key, "/")
return replacer.Replace(key)
}
25 changes: 23 additions & 2 deletions resource/template/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"path"
"path/filepath"
"strconv"
"strings"
"syscall"
"text/template"

Expand All @@ -23,9 +24,11 @@ import (
"github.com/kelseyhightower/confd/log"
)

var replacer = strings.NewReplacer("/", "_")

// StoreClient is used to swap out the
type StoreClient interface {
GetValues(prefix string, keys []string) (map[string]interface{}, error)
GetValues(keys []string) (map[string]interface{}, error)
}

// TemplateResourceConfig holds the parsed template resource.
Expand Down Expand Up @@ -71,13 +74,31 @@ func (t *TemplateResource) setVars() error {
var err error
log.Debug("Retrieving keys from store")
log.Debug("Key prefix set to " + config.Prefix())
t.Vars, err = t.storeClient.GetValues(config.Prefix(), t.Keys)
vars, err := t.storeClient.GetValues(t.Keys)
if err != nil {
return err
}
t.Vars = t.cleanKeys(vars)
return nil
}

func (t *TemplateResource) cleanKeys(vars map[string]interface{}) map[string]interface{} {
clean := make(map[string]interface{}, len(vars))
prefix := config.Prefix()
for key, val := range vars {
clean[pathToKey(key, prefix)] = val
}
return clean
}

// pathToKey translates etcd key paths into something more suitable for use
// in Golang templates. Turn /prefix/key/subkey into key_subkey.
func pathToKey(key, prefix string) string {
key = strings.TrimPrefix(key, prefix)
key = strings.TrimPrefix(key, "/")
return replacer.Replace(key)
}

// createStageFile stages the src configuration file by processing the src
// template and setting the desired owner, group, and mode. It also sets the
// StageFile for the template resource.
Expand Down

0 comments on commit b2af066

Please sign in to comment.