Skip to content

Commit

Permalink
[support] Add info if CGO is used for build
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Sep 15, 2024
1 parent 5459768 commit 8394fc4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
44 changes: 24 additions & 20 deletions support/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -281,8 +282,6 @@ func (i *Info) WithRevision(rev string) *Info {
return i
}

i.Build.GitSHA = extractGitRevFromBuildInfo()

return i
}

Expand Down Expand Up @@ -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)
}
Expand All @@ -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),
)
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 8394fc4

Please sign in to comment.