feat: support Caddyfile
This commit is contained in:
parent
1e595f787c
commit
deaad3f695
1 changed files with 52 additions and 0 deletions
52
stub.go
52
stub.go
|
|
@ -2,6 +2,7 @@ package stub
|
|||
|
||||
import (
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||
)
|
||||
|
||||
type StubDNS struct {
|
||||
|
|
@ -22,3 +23,54 @@ func (StubDNS) CaddyModule() caddy.ModuleInfo {
|
|||
New: func() caddy.Module {return &StubDNS{}},
|
||||
}
|
||||
}
|
||||
|
||||
// Provision sets up the module. Implements caddy.Provisioner.
|
||||
func (p *StubDNS) Provision(ctx caddy.Context) error {
|
||||
repl := caddy.NewReplacer()
|
||||
p.Address = repl.ReplaceAll(p.Address, "")
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalCaddyfile sets up the DNS provider from Caddyfile tokens. Syntax:
|
||||
//
|
||||
// stub_dns [address] {
|
||||
// address <address>
|
||||
// }
|
||||
//
|
||||
func (s *StubDNS) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
||||
for d.Next() {
|
||||
if d.NextArg() {
|
||||
s.Address = d.Val()
|
||||
}
|
||||
if d.NextArg() {
|
||||
return d.ArgErr()
|
||||
}
|
||||
for nesting := d.Nesting(); d.NextBlock(nesting); {
|
||||
switch d.Val() {
|
||||
case "address":
|
||||
if s.Address != "" {
|
||||
return d.Err("Address already set")
|
||||
}
|
||||
if d.NextArg() {
|
||||
s.Address = d.Val()
|
||||
}
|
||||
if d.NextArg() {
|
||||
return d.ArgErr()
|
||||
}
|
||||
default:
|
||||
return d.Errf("unrecognized subdirective '%s'", d.Val())
|
||||
}
|
||||
}
|
||||
}
|
||||
if s.Address == "" {
|
||||
return d.Err("missing Address")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
// Interface guards
|
||||
var (
|
||||
_ caddy.Provisioner = (*StubDNS)(nil)
|
||||
_ caddyfile.Unmarshaler = (*StubDNS)(nil)
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue