mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-02-03 22:18:39 +00:00
all: workaround problem with EdgeOS dnsmasq config
This commit is contained in:
committed by
Cuong Manh Le
parent
7af59ee589
commit
a4edf266f0
30
internal/router/dnsmasq/conf.go
Normal file
30
internal/router/dnsmasq/conf.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package dnsmasq
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func InterfaceNameFromConfig(filename string) (string, error) {
|
||||
buf, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return interfaceNameFromReader(bytes.NewReader(buf))
|
||||
}
|
||||
|
||||
func interfaceNameFromReader(r io.Reader) (string, error) {
|
||||
scanner := bufio.NewScanner(r)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
after, found := strings.CutPrefix(line, "interface=")
|
||||
if found {
|
||||
return after, nil
|
||||
}
|
||||
}
|
||||
return "", errors.New("not found")
|
||||
}
|
||||
46
internal/router/dnsmasq/conf_test.go
Normal file
46
internal/router/dnsmasq/conf_test.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package dnsmasq
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_interfaceNameFromReader(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
in string
|
||||
wantIface string
|
||||
}{
|
||||
{
|
||||
"good",
|
||||
`interface=lo`,
|
||||
"lo",
|
||||
},
|
||||
{
|
||||
"multiple",
|
||||
`interface=lo
|
||||
interface=eth0
|
||||
`,
|
||||
"lo",
|
||||
},
|
||||
{
|
||||
"no iface",
|
||||
`cache-size=100`,
|
||||
"",
|
||||
},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ifaceName, err := interfaceNameFromReader(strings.NewReader(tc.in))
|
||||
if tc.wantIface != "" && err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
if tc.wantIface != ifaceName {
|
||||
t.Errorf("mismatched, want: %q, got: %q", tc.wantIface, ifaceName)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user