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:
Cuong Manh Le
2024-02-19 18:29:22 +07:00
committed by Cuong Manh Le
parent 5145729ab1
commit fdb82f6ec3
4 changed files with 23 additions and 14 deletions
+8 -9
View File
@@ -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))