Skip to content

Commit

Permalink
yaml/json file backend support
Browse files Browse the repository at this point in the history
  • Loading branch information
HeavyHorst committed Feb 23, 2016
1 parent 710bf63 commit e8fb2ba
Show file tree
Hide file tree
Showing 35 changed files with 11,868 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions backends/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/kelseyhightower/confd/backends/redis"
"github.com/kelseyhightower/confd/backends/stackengine"
"github.com/kelseyhightower/confd/backends/vault"
"github.com/kelseyhightower/confd/backends/yaml"
"github.com/kelseyhightower/confd/backends/zookeeper"
"github.com/kelseyhightower/confd/log"
)
Expand Down Expand Up @@ -47,6 +48,8 @@ func New(config Config) (StoreClient, error) {
return redis.NewRedisClient(backendNodes)
case "env":
return env.NewEnvClient()
case "yaml":
return yaml.NewYamlClient(config.YAMLFile)
case "vault":
vaultConfig := map[string]string{
"app-id": config.AppID,
Expand Down
1 change: 1 addition & 0 deletions backends/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ type Config struct {
Username string
AppID string
UserID string
YAMLFile string
}
91 changes: 91 additions & 0 deletions backends/yaml/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package yaml

import (
"fmt"
"io/ioutil"
"strings"

"github.com/fsnotify/fsnotify"
"github.com/kelseyhightower/confd/log"
"gopkg.in/yaml.v2"
)

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

// Client provides a shell for the yaml client
type Client struct {
filepath string
}

func NewYamlClient(filepath string) (*Client, error) {
return &Client{filepath}, nil
}

func (c *Client) GetValues(keys []string) (map[string]string, error) {
yamlMap := make(map[string]string)
vars := make(map[string]string)

data, err := ioutil.ReadFile(c.filepath)
if err != nil {
return vars, err
}
err = yaml.Unmarshal(data, &yamlMap)
if err != nil {
return vars, err
}

for _, key := range keys {
k := transform(key)
for yamlKey, yamlValue := range yamlMap {
if strings.HasPrefix(yamlKey, k) {
vars[clean(yamlKey)] = yamlValue
}
}
}
log.Debug(fmt.Sprintf("Key Map: %#v", vars))

return vars, nil
}

func transform(key string) string {
k := strings.TrimPrefix(key, "/")
return strings.ToUpper(replacer.Replace(k))
}

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

func clean(key string) string {
newKey := "/" + key
return cleanReplacer.Replace(strings.ToLower(newKey))
}

func (c *Client) WatchPrefix(prefix string, waitIndex uint64, stopChan chan bool) (uint64, error) {
if waitIndex == 0 {
return 1, nil
}

watcher, err := fsnotify.NewWatcher()
if err != nil {
return 0, err
}
defer watcher.Close()

err = watcher.Add(c.filepath)
if err != nil {
return 0, err
}

for {
select {
case event := <-watcher.Events:
if event.Op&fsnotify.Write == fsnotify.Write {
return 0, nil
}
case err := <-watcher.Errors:
return 0, err
case <-stopChan:
return 0, nil
}
}
return waitIndex, nil
}
6 changes: 6 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ var (
watch bool
appID string
userID string
yamlFile string
)

// A Config structure is used to configure confd.
Expand Down Expand Up @@ -76,6 +77,7 @@ type Config struct {
Watch bool `toml:"watch"`
AppID string `toml:"app_id"`
UserID string `toml:"user_id"`
YAMLFile string `toml:"yaml_file"`
}

func init() {
Expand All @@ -87,6 +89,7 @@ func init() {
flag.StringVar(&clientKey, "client-key", "", "the client key")
flag.StringVar(&confdir, "confdir", "/etc/confd", "confd conf directory")
flag.StringVar(&configFile, "config-file", "", "the confd config file")
flag.StringVar(&yamlFile, "yaml-file", "", "yaml file")
flag.IntVar(&interval, "interval", 600, "backend polling interval")
flag.BoolVar(&keepStageFile, "keep-stage-file", false, "keep staged files")
flag.StringVar(&logLevel, "log-level", "", "level which confd should log messages")
Expand Down Expand Up @@ -218,6 +221,7 @@ func initConfig() error {
Username: config.Username,
AppID: config.AppID,
UserID: config.UserID,
YAMLFile: config.YAMLFile,
}
// Template configuration.
templateConfig = template.Config{
Expand Down Expand Up @@ -319,5 +323,7 @@ func setConfigFromFlag(f *flag.Flag) {
config.AppID = appID
case "user-id":
config.UserID = userID
case "yaml-file":
config.YAMLFile = yamlFile
}
}
6 changes: 6 additions & 0 deletions vendor/github.com/fsnotify/fsnotify/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions vendor/github.com/fsnotify/fsnotify/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions vendor/github.com/fsnotify/fsnotify/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e8fb2ba

Please sign in to comment.