From 8394fc4b0678870f9e30517d29bb74193d27ae12 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Sun, 15 Sep 2024 15:55:54 +0300 Subject: [PATCH] [support] Add info if CGO is used for build --- CHANGELOG.md | 1 + support/support.go | 44 ++++++++++++++++++++++++-------------------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 77ecc58e..5fcb405b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - `[system]` Added macOS support for `GetMemUsage` - `[support]` Added locale to default OS info output - `[mathutil]` Added methods `IsInt`, `IsFloat`, and `IsNumber` +- `[support]` Added info if CGO is used for build ### [13.4.0](https://kaos.sh/ek/13.4.0) diff --git a/support/support.go b/support/support.go index 7e4365a8..2ff28783 100644 --- a/support/support.go +++ b/support/support.go @@ -127,6 +127,7 @@ type BuildInfo struct { GoVersion string `json:"go_version"` GoArch string `json:"go_arch"` GoOS string `json:"go_os"` + CGO bool `json:"cgo"` GitSHA string `json:"git_sha,omitempty"` BinSHA string `json:"bin_sha,omitempty"` @@ -281,8 +282,6 @@ func (i *Info) WithRevision(rev string) *Info { return i } - i.Build.GitSHA = extractGitRevFromBuildInfo() - return i } @@ -436,6 +435,22 @@ func (i *Info) appendBuildInfo() { bin, _ := os.Executable() binSHA := hash.FileHash(bin) + info, ok := debug.ReadBuildInfo() + + if ok { + for _, s := range info.Settings { + switch s.Key { + case "CGO_ENABLED": + if s.Value == "1" { + i.Build.CGO = true + } + case "vcs.revision": + if len(s.Value) > 7 { + i.Build.GitSHA = s.Value[:7] + } + } + } + } i.Build.BinSHA = strutil.Head(binSHA, 7) } @@ -459,8 +474,14 @@ func (i *Info) printAppInfo() { return } + goInfo := fmtc.Sprintf("%s {s}(%s/%s){!}", i.Build.GoVersion, i.Build.GoOS, i.Build.GoArch) + + if i.Build.CGO { + goInfo += fmtc.Sprint(" {s}+CGO{!}") + } + format(7, false, - "Go", fmtc.Sprintf("%s {s}(%s/%s){!}", i.Build.GoVersion, i.Build.GoOS, i.Build.GoArch), + "Go", goInfo, "Git SHA", i.Build.GitSHA+getHashColorBullet(i.Build.GitSHA), "Bin SHA", i.Build.BinSHA+getHashColorBullet(i.Build.BinSHA), ) @@ -814,23 +835,6 @@ func format(size int, printEmpty bool, records ...string) { } } -// extractGitRevFromBuildInfo extracts git SHA from embedded build info -func extractGitRevFromBuildInfo() string { - info, ok := debug.ReadBuildInfo() - - if !ok { - return "" - } - - for _, s := range info.Settings { - if s.Key == "vcs.revision" && len(s.Value) > 7 { - return s.Value[:7] - } - } - - return "" -} - // getHashColorBullet return bullet with color from hash func getHashColorBullet(v string) string { if v == "" || fmtc.DisableColors || !fmtc.IsTrueColorSupported() {