diff options
| author | Shulhan <ms@kilabit.info> | 2019-02-07 13:54:47 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2019-02-07 13:54:47 +0700 |
| commit | a52f3e613c490366f1c867ca297ba46be0ea81c3 (patch) | |
| tree | f28f3f3a65ca915603b8bf2c58655890128c0c6d /lib | |
| parent | c05eade29b6f506115870bc50a548bb9d0ac3e71 (diff) | |
| download | pakakeh.go-a52f3e613c490366f1c867ca297ba46be0ea81c3.tar.xz | |
dns: add function to get list of system name servers
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/dns/funcs.go | 26 | ||||
| -rw-r--r-- | lib/dns/funcs_test.go | 31 | ||||
| -rw-r--r-- | lib/dns/testdata/resolv.conf | 1 |
3 files changed, 58 insertions, 0 deletions
diff --git a/lib/dns/funcs.go b/lib/dns/funcs.go new file mode 100644 index 00000000..e2ab2468 --- /dev/null +++ b/lib/dns/funcs.go @@ -0,0 +1,26 @@ +// Copyright 2019, Shulhan <ms@kilabit.info>. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package dns + +import ( + libnet "github.com/shuLhan/share/lib/net" +) + +// +// GetSystemNameServers return list of system name servers by reading +// resolv.conf formatted file in path. +// +// Default path is "/etc/resolv.conf". +// +func GetSystemNameServers(path string) []string { + if len(path) == 0 { + path = "/etc/resolv.conf" + } + rc, err := libnet.NewResolvConf(path) + if err != nil { + return nil + } + return rc.NameServers +} diff --git a/lib/dns/funcs_test.go b/lib/dns/funcs_test.go new file mode 100644 index 00000000..fb9e2946 --- /dev/null +++ b/lib/dns/funcs_test.go @@ -0,0 +1,31 @@ +// Copyright 2019, Shulhan <ms@kilabit.info>. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package dns + +import ( + "testing" + + "github.com/shuLhan/share/lib/test" +) + +func TestGetSystemNameServers(t *testing.T) { + cases := []struct { + path string + exp []string + }{{ + path: "testdata/resolv.conf", + exp: []string{ + "127.0.0.1", + }, + }} + + for _, c := range cases { + t.Log(c.path) + + got := GetSystemNameServers(c.path) + + test.Assert(t, "NameServers", c.exp, got, true) + } +} diff --git a/lib/dns/testdata/resolv.conf b/lib/dns/testdata/resolv.conf new file mode 100644 index 00000000..bbc8559c --- /dev/null +++ b/lib/dns/testdata/resolv.conf @@ -0,0 +1 @@ +nameserver 127.0.0.1 |
