forked from kirk-enterprise/confd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'HeavyHorst/master'
- Loading branch information
Showing
37 changed files
with
11,906 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,5 @@ type Config struct { | |
Username string | ||
AppID string | ||
UserID string | ||
YAMLFile string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package file | ||
|
||
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 NewFileClient(filepath string) (*Client, error) { | ||
return &Client{filepath}, nil | ||
} | ||
|
||
func (c *Client) GetValues(keys []string) (map[string]string, error) { | ||
yamlMap := make(map[interface{}]interface{}) | ||
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 | ||
} | ||
|
||
nodeWalk(yamlMap, "", vars) | ||
log.Debug(fmt.Sprintf("Key Map: %#v", vars)) | ||
|
||
return vars, nil | ||
} | ||
|
||
// nodeWalk recursively descends nodes, updating vars. | ||
func nodeWalk(node map[interface{}]interface{}, key string, vars map[string]string) error { | ||
for k, v := range node { | ||
key := key + "/" + k.(string) | ||
|
||
switch v.(type) { | ||
case map[interface{}]interface{}: | ||
nodeWalk(v.(map[interface{}]interface{}), key, vars) | ||
case []interface{}: | ||
for _, j := range v.([]interface{}) { | ||
switch j.(type) { | ||
case map[interface{}]interface{}: | ||
nodeWalk(j.(map[interface{}]interface{}), key, vars) | ||
case string: | ||
vars[key+"/"+j.(string)] = "" | ||
} | ||
} | ||
case string: | ||
vars[key] = v.(string) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (c *Client) WatchPrefix(prefix string, keys []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 1, nil | ||
} | ||
case err := <-watcher.Errors: | ||
return 0, err | ||
case <-stopChan: | ||
return 0, nil | ||
} | ||
} | ||
return waitIndex, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/bin/bash | ||
|
||
cat <<EOT >> test.yaml | ||
key: foobar | ||
database: | ||
- host: 127.0.0.1 | ||
- password: p@sSw0rd | ||
- port: "3306" | ||
- username: confd | ||
upstream: | ||
- app1: 10.0.1.10:8080 | ||
- app2: 10.0.1.11:8080 | ||
prefix: | ||
database: | ||
- host: 127.0.0.1 | ||
- password: p@sSw0rd | ||
- port: "3306" | ||
- username: confd | ||
upstream: | ||
app1: 10.0.1.10:8080 | ||
app2: 10.0.1.11:8080 | ||
EOT | ||
|
||
# Run confd | ||
confd --onetime --log-level debug --confdir ./integration/confdir --backend file --file test.yaml --watch |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.