mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-02-03 22:18:39 +00:00
cmd/cli: only emit error for running interfaces
While at it, also ensure setDNS/resetDNS return a wrapped error on Darwin/Windows, so the caller can decide whether to print the error to users.
This commit is contained in:
committed by
Cuong Manh Le
parent
5145729ab1
commit
fdb82f6ec3
@@ -2223,3 +2223,8 @@ 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
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package cli
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net"
|
||||
"os/exec"
|
||||
|
||||
@@ -36,10 +37,8 @@ func setDNS(iface *net.Interface, nameservers []string) error {
|
||||
cmd := "networksetup"
|
||||
args := []string{"-setdnsservers", iface.Name}
|
||||
args = append(args, nameservers...)
|
||||
|
||||
if err := exec.Command(cmd, args...).Run(); err != nil {
|
||||
mainLog.Load().Error().Err(err).Msgf("setDNS failed, ips = %q", nameservers)
|
||||
return err
|
||||
if out, err := exec.Command(cmd, args...).CombinedOutput(); err != nil {
|
||||
return fmt.Errorf("%v: %w", string(out), err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -53,10 +52,8 @@ func resetDNS(iface *net.Interface) error {
|
||||
}
|
||||
cmd := "networksetup"
|
||||
args := []string{"-setdnsservers", iface.Name, "empty"}
|
||||
|
||||
if err := exec.Command(cmd, args...).Run(); err != nil {
|
||||
mainLog.Load().Error().Err(err).Msgf("resetDNS failed")
|
||||
return err
|
||||
if out, err := exec.Command(cmd, args...).CombinedOutput(); err != nil {
|
||||
return fmt.Errorf("%v: %w", string(out), err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -71,7 +68,9 @@ func currentStaticDNS(iface *net.Interface) []string {
|
||||
args := []string{"-getdnsservers", iface.Name}
|
||||
out, err := exec.Command(cmd, args...).Output()
|
||||
if err != nil {
|
||||
mainLog.Load().Error().Err(err).Msg("could not get current static DNS")
|
||||
if ifaceUp(iface) {
|
||||
mainLog.Load().Error().Err(err).Msg("could not get current static DNS")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
scanner := bufio.NewScanner(bytes.NewReader(out))
|
||||
|
||||
@@ -89,8 +89,7 @@ func resetDNS(iface *net.Interface) error {
|
||||
// Restoring ipv4 DHCP.
|
||||
output, err := netsh("interface", "ipv4", "set", "dnsserver", strconv.Itoa(iface.Index), "dhcp")
|
||||
if err != nil {
|
||||
mainLog.Load().Error().Err(err).Msgf("failed to reset ipv4 DNS: %s", string(output))
|
||||
return err
|
||||
return fmt.Errorf("%s: %w", string(output), err)
|
||||
}
|
||||
// If there's static DNS saved, restoring it.
|
||||
if nss := savedStaticNameservers(iface); len(nss) > 0 {
|
||||
@@ -178,12 +177,16 @@ func currentDNS(iface *net.Interface) []string {
|
||||
func currentStaticDNS(iface *net.Interface) []string {
|
||||
luid, err := winipcfg.LUIDFromIndex(uint32(iface.Index))
|
||||
if err != nil {
|
||||
mainLog.Load().Error().Err(err).Msg("could not get interface LUID")
|
||||
if ifaceUp(iface) {
|
||||
mainLog.Load().Error().Err(err).Msg("could not get interface LUID")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
guid, err := luid.GUID()
|
||||
if err != nil {
|
||||
mainLog.Load().Error().Err(err).Msg("could not get interface GUID")
|
||||
if ifaceUp(iface) {
|
||||
mainLog.Load().Error().Err(err).Msg("could not get interface GUID")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
var ns []string
|
||||
|
||||
@@ -704,7 +704,9 @@ func withEachPhysicalInterfaces(excludeIfaceName, context string, f func(i *net.
|
||||
return
|
||||
}
|
||||
if err := f(netIface); err != nil {
|
||||
mainLog.Load().Warn().Err(err).Msgf("failed to %s for interface: %q", context, i.Name)
|
||||
if ifaceUp(netIface) {
|
||||
mainLog.Load().Warn().Err(err).Msgf("failed to %s for interface: %q", context, i.Name)
|
||||
}
|
||||
} else {
|
||||
mainLog.Load().Debug().Msgf("%s for interface %q successfully", context, i.Name)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user