-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
710bf63
commit e8fb2ba
Showing
35 changed files
with
11,868 additions
and
0 deletions.
There are no files selected for viewing
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,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 | ||
} |
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.
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.