2018-02-23 15:02:05 +00:00
|
|
|
// Package parse contains functions that can be used in the setup code for plugins.
|
2017-12-13 10:18:08 -06:00
|
|
|
package parse
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
|
2018-09-19 08:16:04 +01:00
|
|
|
"github.com/coredns/coredns/plugin/pkg/transport"
|
|
|
|
|
|
2019-07-03 09:04:47 +08:00
|
|
|
"github.com/caddyserver/caddy"
|
2017-12-13 10:18:08 -06:00
|
|
|
)
|
|
|
|
|
|
2020-07-08 09:00:26 -07:00
|
|
|
// Transfer parses transfer statements: 'transfer [to|from] [address...]'.
|
|
|
|
|
func Transfer(c *caddy.Controller, secondary bool) (tos, froms []string, err error) {
|
2017-12-13 10:18:08 -06:00
|
|
|
if !c.NextArg() {
|
2020-07-08 09:00:26 -07:00
|
|
|
return nil, nil, c.ArgErr()
|
2017-12-13 10:18:08 -06:00
|
|
|
}
|
|
|
|
|
value := c.Val()
|
|
|
|
|
switch value {
|
2020-07-08 09:00:26 -07:00
|
|
|
case "to":
|
|
|
|
|
tos = c.RemainingArgs()
|
|
|
|
|
for i := range tos {
|
|
|
|
|
if tos[i] != "*" {
|
|
|
|
|
normalized, err := HostPort(tos[i], transport.Port)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
tos[i] = normalized
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-13 10:18:08 -06:00
|
|
|
case "from":
|
2020-07-08 09:00:26 -07:00
|
|
|
if !secondary {
|
|
|
|
|
return nil, nil, fmt.Errorf("can't use `transfer from` when not being a secondary")
|
2020-07-07 21:38:07 +02:00
|
|
|
}
|
2020-07-08 09:00:26 -07:00
|
|
|
froms = c.RemainingArgs()
|
2017-12-13 10:18:08 -06:00
|
|
|
for i := range froms {
|
|
|
|
|
if froms[i] != "*" {
|
2018-09-19 08:16:04 +01:00
|
|
|
normalized, err := HostPort(froms[i], transport.Port)
|
2017-12-13 10:18:08 -06:00
|
|
|
if err != nil {
|
2020-07-08 09:00:26 -07:00
|
|
|
return nil, nil, err
|
2017-12-13 10:18:08 -06:00
|
|
|
}
|
|
|
|
|
froms[i] = normalized
|
|
|
|
|
} else {
|
2020-07-08 09:00:26 -07:00
|
|
|
return nil, nil, fmt.Errorf("can't use '*' in transfer from")
|
2017-12-13 10:18:08 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-08 09:00:26 -07:00
|
|
|
return
|
2017-12-13 10:18:08 -06:00
|
|
|
}
|