From 583718f234d06d70a099b02952994286a2255757 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Wed, 14 Feb 2024 09:52:18 +0700 Subject: [PATCH] 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. --- cmd/cli/cli.go | 5 ----- cmd/cli/os_darwin.go | 9 +++------ cmd/cli/os_freebsd.go | 4 ++-- cmd/cli/os_linux.go | 4 ++-- cmd/cli/os_windows.go | 14 ++++---------- cmd/cli/prog.go | 9 +++------ 6 files changed, 14 insertions(+), 31 deletions(-) diff --git a/cmd/cli/cli.go b/cmd/cli/cli.go index a386646..e97b53c 100644 --- a/cmd/cli/cli.go +++ b/cmd/cli/cli.go @@ -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 -} diff --git a/cmd/cli/os_darwin.go b/cmd/cli/os_darwin.go index 3b21844..7ce4aa1 100644 --- a/cmd/cli/os_darwin.go +++ b/cmd/cli/os_darwin.go @@ -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 } diff --git a/cmd/cli/os_freebsd.go b/cmd/cli/os_freebsd.go index a8de0c6..216b36f 100644 --- a/cmd/cli/os_freebsd.go +++ b/cmd/cli/os_freebsd.go @@ -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 } diff --git a/cmd/cli/os_linux.go b/cmd/cli/os_linux.go index c7661f0..fcff741 100644 --- a/cmd/cli/os_linux.go +++ b/cmd/cli/os_linux.go @@ -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 { diff --git a/cmd/cli/os_windows.go b/cmd/cli/os_windows.go index e185dc0..56097f8 100644 --- a/cmd/cli/os_windows.go +++ b/cmd/cli/os_windows.go @@ -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. diff --git a/cmd/cli/prog.go b/cmd/cli/prog.go index 1940bd4..03f2105 100644 --- a/cmd/cli/prog.go +++ b/cmd/cli/prog.go @@ -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 }