Get finds the first value in the configuration that matches the alias and contains key. Get returns the empty string if no value was found, or if the Config contains an invalid conditional Include value. The match for key is case insensitive.
(alias, key string)
| 344 | // |
| 345 | // The match for key is case insensitive. |
| 346 | func (c *Config) Get(alias, key string) (string, error) { |
| 347 | lowerKey := strings.ToLower(key) |
| 348 | for _, host := range c.Hosts { |
| 349 | if !host.Matches(alias) { |
| 350 | continue |
| 351 | } |
| 352 | for _, node := range host.Nodes { |
| 353 | switch t := node.(type) { |
| 354 | case *Empty: |
| 355 | continue |
| 356 | case *KV: |
| 357 | // "keys are case insensitive" per the spec |
| 358 | lkey := strings.ToLower(t.Key) |
| 359 | if lkey == "match" { |
| 360 | panic("can't handle Match directives") |
| 361 | } |
| 362 | if lkey == lowerKey { |
| 363 | return t.Value, nil |
| 364 | } |
| 365 | case *Include: |
| 366 | val := t.Get(alias, key) |
| 367 | if val != "" { |
| 368 | return val, nil |
| 369 | } |
| 370 | default: |
| 371 | return "", fmt.Errorf("unknown Node type %v", t) |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | return "", nil |
| 376 | } |
| 377 | |
| 378 | // GetAll returns all values in the configuration that match the alias and |
| 379 | // contains key, or nil if none are present. |