Pulse/internal/utils/helpers.go
rcourtman b1bc704e3a Consolidate duplicate normalizeVersion functions into shared utility
- Move normalizeVersion to utils.NormalizeVersion for single source of truth
- Update agentupdate and dockeragent packages to use shared function
- Add 14 test cases for version normalization

This prevents bugs like issue #773 where a fix applied to one copy
but not the other caused an update loop.
2025-11-29 22:57:33 +00:00

50 lines
1.4 KiB
Go

package utils
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"github.com/google/uuid"
)
// GenerateID generates a unique ID with the given prefix
func GenerateID(prefix string) string {
return fmt.Sprintf("%s-%s", prefix, uuid.NewString())
}
// WriteJSONResponse writes a JSON response to the http.ResponseWriter
func WriteJSONResponse(w http.ResponseWriter, data interface{}) error {
w.Header().Set("Content-Type", "application/json")
// Use Marshal instead of Encoder for better performance with large payloads
jsonData, err := json.Marshal(data)
if err != nil {
return err
}
_, err = w.Write(jsonData)
return err
}
// ParseBool interprets common boolean strings, returning true for typical truthy values.
func ParseBool(value string) bool {
switch strings.ToLower(strings.TrimSpace(value)) {
case "1", "true", "yes", "y", "on":
return true
default:
return false
}
}
// GetenvTrim returns the environment variable value with surrounding whitespace removed.
func GetenvTrim(key string) string {
return strings.TrimSpace(os.Getenv(key))
}
// NormalizeVersion strips the "v" prefix from version strings for comparison.
// This normalizes versions like "v4.33.1" to "4.33.1" so that version strings
// from different sources (agent vs server) can be compared consistently.
func NormalizeVersion(version string) string {
return strings.TrimPrefix(strings.TrimSpace(version), "v")
}