From 65109a787ca95a65a1b6cfaeb57c7fefd451fa16 Mon Sep 17 00:00:00 2001 From: Kharec Date: Mon, 26 Jan 2026 22:17:14 +0100 Subject: [PATCH] feat: use GetVersion() --- cmd/goyco/main.go | 2 +- internal/handlers/api_handler.go | 8 ++++---- internal/version/version_test.go | 20 ++++++++++---------- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/cmd/goyco/main.go b/cmd/goyco/main.go index 2dc0c0c..6b2fa07 100644 --- a/cmd/goyco/main.go +++ b/cmd/goyco/main.go @@ -55,7 +55,7 @@ func run(args []string) error { docs.SwaggerInfo.Title = fmt.Sprintf("%s API", cfg.App.Title) docs.SwaggerInfo.Description = "Y Combinator-style news board API." - docs.SwaggerInfo.Version = version.Version + docs.SwaggerInfo.Version = version.GetVersion() docs.SwaggerInfo.BasePath = "/api" docs.SwaggerInfo.Host = fmt.Sprintf("%s:%s", cfg.Server.Host, cfg.Server.Port) docs.SwaggerInfo.Schemes = []string{"http"} diff --git a/internal/handlers/api_handler.go b/internal/handlers/api_handler.go index 5c7c407..471685b 100644 --- a/internal/handlers/api_handler.go +++ b/internal/handlers/api_handler.go @@ -75,7 +75,7 @@ func (h *APIHandler) GetAPIInfo(w http.ResponseWriter, r *http.Request) { apiInfo := map[string]any{ "name": fmt.Sprintf("%s API", h.config.App.Title), - "version": version.Version, + "version": version.GetVersion(), "description": "Y Combinator-style news board API", "endpoints": map[string]any{ "authentication": map[string]any{ @@ -145,7 +145,7 @@ func (h *APIHandler) GetHealth(w http.ResponseWriter, r *http.Request) { if h.healthChecker != nil { health := h.healthChecker.CheckHealth() - health["version"] = version.Version + health["version"] = version.GetVersion() SendSuccessResponse(w, "Health check successful", health) return } @@ -155,7 +155,7 @@ func (h *APIHandler) GetHealth(w http.ResponseWriter, r *http.Request) { health := map[string]any{ "status": "healthy", "timestamp": currentTimestamp, - "version": version.Version, + "version": version.GetVersion(), "services": map[string]any{ "database": "connected", "api": "running", @@ -230,7 +230,7 @@ func (h *APIHandler) GetMetrics(w http.ResponseWriter, r *http.Request) { }, "system": map[string]any{ "timestamp": time.Now().UTC().Format(time.RFC3339), - "version": version.Version, + "version": version.GetVersion(), }, } diff --git a/internal/version/version_test.go b/internal/version/version_test.go index cd02b32..822204a 100644 --- a/internal/version/version_test.go +++ b/internal/version/version_test.go @@ -8,39 +8,39 @@ import ( func TestVersionSemver(t *testing.T) { semverRegex := regexp.MustCompile(`^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$`) - if !semverRegex.MatchString(Version) { - t.Errorf("Version %q does not follow semantic versioning format (MAJOR.MINOR.PATCH[-PRERELEASE][+BUILD])", Version) + if !semverRegex.MatchString(GetVersion()) { + t.Errorf("Version %q does not follow semantic versioning format (MAJOR.MINOR.PATCH[-PRERELEASE][+BUILD])", GetVersion()) } } func TestVersionSemverFlexible(t *testing.T) { flexibleSemverRegex := regexp.MustCompile(`^(0|[1-9]\d*)\.(0|[1-9]\d*)(?:\.(0|[1-9]\d*))?(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$`) - if !flexibleSemverRegex.MatchString(Version) { - t.Errorf("Version %q does not follow semantic versioning format (MAJOR.MINOR[.PATCH][-PRERELEASE][+BUILD])", Version) + if !flexibleSemverRegex.MatchString(GetVersion()) { + t.Errorf("Version %q does not follow semantic versioning format (MAJOR.MINOR[.PATCH][-PRERELEASE][+BUILD])", GetVersion()) } } func TestVersionNotEmpty(t *testing.T) { - if Version == "" { + if GetVersion() == "" { t.Error("Version should not be empty") } } func TestVersionFormat(t *testing.T) { - if !regexp.MustCompile(`\d+\.\d+`).MatchString(Version) { - t.Errorf("Version %q should contain at least MAJOR.MINOR format", Version) + if !regexp.MustCompile(`\d+\.\d+`).MatchString(GetVersion()) { + t.Errorf("Version %q should contain at least MAJOR.MINOR format", GetVersion()) } } func TestVersionStartsWithNumber(t *testing.T) { - if !regexp.MustCompile(`^\d+`).MatchString(Version) { - t.Errorf("Version %q should start with a number", Version) + if !regexp.MustCompile(`^\d+`).MatchString(GetVersion()) { + t.Errorf("Version %q should start with a number", GetVersion()) } } func TestVersionNoLeadingZeros(t *testing.T) { - parts := regexp.MustCompile(`^(\d+)\.(\d+)`).FindStringSubmatch(Version) + parts := regexp.MustCompile(`^(\d+)\.(\d+)`).FindStringSubmatch(GetVersion()) if len(parts) >= 3 { major := parts[1] minor := parts[2]