Skip to content

Commit

Permalink
Merge branch 'main' into beta
Browse files Browse the repository at this point in the history
  • Loading branch information
f18m committed Nov 12, 2024
2 parents fc3b7bf + bf156aa commit b4c1132
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
26 changes: 16 additions & 10 deletions dhcp-clients-webapp-backend/pkg/uibackend/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ type PastDhcpClientData struct {
type DhcpClientFriendlyName struct {
MacAddress net.HardwareAddr
FriendlyName string
Link template.Template
Link *template.Template // maybe nil
}

// IpAddressReservation represents a static IP configuration loaded from the addon configuration file
type IpAddressReservation struct {
Name string
Mac net.HardwareAddr
IP netip.Addr
Link template.Template
Link *template.Template // maybe nil
}

// AddonConfig is used to unmarshal HomeAssistant option file correctly
Expand Down Expand Up @@ -186,9 +186,12 @@ func (b *AddonConfig) UnmarshalJSON(data []byte) error {
return fmt.Errorf("invalid MAC address found inside 'ip_address_reservations': %s", r.Mac)
}

linkTemplate, err := template.New("linkTemplate").Parse(r.Link)
if err != nil {
return fmt.Errorf("invalid golang template found inside 'link': %s", r.Link)
var linkTemplate *template.Template
if r.Link != "" {
linkTemplate, err = template.New("linkTemplate").Parse(r.Link)
if err != nil {
return fmt.Errorf("invalid golang template found inside 'link': %s", r.Link)
}
}

// normalize the IP and MAC address format (e.g. to lowercase)
Expand All @@ -199,7 +202,7 @@ func (b *AddonConfig) UnmarshalJSON(data []byte) error {
Name: r.Name,
Mac: macAddr,
IP: ipAddr,
Link: *linkTemplate,
Link: linkTemplate,
}

b.ipAddressReservationsByIP[ipAddr] = ipReservation
Expand All @@ -213,15 +216,18 @@ func (b *AddonConfig) UnmarshalJSON(data []byte) error {
return fmt.Errorf("invalid MAC address found inside 'dhcp_clients_friendly_names': %s", client.Mac)
}

linkTemplate, err := template.New("linkTemplate").Parse(client.Link)
if err != nil {
return fmt.Errorf("invalid golang template found inside 'link': %s", client.Link)
var linkTemplate *template.Template
if client.Link != "" {
linkTemplate, err = template.New("linkTemplate").Parse(client.Link)
if err != nil {
return fmt.Errorf("invalid golang template found inside 'link': %s", client.Link)
}
}

b.friendlyNames[macAddr.String()] = DhcpClientFriendlyName{
MacAddress: macAddr,
FriendlyName: client.Name,
Link: *linkTemplate,
Link: linkTemplate,
}
}

Expand Down
4 changes: 2 additions & 2 deletions dhcp-clients-webapp-backend/pkg/uibackend/uibackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,12 +500,12 @@ func (b *UIBackend) evaluateLink(hostname string, ip netip.Addr, mac net.Hardwar

r, hasFriendlyName := b.cfg.friendlyNames[mac.String()]
if hasFriendlyName {
theTemplate = &r.Link
theTemplate = r.Link
friendlyName = r.FriendlyName
} else {
r, hasReservation := b.cfg.ipAddressReservationsByIP[ip]
if hasReservation {
theTemplate = &r.Link
theTemplate = r.Link
}
}

Expand Down
4 changes: 2 additions & 2 deletions dhcp-clients-webapp-backend/pkg/uibackend/uibackend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func MustParseMAC(s string) net.HardwareAddr {
return mac
}

func MustParseTemplate(s string) template.Template {
return *template.Must(template.New("test").Parse(s))
func MustParseTemplate(s string) *template.Template {
return template.Must(template.New("test").Parse(s))
}

func getMockLeases() []*dnsmasq.Lease {
Expand Down

0 comments on commit b4c1132

Please sign in to comment.