mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-02-03 22:18:39 +00:00
Move platform-specific network interface detection from cmd/cli/ to root package as ValidInterfaces function. This eliminates code duplication and provides a consistent interface for determining valid physical network interfaces across all platforms. - Remove duplicate validInterfacesMap functions from platform-specific files - Add context parameter to virtualInterfaces for proper logging - Update all callers to use ctrld.ValidInterfaces instead of local functions - Improve error handling in virtual interface detection on Linux
36 lines
797 B
Go
36 lines
797 B
Go
package ctrld
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"context"
|
|
"io"
|
|
"os/exec"
|
|
"strings"
|
|
)
|
|
|
|
// ValidInterfaces returns a set of all valid hardware ports.
|
|
func ValidInterfaces(_ context.Context) map[string]struct{} {
|
|
b, err := exec.Command("networksetup", "-listallhardwareports").Output()
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
return parseListAllHardwarePorts(bytes.NewReader(b))
|
|
}
|
|
|
|
// parseListAllHardwarePorts parses output of "networksetup -listallhardwareports"
|
|
// and returns map presents all hardware ports.
|
|
func parseListAllHardwarePorts(r io.Reader) map[string]struct{} {
|
|
m := make(map[string]struct{})
|
|
scanner := bufio.NewScanner(r)
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
after, ok := strings.CutPrefix(line, "Device: ")
|
|
if !ok {
|
|
continue
|
|
}
|
|
m[after] = struct{}{}
|
|
}
|
|
return m
|
|
}
|