Files
fn_registry/functions/infra/go_build_binary.go
T
egutierrez 90693fb32f feat: funciones infra — Docker, deploy, build y health check
Funciones impuras para gestión de contenedores: docker_build_image,
docker_compose_up/down, docker_volume_create/list/remove,
generate_dockerfile, write_dockerfile, go_build_binary, health_check_http,
deploy_app y stop_app. Todas con tests unitarios donde aplica.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-30 14:24:12 +02:00

43 lines
1.2 KiB
Go

package infra
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
// GoBuildBinary compila un binario Go desde projectDir hacia outputPath.
// Si ldflags está vacío usa "-s -w" (strip debug info). Si outputPath está vacío
// usa "build/{dirname}" dentro del projectDir. Requiere Go instalado en PATH.
func GoBuildBinary(projectDir, outputPath string, ldflags string, tags string) error {
if ldflags == "" {
ldflags = "-s -w"
}
if outputPath == "" {
outputPath = filepath.Join(projectDir, "build", filepath.Base(projectDir))
}
// Crear directorio destino si no existe
if err := os.MkdirAll(filepath.Dir(outputPath), 0o755); err != nil {
return fmt.Errorf("go_build_binary: crear directorio destino %s: %w", filepath.Dir(outputPath), err)
}
args := []string{"build", "-trimpath", "-ldflags=" + ldflags}
if tags != "" {
args = append(args, "-tags="+tags)
}
args = append(args, "-o", outputPath, ".")
cmd := exec.Command("go", args...)
cmd.Dir = projectDir
cmd.Env = append(os.Environ(), "CGO_ENABLED=0")
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("go build en %s: %s", projectDir, strings.TrimSpace(string(out)))
}
return nil
}