cmd/cli: silent un-necessary error for physical interfaces loop

The loop is run after the main interface DNS was set, thus the error
would make noise to users. This commit removes the noise, by making
currentStaticDNS returns an additional error, so it's up to the caller
to decive whether to emit the error or not.

Further, the physical interface loop will now only log when the callback
function runs successfully. Emitting the callback error can be done in
the future, until we can figure out how to detect physical interfaces in
Go portably.
This commit is contained in:
Cuong Manh Le
2024-02-14 09:52:18 +07:00
committed by Cuong Manh Le
parent fdb82f6ec3
commit 583718f234
6 changed files with 14 additions and 31 deletions

View File

@@ -2223,8 +2223,3 @@ func absHomeDir(filename string) string {
}
return filepath.Join(dir, filename)
}
// ifaceUp reports whether the net interface is up.
func ifaceUp(iface *net.Interface) bool {
return iface != nil && iface.Flags&net.FlagUp != 0
}

View File

@@ -63,15 +63,12 @@ func currentDNS(_ *net.Interface) []string {
}
// currentStaticDNS returns the current static DNS settings of given interface.
func currentStaticDNS(iface *net.Interface) []string {
func currentStaticDNS(iface *net.Interface) ([]string, error) {
cmd := "networksetup"
args := []string{"-getdnsservers", iface.Name}
out, err := exec.Command(cmd, args...).Output()
if err != nil {
if ifaceUp(iface) {
mainLog.Load().Error().Err(err).Msg("could not get current static DNS")
}
return nil
return nil, err
}
scanner := bufio.NewScanner(bytes.NewReader(out))
var ns []string
@@ -81,5 +78,5 @@ func currentStaticDNS(iface *net.Interface) []string {
ns = append(ns, ip.String())
}
}
return ns
return ns, nil
}

View File

@@ -68,6 +68,6 @@ func currentDNS(_ *net.Interface) []string {
}
// currentStaticDNS returns the current static DNS settings of given interface.
func currentStaticDNS(iface *net.Interface) []string {
return currentDNS(iface)
func currentStaticDNS(iface *net.Interface) ([]string, error) {
return currentDNS(iface), nil
}

View File

@@ -204,8 +204,8 @@ func currentDNS(iface *net.Interface) []string {
}
// currentStaticDNS returns the current static DNS settings of given interface.
func currentStaticDNS(iface *net.Interface) []string {
return currentDNS(iface)
func currentStaticDNS(iface *net.Interface) ([]string, error) {
return currentDNS(iface), nil
}
func getDNSByResolvectl(iface string) []string {

View File

@@ -174,20 +174,14 @@ func currentDNS(iface *net.Interface) []string {
}
// currentStaticDNS returns the current static DNS settings of given interface.
func currentStaticDNS(iface *net.Interface) []string {
func currentStaticDNS(iface *net.Interface) ([]string, error) {
luid, err := winipcfg.LUIDFromIndex(uint32(iface.Index))
if err != nil {
if ifaceUp(iface) {
mainLog.Load().Error().Err(err).Msg("could not get interface LUID")
}
return nil
return nil, err
}
guid, err := luid.GUID()
if err != nil {
if ifaceUp(iface) {
mainLog.Load().Error().Err(err).Msg("could not get interface GUID")
}
return nil
return nil, err
}
var ns []string
for _, path := range []string{v4InterfaceKeyPathFormat, v6InterfaceKeyPathFormat} {
@@ -205,7 +199,7 @@ func currentStaticDNS(iface *net.Interface) []string {
}
}
}
return ns
return ns, nil
}
// addDnsServerForwarders adds given nameservers to DNS server forwarders list.

View File

@@ -703,11 +703,8 @@ func withEachPhysicalInterfaces(excludeIfaceName, context string, f func(i *net.
if strings.Contains(netIface.Name, "vEthernet") {
return
}
if err := f(netIface); err != nil {
if ifaceUp(netIface) {
mainLog.Load().Warn().Err(err).Msgf("failed to %s for interface: %q", context, i.Name)
}
} else {
// TODO: investigate whether we should report this error?
if err := f(netIface); err == nil {
mainLog.Load().Debug().Msgf("%s for interface %q successfully", context, i.Name)
}
})
@@ -735,7 +732,7 @@ func saveCurrentStaticDNS(iface *net.Interface) error {
if err := os.Remove(file); err != nil && !errors.Is(err, os.ErrNotExist) {
mainLog.Load().Warn().Err(err).Msg("could not remove old static DNS settings file")
}
ns := currentStaticDNS(iface)
ns, _ := currentStaticDNS(iface)
if len(ns) == 0 {
return nil
}