Apply go fix modernizations

This commit is contained in:
Ian Gulliver
2026-03-05 11:39:36 -08:00
parent 41f3602696
commit dd360b8e28
10 changed files with 57 additions and 60 deletions

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"log"
"net"
"slices"
"sort"
"strings"
"time"
@@ -28,7 +29,6 @@ func (h *artnetHandler) HandlePollReply(src *net.UDPAddr, pkt *artnet.PollReplyP
h.discovery.HandlePollReply(src, pkt)
}
func (t *Tendrils) startArtNet(ctx context.Context, iface net.Interface) {
srcIP, broadcast := getInterfaceIPv4(iface)
if srcIP == nil || broadcast == nil {
@@ -124,7 +124,6 @@ func (n *Nodes) UpdateArtNet(node *Node, inputs, outputs []int) {
}
}
func (n *Nodes) expireArtNet() {
for _, node := range n.nodes {
if node.ArtNetInputs != nil {
@@ -197,7 +196,7 @@ func (n *Nodes) logArtNet() {
seen[u] = true
}
}
sort.Slice(allUniverses, func(i, j int) bool { return allUniverses[i] < allUniverses[j] })
slices.Sort(allUniverses)
log.Printf("[sigusr1] ================ %d artnet universes ================", len(allUniverses))
for _, u := range allUniverses {

12
bmd.go
View File

@@ -211,9 +211,9 @@ func (t *Tendrils) probeHyperDeck(ip net.IP) string {
}
response := string(buf[:n])
for _, line := range strings.Split(response, "\r\n") {
if strings.HasPrefix(line, "model: ") {
model := strings.TrimPrefix(line, "model: ")
for line := range strings.SplitSeq(response, "\r\n") {
if after, ok := strings.CutPrefix(line, "model: "); ok {
model := after
if t.DebugBMD {
log.Printf("[bmd] hyperdeck %s at %s", model, ip)
}
@@ -238,9 +238,9 @@ func (t *Tendrils) probeVideoHub(ip net.IP) string {
}
response := string(buf[:n])
for _, line := range strings.Split(response, "\n") {
if strings.HasPrefix(line, "Model name: ") {
model := strings.TrimSpace(strings.TrimPrefix(line, "Model name: "))
for line := range strings.SplitSeq(response, "\n") {
if after, ok := strings.CutPrefix(line, "Model name: "); ok {
model := strings.TrimSpace(after)
if t.DebugBMD {
log.Printf("[bmd] videohub %s at %s", model, ip)
}

View File

@@ -527,7 +527,7 @@ func (t *Tendrils) queryDanteTxChannels(conn *net.UDPConn, ip net.IP, txCount in
}
pagesNeeded := (txCount + 15) / 16
for page := 0; page < pagesNeeded; page++ {
for page := range pagesNeeded {
pageNum := byte(page + 1)
args := []byte{0x00, 0x01, 0x00, pageNum, 0x00, 0x00}
@@ -551,7 +551,7 @@ func (t *Tendrils) queryDanteSubscriptions(conn *net.UDPConn, ip net.IP, rxCount
hasMulticast := false
pagesNeeded := (rxCount + 15) / 16
for page := 0; page < pagesNeeded; page++ {
for page := range pagesNeeded {
pageNum := byte(page + 1)
args := []byte{0x00, 0x01, 0x00, pageNum, 0x00, 0x00}
@@ -582,7 +582,7 @@ func (t *Tendrils) queryDanteSubscriptions(conn *net.UDPConn, ip net.IP, rxCount
}
}
recordOffset := 12
for idx := 0; idx < subCount; idx++ {
for idx := range subCount {
if recordOffset+20 > len(resp) {
break
}
@@ -612,7 +612,7 @@ func (t *Tendrils) queryDanteSubscriptions(conn *net.UDPConn, ip net.IP, rxCount
}
} else {
recordOffset := 14
for idx := 0; idx < subCount; idx++ {
for idx := range subCount {
if recordOffset+10 > len(resp) {
break
}
@@ -663,7 +663,7 @@ func (t *Tendrils) queryDanteSubscriptions3400(conn *net.UDPConn, ip net.IP, rxC
pagesNeeded := (rxCount + 15) / 16
startChannel := 1
for page := 0; page < pagesNeeded; page++ {
for page := range pagesNeeded {
pageNum := page + 1
args := make([]byte, 24)
args[7] = 0x01

View File

@@ -29,7 +29,7 @@ type Error struct {
Utilization float64 `json:"utilization,omitempty"`
FirstSeen time.Time `json:"first_seen"`
LastSeen time.Time `json:"last_seen"`
LastUpdated time.Time `json:"last_updated,omitempty"`
LastUpdated time.Time `json:"last_updated,omitzero"`
}
type ErrorTracker struct {

View File

@@ -46,8 +46,8 @@ func extractNetaudioChanDevice(s string) string {
return ""
}
name := strings.ReplaceAll(s[:idx], "\\", "")
if atIdx := strings.Index(name, "@"); atIdx >= 0 {
return name[atIdx+1:]
if _, after, ok := strings.Cut(name, "@"); ok {
return after
}
return ""
}
@@ -235,7 +235,7 @@ func (t *Tendrils) sendMDNSQueries(ifaceName string, conn *net.UDPConn) {
dest := &net.UDPAddr{IP: net.IPv4(224, 0, 0, 251), Port: 5353}
n := len(mdnsServices)
for i := 0; i < n; i++ {
for i := range n {
service := mdnsServices[(i+mdnsQueryOffset)%n]
msg := new(dns.Msg)
msg.SetQuestion(service, dns.TypePTR)

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"log"
"net"
"slices"
"sort"
"strings"
"sync"
@@ -133,12 +134,7 @@ func (n *Nodes) isSharedName(name string) bool {
if n.t.config == nil {
return false
}
for _, shared := range n.t.config.SharedNames {
if shared == name {
return true
}
}
return false
return slices.Contains(n.t.config.SharedNames, name)
}
func (n *Nodes) createNode() *Node {

View File

@@ -17,7 +17,7 @@ func (t *Tendrils) startSACNDiscoveryListener(ctx context.Context, iface net.Int
}
defer receiver.Stop()
receiver.SetHandler(func(src *net.UDPAddr, pkt interface{}) {
receiver.SetHandler(func(src *net.UDPAddr, pkt any) {
if disc, ok := pkt.(*sacn.DiscoveryPacket); ok {
t.handleSACNDiscoveryPacket(src.IP, disc)
}

View File

@@ -52,7 +52,7 @@ func defaultSNMPConfig() *snmpConfig {
}
}
func snmpToInt(val interface{}) (int, bool) {
func snmpToInt(val any) (int, bool) {
switch v := val.(type) {
case int:
return v, true

View File

@@ -21,7 +21,7 @@ func getInterfaceIPv4(iface net.Interface) (srcIP, broadcast net.IP) {
srcIP = ipnet.IP.To4()
mask := ipnet.Mask
broadcast = make(net.IP, 4)
for i := 0; i < 4; i++ {
for i := range 4 {
broadcast[i] = srcIP[i] | ^mask[i]
}
return srcIP, broadcast

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"math"
"net"
"slices"
"sort"
"strings"
"time"
@@ -57,7 +58,7 @@ func (s ArtNetUniverseSet) Universes() []ArtNetUniverse {
for u := range s {
result = append(result, u)
}
sort.Slice(result, func(i, j int) bool { return result[i] < result[j] })
slices.Sort(result)
return result
}
@@ -91,7 +92,7 @@ func (s SACNUniverseSet) Universes() []SACNUniverse {
for u := range s {
result = append(result, u)
}
sort.Slice(result, func(i, j int) bool { return result[i] < result[j] })
slices.Sort(result)
return result
}
@@ -137,11 +138,11 @@ const (
)
type MulticastGroup struct {
ID MulticastGroupID
SACNUniverse SACNUniverse
DanteFlow int
DanteAV int
RawIP string
ID MulticastGroupID
SACNUniverse SACNUniverse
DanteFlow int
DanteAV int
RawIP string
}
func (g MulticastGroup) String() string {
@@ -268,7 +269,7 @@ func (s MulticastMembershipSet) SACNInputs() []SACNUniverse {
result = append(result, g.SACNUniverse)
}
}
sort.Slice(result, func(i, j int) bool { return result[i] < result[j] })
slices.Sort(result)
return result
}
@@ -476,28 +477,28 @@ func (f *DanteFlows) Expire(maxAge time.Duration) bool {
}
type Node struct {
ID string `json:"id"`
Names NameSet `json:"names"`
Interfaces InterfaceMap `json:"interfaces"`
MACTable map[string]string `json:"mac_table,omitempty"`
Type NodeType `json:"type,omitempty"`
PoEBudget *PoEBudget `json:"poe_budget,omitempty"`
DanteTxChannels int `json:"dante_tx_channels,omitempty"`
DanteClockMasterSeen time.Time `json:"-"`
DanteFlows *DanteFlows `json:"dante_flows,omitempty"`
MulticastGroups MulticastMembershipSet `json:"multicast_groups,omitempty"`
ArtNetInputs ArtNetUniverseSet `json:"artnet_inputs,omitempty"`
ArtNetOutputs ArtNetUniverseSet `json:"artnet_outputs,omitempty"`
SACNUnicastInputs SACNUniverseSet `json:"sacn_unicast_inputs,omitempty"`
SACNOutputs SACNUniverseSet `json:"sacn_outputs,omitempty"`
ArtmapMappings []ArtmapMapping `json:"artmap_mappings,omitempty"`
Unreachable bool `json:"unreachable,omitempty"`
Avoid bool `json:"avoid,omitempty"`
InConfig bool `json:"in_config,omitempty"`
IsSelf bool `json:"is_self,omitempty"`
errors *ErrorTracker
pollTrigger chan struct{}
cancelFunc context.CancelFunc
ID string `json:"id"`
Names NameSet `json:"names"`
Interfaces InterfaceMap `json:"interfaces"`
MACTable map[string]string `json:"mac_table,omitempty"`
Type NodeType `json:"type,omitempty"`
PoEBudget *PoEBudget `json:"poe_budget,omitempty"`
DanteTxChannels int `json:"dante_tx_channels,omitempty"`
DanteClockMasterSeen time.Time `json:"-"`
DanteFlows *DanteFlows `json:"dante_flows,omitempty"`
MulticastGroups MulticastMembershipSet `json:"multicast_groups,omitempty"`
ArtNetInputs ArtNetUniverseSet `json:"artnet_inputs,omitempty"`
ArtNetOutputs ArtNetUniverseSet `json:"artnet_outputs,omitempty"`
SACNUnicastInputs SACNUniverseSet `json:"sacn_unicast_inputs,omitempty"`
SACNOutputs SACNUniverseSet `json:"sacn_outputs,omitempty"`
ArtmapMappings []ArtmapMapping `json:"artmap_mappings,omitempty"`
Unreachable bool `json:"unreachable,omitempty"`
Avoid bool `json:"avoid,omitempty"`
InConfig bool `json:"in_config,omitempty"`
IsSelf bool `json:"is_self,omitempty"`
errors *ErrorTracker
pollTrigger chan struct{}
cancelFunc context.CancelFunc
}
func (n *Node) IsDanteClockMaster() bool {
@@ -703,11 +704,12 @@ func (i *Interface) String() string {
parts = append(parts, i.Stats.String())
}
result := parts[0]
var result strings.Builder
result.WriteString(parts[0])
for _, p := range parts[1:] {
result += " " + p
result.WriteString(" " + p)
}
return result
return result.String()
}
func (s *InterfaceStats) String() string {