/build

Ridiculously easy debug.BuildInfo

Primary LanguageGoMIT LicenseMIT

Build

License Go Reference Go Report Card GitHub CI codecov

Project Description

Ridiculously simple Go build info 🛠️

Installation

go get github.com/FollowTheProcess/build@latest

Quickstart

package main

import (
	"fmt"
	"os"

	"github.com/FollowTheProcess/build"
)

func main() {
	info, ok := build.Info()
	if !ok {
		fmt.Fprintf(os.Stderr, "could not get build info")
		os.Exit(1)
	}

	fmt.Printf("%s\n", info)
}

Gets you...

go:           go1.23.2
path:         github.com/FollowTheProcess/build/cmd/build
os:           darwin
arch:         amd64
vcs:          git
version:      (devel)
commit:       5e8b8a68867eff5f754bfecdbc8baeb2c14c711c
dirty:        true
time:         2024-10-06T10:39:12Z
main:         mod  github.com/FollowTheProcess/build  (devel)  
-buildmode:   exe
-compiler:    gc
-ldflags:     -X main.version=dev
CGO_ENABLED:  0
GOAMD64:      v1

Tip

It's also JSON serialisable!

package main

import (
	"encoding/json"
	"fmt"
	"os"

	"github.com/FollowTheProcess/build"
)

func main() {
	info, _ := build.Info()
	if err := json.NewEncoder(os.Stdout).Encode(info); err != nil {
		fmt.Fprintf(os.Stderr, "could not write JSON: %v\n", err)
		os.Exit(1)
	}
}

Gets you...

{
  "main": {
    "path": "github.com/FollowTheProcess/build",
    "version": "(devel)"
  },
  "time": "2024-10-06T10:39:12Z",
  "settings": {
    "-buildmode": "exe",
    "-compiler": "gc",
    "-ldflags": "-X main.version=dev",
    "CGO_ENABLED": "0",
    "GOAMD64": "v1"
  },
  "go": "go1.23.2",
  "path": "github.com/FollowTheProcess/build/cmd/build",
  "os": "darwin",
  "arch": "amd64",
  "vcs": "git",
  "version": "(devel)",
  "dirty": true
}

build.Info returns a BuildInfo struct from which you can take any component of the build info:

package main

import (
	"fmt"
	"os"

	"github.com/FollowTheProcess/build"
)

func main() {
	info, ok := build.Info()
	if !ok {
		fmt.Fprintf(os.Stderr, "could not get build info")
		os.Exit(1)
	}

	fmt.Printf("Version: %s\n", info.Version)
   	fmt.Printf("Commit: %s\n", info.Commit)
}

Credits

This package is wholly based on the Go internal implementation of runtime/debug.BuildInfo, this is just a slightly nicer wrapper that makes it easier to access common settings