diff --git a/cli.go b/cli.go index dd291d0..948991f 100644 --- a/cli.go +++ b/cli.go @@ -1,4 +1,5 @@ // SPDX-License-Identifier: BSD-3-Clause + package main import ( diff --git a/cli_test.go b/cli_test.go index dce74a8..b496ce2 100644 --- a/cli_test.go +++ b/cli_test.go @@ -1,4 +1,5 @@ // SPDX-License-Identifier: BSD-3-Clause + package main import ( diff --git a/conf/notwin.go b/conf/notwin.go index 0f61861..7f8aa7a 100644 --- a/conf/notwin.go +++ b/conf/notwin.go @@ -7,7 +7,6 @@ package conf import ( "os" "runtime" - "strings" "github.com/miekg/dns" ) @@ -24,29 +23,3 @@ func GetDNSConfig() (*dns.ClientConfig, error) { return dns.ClientConfigFromFile("/etc/resolv.conf") } } - -// Plan 9 stores its network data in /net/ndb, which seems to be formatted a specific way -func getPlan9Config(str string) (*dns.ClientConfig, error) { - str = strings.ReplaceAll(str, "\n", "") - spl := strings.FieldsFunc(str, split) - servers := []string{} - for _, option := range spl { - if strings.HasPrefix(option, "dns=") { - servers = append(servers, strings.TrimPrefix(option, "dns=")) - } - } - - // TODO: read more about how customizable Plan 9 is - return &dns.ClientConfig{ - Servers: servers, - Search: []string{}, - Port: "53", - Ndots: 1, - Timeout: 5, - Attempts: 1, - }, nil -} - -func split(r rune) bool { - return r == ' ' || r == '\t' -} diff --git a/conf/notwin_test.go b/conf/notwin_test.go deleted file mode 100644 index 5845b99..0000000 --- a/conf/notwin_test.go +++ /dev/null @@ -1,18 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -package conf - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestGetPlan9Config(t *testing.T) { - ndb := `ip=192.168.122.45 ipmask=255.255.255.0 ipgw=192.168.122.1 - sys=chog9 - dns=192.168.122.1` - - act, err := getPlan9Config(ndb) - assert.Nil(t, err) - assert.Equal(t, act.Servers[0], "192.168.122.1") -} diff --git a/conf/plan9.go b/conf/plan9.go new file mode 100644 index 0000000..cb507fb --- /dev/null +++ b/conf/plan9.go @@ -0,0 +1,40 @@ +// SPDX-License-Identifier: BSD-3-Clause + +package conf + +import ( + "fmt" + "strings" + + "github.com/miekg/dns" +) + +// Plan 9 stores its network data in /net/ndb, which seems to be formatted a specific way +// Yoink it and use it. +// +// See ndb(7). +func getPlan9Config(str string) (*dns.ClientConfig, error) { + str = strings.ReplaceAll(str, "\n", "") + spl := strings.FieldsFunc(str, splitChars) + var servers []string + for _, option := range spl { + if strings.HasPrefix(option, "dns=") { + servers = append(servers, strings.TrimPrefix(option, "dns=")) + } + } + if len(servers) == 0 { + return nil, fmt.Errorf("plan9: no DNS servers found") + } + + // TODO: read more about how customizable Plan 9 is + return &dns.ClientConfig{ + Servers: servers, + Search: []string{}, + Port: "53", + }, nil +} + +// Split the string at either space or tabs +func splitChars(r rune) bool { + return r == ' ' || r == '\t' +} diff --git a/conf/plan9_test.go b/conf/plan9_test.go new file mode 100644 index 0000000..fa81fd2 --- /dev/null +++ b/conf/plan9_test.go @@ -0,0 +1,45 @@ +// SPDX-License-Identifier: BSD-3-Clause + +package conf + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetPlan9Config(t *testing.T) { + ndbs := []struct { + in string + want string + }{ + {`ip=192.168.122.45 ipmask=255.255.255.0 ipgw=192.168.122.1 + sys=chog9 + dns=192.168.122.1`, "192.168.122.1"}, + {`ipnet=murray-hill ip=135.104.0.0 ipmask=255.255.0.0 + dns=135.104.10.1 + ntp=ntp.cs.bell-labs.com + ipnet=plan9 ip=135.104.9.0 ipmask=255.255.255.0 + ntp=oncore.cs.bell-labs.com + smtp=smtp1.cs.bell-labs.com + ip=135.104.9.6 sys=anna dom=anna.cs.bell-labs.com + smtp=smtp2.cs.bell-labs.com`, "135.104.10.1"}, + } + + for _, ndb := range ndbs { + act, err := getPlan9Config(ndb.in) + assert.Nil(t, err) + assert.Equal(t, ndb.want, act.Servers[0]) + } + + invalid := `sys = spindle + dom=spindle.research.bell-labs.com + bootf=/mips/9powerboot + ip=135.104.117.32 ether=080069020677 + proto=il` + + act, err := getPlan9Config(invalid) + assert.ErrorContains(t, err, "no DNS servers found") + assert.Nil(t, act) + +} diff --git a/go.mod b/go.mod index 1ce1512..ebe8a60 100644 --- a/go.mod +++ b/go.mod @@ -30,7 +30,7 @@ require ( github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect - golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b // indirect + golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b golang.org/x/text v0.3.7 // indirect golang.org/x/tools v0.1.11 // indirect gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect