本文整理汇总了Golang中github.com/hashicorp/consul-template/dependency.ParseStoreKeyPrefix函数的典型用法代码示例。如果您正苦于以下问题:Golang ParseStoreKeyPrefix函数的具体用法?Golang ParseStoreKeyPrefix怎么用?Golang ParseStoreKeyPrefix使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ParseStoreKeyPrefix函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestTreeFunc_missingData
func TestTreeFunc_missingData(t *testing.T) {
d, err := dep.ParseStoreKeyPrefix("non-existing")
if err != nil {
t.Fatal(err)
}
brain := NewBrain()
used := make(map[string]dep.Dependency)
missing := make(map[string]dep.Dependency)
f := treeFunc(brain, used, missing)
result, err := f("non-existing")
if err != nil {
t.Fatal(err)
}
expected := []*dep.KeyPair{}
if !reflect.DeepEqual(result, expected) {
t.Errorf("expected %q to be %q", result, expected)
}
if _, ok := used[d.HashCode()]; !ok {
t.Errorf("expected dep to be used")
}
if _, ok := missing[d.HashCode()]; !ok {
t.Errorf("expected dep to be missing")
}
}
示例2: TestParseFlags_prefixes
func TestParseFlags_prefixes(t *testing.T) {
cli := NewCLI(ioutil.Discard, ioutil.Discard)
config, _, _, err := cli.parseFlags([]string{
"-prefix", "[email protected]:backup",
})
if err != nil {
t.Fatal(err)
}
if len(config.Prefixes) != 1 {
t.Fatal("expected 1 prefix")
}
d, err := dep.ParseStoreKeyPrefix("[email protected]")
if err != nil {
t.Fatal(err)
}
expected := &Prefix{
Source: d,
SourceRaw: "[email protected]",
Destination: "backup",
}
if !reflect.DeepEqual(config.Prefixes[0], expected) {
t.Errorf("expected %q to be %q", config.Prefixes[0], expected)
}
}
示例3: TestReceive_receivesData
func TestReceive_receivesData(t *testing.T) {
prefix, err := dep.ParseStoreKeyPrefix("foo/bar")
if err != nil {
t.Fatal(err)
}
config := testConfig(`
prefix {
path = "foo/bar"
}
`, t)
runner, err := NewRunner(config, []string{"env"}, true)
if err != nil {
t.Fatal(err)
}
runner.outStream, runner.errStream = ioutil.Discard, ioutil.Discard
data := []*dep.KeyPair{&dep.KeyPair{Path: "foo/bar"}}
runner.Receive(prefix, data)
if !reflect.DeepEqual(runner.data[prefix.HashCode()], data) {
t.Errorf("expected %#v to be %#v", runner.data[prefix.HashCode()], data)
}
}
示例4: treeFunc
// treeFunc returns or accumulates keyPrefix dependencies.
func treeFunc(brain *Brain,
used, missing map[string]dep.Dependency) func(string) ([]*dep.KeyPair, error) {
return func(s string) ([]*dep.KeyPair, error) {
result := make([]*dep.KeyPair, 0)
if len(s) == 0 {
return result, nil
}
d, err := dep.ParseStoreKeyPrefix(s)
if err != nil {
return result, err
}
addDependency(used, d)
// Only return non-empty top-level keys
if value, ok := brain.Recall(d); ok {
for _, pair := range value.([]*dep.KeyPair) {
parts := strings.Split(pair.Key, "/")
if parts[len(parts)-1] != "" {
result = append(result, pair)
}
}
return result, nil
}
addDependency(missing, d)
return result, nil
}
}
示例5: ParsePrefix
// ParsePrefix parses a prefix of the format "[email protected]:destination" into the
// Prefix component.
func ParsePrefix(s string) (*Prefix, error) {
if len(strings.TrimSpace(s)) < 1 {
return nil, fmt.Errorf("cannot specify empty prefix declaration")
}
var sourceRaw, destination string
parts := prefixRe.FindAllString(s, -1)
switch len(parts) {
case 1:
sourceRaw = parts[0]
case 2:
sourceRaw, destination = parts[0], parts[1]
default:
return nil, fmt.Errorf("invalid prefix declaration format")
}
source, err := dep.ParseStoreKeyPrefix(sourceRaw)
if err != nil {
return nil, err
}
if destination == "" {
destination = source.Prefix
}
// ensure destination prefix ends with "/"
destination = strings.TrimSuffix(destination, "/") + "/"
return &Prefix{
Source: source,
SourceRaw: sourceRaw,
Destination: destination,
}, nil
}
示例6: TestRun_doublePass
func TestRun_doublePass(t *testing.T) {
in := test.CreateTempfile([]byte(`
{{ range ls "services" }}
{{ range service .Key }}
{{.Node}} {{.Address}}:{{.Port}}
{{ end }}
{{ end }}
`), t)
defer test.DeleteTempfile(in, t)
config := DefaultConfig()
config.Merge(&Config{
ConfigTemplates: []*ConfigTemplate{
&ConfigTemplate{Source: in.Name()},
},
})
runner, err := NewRunner(config, true, false)
if err != nil {
t.Fatal(err)
}
if len(runner.dependencies) != 0 {
t.Errorf("expected %d to be %d", len(runner.dependencies), 0)
}
if err := runner.Run(); err != nil {
t.Fatal(err)
}
if len(runner.dependencies) != 1 {
t.Errorf("expected %d to be %d", len(runner.dependencies), 1)
}
d, err := dep.ParseStoreKeyPrefix("services")
if err != nil {
t.Fatal(err)
}
data := []*dep.KeyPair{
&dep.KeyPair{Key: "service1"},
&dep.KeyPair{Key: "service2"},
&dep.KeyPair{Key: "service3"},
}
runner.Receive(d, data)
if err := runner.Run(); err != nil {
t.Fatal(err)
}
if len(runner.dependencies) != 4 {
t.Errorf("expected %d to be %d", len(runner.dependencies), 4)
}
}
示例7: TestMerge_prefixes
func TestMerge_prefixes(t *testing.T) {
global, err := dep.ParseStoreKeyPrefix("global/time")
if err != nil {
t.Fatal(err)
}
redis, err := dep.ParseStoreKeyPrefix("config/redis")
if err != nil {
t.Fatal(err)
}
config := testConfig(`
prefixes = ["global/time"]
`, t)
config.Merge(testConfig(`
prefixes = ["config/redis"]
`, t))
expected := []*dep.StoreKeyPrefix{global, redis}
if !reflect.DeepEqual(config.Prefixes, expected) {
t.Errorf("expected \n\n%#v\n\n to be \n\n%#v\n\n", config.Prefixes, expected)
}
}
示例8: TestParseFlags_prefix
func TestParseFlags_prefix(t *testing.T) {
cli := NewCLI(ioutil.Discard, ioutil.Discard)
config, _, _, _, err := cli.parseFlags([]string{
"-prefix", "global",
})
if err != nil {
t.Fatal(err)
}
expected, err := dep.ParseStoreKeyPrefix("global")
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(config.Prefixes[0], expected) {
t.Errorf("expected %#v to be %#v", config.Prefixes[0], expected)
}
}
示例9: TestRun_sanitize
func TestRun_sanitize(t *testing.T) {
prefix, err := dep.ParseStoreKeyPrefix("foo/bar")
if err != nil {
t.Fatal(err)
}
config := testConfig(`
sanitize = true
prefixes = ["foo/bar"]
`, t)
runner, err := NewRunner(config, []string{"env"}, true)
if err != nil {
t.Fatal(err)
}
outStream, errStream := new(bytes.Buffer), new(bytes.Buffer)
runner.outStream, runner.errStream = outStream, errStream
pair := []*dep.KeyPair{
&dep.KeyPair{
Path: "foo/bar",
Key: "b*a*r",
Value: "baz",
},
}
runner.Receive(prefix, pair)
exitCh, err := runner.Run()
if err != nil {
t.Fatal(err)
}
select {
case err := <-runner.ErrCh:
t.Fatal(err)
case <-exitCh:
expected := "b_a_r=baz"
if !strings.Contains(outStream.String(), expected) {
t.Fatalf("expected %q to include %q", outStream.String(), expected)
}
}
}
示例10: TestParsePrefix_source
func TestParsePrefix_source(t *testing.T) {
source := "global"
prefix, err := ParsePrefix(source)
if err != nil {
t.Fatal(err)
}
if prefix.SourceRaw != source {
t.Errorf("expected %q to equal %q", prefix.SourceRaw, source)
}
d, err := dep.ParseStoreKeyPrefix("global")
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(prefix.Source, d) {
t.Errorf("expected %#v to equal %#v", prefix.Source, d)
}
}
示例11: TestTreeFunc_hasData
func TestTreeFunc_hasData(t *testing.T) {
d, err := dep.ParseStoreKeyPrefix("existing")
if err != nil {
t.Fatal(err)
}
data := []*dep.KeyPair{
&dep.KeyPair{Key: "", Value: ""},
&dep.KeyPair{Key: "user/sethvargo", Value: "true"},
&dep.KeyPair{Key: "maxconns", Value: "11"},
&dep.KeyPair{Key: "minconns", Value: "2"},
}
brain := NewBrain()
brain.Remember(d, data)
used := make(map[string]dep.Dependency)
missing := make(map[string]dep.Dependency)
f := treeFunc(brain, used, missing)
result, err := f("existing")
if err != nil {
t.Fatal(err)
}
expected := []*dep.KeyPair{
&dep.KeyPair{Key: "user/sethvargo", Value: "true"},
&dep.KeyPair{Key: "maxconns", Value: "11"},
&dep.KeyPair{Key: "minconns", Value: "2"},
}
if !reflect.DeepEqual(result, expected) {
t.Errorf("expected %q to be %q", result, expected)
}
if len(missing) != 0 {
t.Errorf("expected missing to have 0 elements, but had %d", len(missing))
}
if _, ok := used[d.HashCode()]; !ok {
t.Errorf("expected dep to be used")
}
}
示例12: TestRun_exitCh
func TestRun_exitCh(t *testing.T) {
prefix, err := dep.ParseStoreKeyPrefix("foo/bar")
if err != nil {
t.Fatal(err)
}
config := testConfig(`
prefixes = ["foo/bar"]
`, t)
runner, err := NewRunner(config, []string{"env"}, true)
if err != nil {
t.Fatal(err)
}
outStream, errStream := new(bytes.Buffer), new(bytes.Buffer)
runner.outStream, runner.errStream = outStream, errStream
pair := []*dep.KeyPair{
&dep.KeyPair{
Path: "foo/bar",
Key: "bar",
Value: "baz",
},
}
runner.Receive(prefix, pair)
exitCh, err := runner.Run()
if err != nil {
t.Fatal(err)
}
select {
case err := <-runner.ErrCh:
t.Fatal(err)
case <-exitCh:
// Ok
}
}
示例13: NewRunner
// NewRunner accepts a JsonExport, and boolean value for once mode.
func NewRunner(config *JsonExport, once bool) (*Runner, error) {
var err error
log.Printf("[INFO] (runner) creating new runner (once: %v)\n", once)
runner := &Runner{
config: config,
once: once,
}
s := strings.TrimPrefix(config.Prefix, "/")
runner.Prefix, err = dep.ParseStoreKeyPrefix(s)
if err != nil {
return nil, err
}
if err := runner.init(); err != nil {
return nil, err
}
return runner, nil
}
示例14: TestParsePrefix_destination
func TestParsePrefix_destination(t *testing.T) {
source, destination := "[email protected]", "backup"
prefix, err := ParsePrefix(fmt.Sprintf("%s:%s", source, destination))
if err != nil {
t.Fatal(err)
}
if prefix.SourceRaw != source {
t.Errorf("expected %q to equal %q", prefix.SourceRaw, source)
}
d, err := dep.ParseStoreKeyPrefix("[email protected]")
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(prefix.Source, d) {
t.Errorf("expected %#v to equal %#v", prefix.Source, d)
}
if prefix.Destination != destination {
t.Errorf("expected %q to equal %q", prefix.Destination, destination)
}
}
示例15: ParseConfig
// ParseConfig reads the configuration file at the given path and returns a new
// Config struct with the data populated.
func ParseConfig(path string) (*Config, error) {
var errs *multierror.Error
// Read the contents of the file
contents, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("error reading config at %q: %s", path, err)
}
// Parse the file (could be HCL or JSON)
var shadow interface{}
if err := hcl.Decode(&shadow, string(contents)); err != nil {
return nil, fmt.Errorf("error decoding config at %q: %s", path, err)
}
// Convert to a map and flatten the keys we want to flatten
parsed, ok := shadow.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("error converting config at %q", path)
}
flattenKeys(parsed, []string{"auth", "ssl", "syslog"})
// Parse the prefixes
if raw, ok := parsed["prefixes"]; ok {
if typed, ok := raw.([]interface{}); !ok {
err = fmt.Errorf("error converting prefixes to []interface{} at %q, was %T", path, raw)
errs = multierror.Append(errs, err)
delete(parsed, "prefixes")
} else {
prefixes := make([]*dep.StoreKeyPrefix, 0, len(typed))
for _, p := range typed {
if s, ok := p.(string); ok {
if prefix, err := dep.ParseStoreKeyPrefix(s); err != nil {
err = fmt.Errorf("error parsing prefix %q at %q: %s", p, path, err)
errs = multierror.Append(errs, err)
} else {
prefixes = append(prefixes, prefix)
}
} else {
err = fmt.Errorf("error converting %T to string", p)
errs = multierror.Append(errs, err)
delete(parsed, "prefixes")
}
}
parsed["prefixes"] = prefixes
}
}
// Parse the wait component
if raw, ok := parsed["wait"]; ok {
if typed, ok := raw.(string); !ok {
err = fmt.Errorf("error converting wait to string at %q", path)
errs = multierror.Append(errs, err)
delete(parsed, "wait")
} else {
if wait, err := watch.ParseWait(typed); err != nil {
err = fmt.Errorf("error parsing wait at %q: %s", path, err)
errs = multierror.Append(errs, err)
delete(parsed, "wait")
} else {
parsed["wait"] = map[string]time.Duration{
"min": wait.Min,
"max": wait.Max,
}
}
}
}
// Create a new, empty config
config := new(Config)
// Use mapstructure to populate the basic config fields
metadata := new(mapstructure.Metadata)
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
DecodeHook: mapstructure.ComposeDecodeHookFunc(
mapstructure.StringToSliceHookFunc(","),
mapstructure.StringToTimeDurationHookFunc(),
),
ErrorUnused: true,
Metadata: metadata,
Result: config,
})
if err != nil {
errs = multierror.Append(errs, err)
return nil, errs.ErrorOrNil()
}
if err := decoder.Decode(parsed); err != nil {
errs = multierror.Append(errs, err)
return nil, errs.ErrorOrNil()
}
// Store a reference to the path where this config was read from
config.Path = path
// Update the list of set keys
if config.setKeys == nil {
config.setKeys = make(map[string]struct{})
}
//.........这里部分代码省略.........