aboutsummaryrefslogtreecommitdiff
path: root/plugin/nsid/setup.go
blob: 07c6493160c5ccfc78ec941f7bb50fa175a1fc61 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package nsid

import (
	"os"
	"strings"

	"github.com/coredns/caddy"
	"github.com/coredns/coredns/core/dnsserver"
	"github.com/coredns/coredns/plugin"
)

func init() { plugin.Register("nsid", setup) }

func setup(c *caddy.Controller) error {
	nsid, err := nsidParse(c)
	if err != nil {
		return plugin.Error("nsid", err)
	}

	dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
		return Nsid{Next: next, Data: nsid}
	})

	return nil
}

func nsidParse(c *caddy.Controller) (string, error) {
	// Use hostname as the default
	nsid, err := os.Hostname()
	if err != nil {
		nsid = "localhost"
	}
	i := 0
	for c.Next() {
		if i > 0 {
			return nsid, plugin.ErrOnce
		}
		i++
		args := c.RemainingArgs()
		if len(args) > 0 {
			nsid = strings.Join(args, " ")
		}
	}
	return nsid, nil
}