mirror of
https://github.com/sysadminsmedia/homebox.git
synced 2025-12-24 22:39:14 +01:00
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: tonya <tonya@tokia.dev>
28 lines
751 B
Go
28 lines
751 B
Go
// Package utils
|
|
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"text/template"
|
|
)
|
|
|
|
// GenerateSubPubConn generates a subscription or publication connection string
|
|
func GenerateSubPubConn(pubSubConn string, topic string) (string, error) {
|
|
if strings.Contains(topic, "{{") || strings.Contains(topic, "}}") {
|
|
return "", fmt.Errorf("topic contains template placeholders, which is not allowed")
|
|
}
|
|
builder := &strings.Builder{}
|
|
tmpl, err := template.New("subPubConn").Parse(pubSubConn)
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to parse template: %w", err)
|
|
}
|
|
err = tmpl.Execute(builder, map[string]interface{}{
|
|
"Topic": topic,
|
|
})
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to parse template: %w", err)
|
|
}
|
|
return builder.String(), nil
|
|
}
|