refactor: replace deprecated strings.Title with custom titleCase
Add simple titleCase functions that handle ASCII strings without the Unicode punctuation issues of strings.Title.
This commit is contained in:
parent
7dad9c7a17
commit
95a258ac32
2 changed files with 44 additions and 3 deletions
|
|
@ -12,6 +12,28 @@ import (
|
||||||
"github.com/rcourtman/pulse-go-rewrite/internal/models"
|
"github.com/rcourtman/pulse-go-rewrite/internal/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// titleCase capitalizes the first letter of each word (simple ASCII-safe version)
|
||||||
|
func titleCase(s string) string {
|
||||||
|
var result strings.Builder
|
||||||
|
capitalizeNext := true
|
||||||
|
for _, r := range s {
|
||||||
|
if unicode.IsSpace(r) || r == '-' {
|
||||||
|
capitalizeNext = true
|
||||||
|
if r == '-' {
|
||||||
|
result.WriteRune(' ')
|
||||||
|
} else {
|
||||||
|
result.WriteRune(r)
|
||||||
|
}
|
||||||
|
} else if capitalizeNext {
|
||||||
|
result.WriteRune(unicode.ToUpper(r))
|
||||||
|
capitalizeNext = false
|
||||||
|
} else {
|
||||||
|
result.WriteRune(unicode.ToLower(r))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result.String()
|
||||||
|
}
|
||||||
|
|
||||||
type MockConfig struct {
|
type MockConfig struct {
|
||||||
NodeCount int
|
NodeCount int
|
||||||
VMsPerNode int
|
VMsPerNode int
|
||||||
|
|
@ -2468,7 +2490,7 @@ func generateCephClusters(nodes []models.Node, storage []models.Storage) []model
|
||||||
cluster := models.CephCluster{
|
cluster := models.CephCluster{
|
||||||
ID: fmt.Sprintf("%s-ceph", instanceName),
|
ID: fmt.Sprintf("%s-ceph", instanceName),
|
||||||
Instance: instanceName,
|
Instance: instanceName,
|
||||||
Name: fmt.Sprintf("%s Ceph", strings.Title(strings.ReplaceAll(instanceName, "-", " "))),
|
Name: fmt.Sprintf("%s Ceph", titleCase(instanceName)),
|
||||||
FSID: fmt.Sprintf("00000000-0000-4000-8000-%012d", rand.Int63n(1_000_000_000_000)),
|
FSID: fmt.Sprintf("00000000-0000-4000-8000-%012d", rand.Int63n(1_000_000_000_000)),
|
||||||
Health: health,
|
Health: health,
|
||||||
HealthMessage: healthMessage,
|
HealthMessage: healthMessage,
|
||||||
|
|
|
||||||
|
|
@ -4,10 +4,29 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
"unicode"
|
||||||
|
|
||||||
"github.com/rcourtman/pulse-go-rewrite/internal/alerts"
|
"github.com/rcourtman/pulse-go-rewrite/internal/alerts"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// titleCase capitalizes the first letter of each word (simple ASCII-safe version)
|
||||||
|
func titleCase(s string) string {
|
||||||
|
var result strings.Builder
|
||||||
|
capitalizeNext := true
|
||||||
|
for _, r := range s {
|
||||||
|
if unicode.IsSpace(r) {
|
||||||
|
capitalizeNext = true
|
||||||
|
result.WriteRune(r)
|
||||||
|
} else if capitalizeNext {
|
||||||
|
result.WriteRune(unicode.ToUpper(r))
|
||||||
|
capitalizeNext = false
|
||||||
|
} else {
|
||||||
|
result.WriteRune(unicode.ToLower(r))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result.String()
|
||||||
|
}
|
||||||
|
|
||||||
// EmailTemplate generates a professional HTML email template for alerts
|
// EmailTemplate generates a professional HTML email template for alerts
|
||||||
func EmailTemplate(alertList []*alerts.Alert, isSingle bool) (subject, htmlBody, textBody string) {
|
func EmailTemplate(alertList []*alerts.Alert, isSingle bool) (subject, htmlBody, textBody string) {
|
||||||
if isSingle && len(alertList) == 1 {
|
if isSingle && len(alertList) == 1 {
|
||||||
|
|
@ -36,11 +55,11 @@ func singleAlertTemplate(alert *alerts.Alert) (subject, htmlBody, textBody strin
|
||||||
case "io":
|
case "io":
|
||||||
alertType = "I/O"
|
alertType = "I/O"
|
||||||
default:
|
default:
|
||||||
alertType = strings.Title(alertType)
|
alertType = titleCase(alertType)
|
||||||
}
|
}
|
||||||
|
|
||||||
subject = fmt.Sprintf("[Pulse Alert] %s: %s on %s",
|
subject = fmt.Sprintf("[Pulse Alert] %s: %s on %s",
|
||||||
strings.Title(string(alert.Level)), alertType, alert.ResourceName)
|
titleCase(string(alert.Level)), alertType, alert.ResourceName)
|
||||||
|
|
||||||
htmlBody = fmt.Sprintf(`<!DOCTYPE html>
|
htmlBody = fmt.Sprintf(`<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue