Przeglądaj źródła

fix: --version flag en agente, BuildCommit en servidor, goreleaser actualizado

- agent: flag --version imprime Version y sale (evita colgar SSH)
- server: updater.BuildCommit inyectable via ldflags, evita falso positivo
  'nueva versión disponible' en builds manuales
- goreleaser: inyecta BuildCommit en server con {{.ShortCommit}}
gnacho 4 miesięcy temu
rodzic
commit
57e6f9c

+ 4 - 1
.goreleaser.yaml

@@ -22,7 +22,10 @@ builds:
     goarch: [amd64, arm64, arm]
     goarm: [7]
     flags: [-trimpath]
-    ldflags: [-s, -w]
+    ldflags:
+      - -s
+      - -w
+      - "-X github.com/gnacho/netpulse/server-go/internal/updater.BuildCommit={{.ShortCommit}}"
 
   - id: netpulse-collector
     dir: collector

+ 10 - 0
agent/cmd/netpulse-agent/main.go

@@ -22,6 +22,8 @@ package main
 import (
 	"context"
 	"crypto/tls"
+	"flag"
+	"fmt"
 	"log"
 	"net/http"
 	"os"
@@ -134,6 +136,14 @@ func (e *configError) Error() string {
 func main() {
 	log.SetOutput(os.Stderr)
 	log.SetPrefix("")
+
+	versionFlag := flag.Bool("version", false, "print version and exit")
+	flag.Parse()
+	if *versionFlag {
+		fmt.Println(Version)
+		os.Exit(0)
+	}
+
 	if err := run(); err != nil {
 		log.Printf("[netpulse-agent] error fatal: %v", err)
 		os.Exit(1)

+ 8 - 0
server-go/internal/updater/updater.go

@@ -30,6 +30,11 @@ const (
 // APIBase es la base de la API de GitHub (inyectable en tests).
 var APIBase = "https://api.github.com"
 
+// BuildCommit se fija en compilación con -ldflags "-X ...". Si está definido,
+// el updater lo usa en vez de ejecutar git rev-parse (permite builds fuera de un
+// repo git, p.ej. despliegues manuales sin working tree).
+var BuildCommit string
+
 type progress struct {
 	Step string `json:"step"`
 }
@@ -82,6 +87,9 @@ func New(repoRoot, repo, token string) *Updater {
 }
 
 func gitShort(repoRoot string) string {
+	if BuildCommit != "" {
+		return BuildCommit
+	}
 	out, err := exec.Command("git", "-C", repoRoot, "rev-parse", "--short", "HEAD").Output()
 	if err != nil {
 		return ""