gobuildinfo

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 15, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

README

Go Reference Go Report Card Test codecov

go-buildinfo

A zero-dependency Go module for capturing and displaying build-time and runtime metadata.

Installation

go get github.com/bartventer/go-buildinfo

Usage


package main

import (
    "fmt"

    gobuildinfo "github.com/bartventer/go-buildinfo"
)

const logo = `
    __          _ __    ___       ____    
   / /_  __  __(_) /___/ (_)___  / __/___ 
  / __ \/ / / / / / __  / / __ \/ /_/ __ \
 / /_/ / /_/ / / / /_/ / / / / / __/ /_/ /
/_.___/\__,_/_/_/\__,_/_/_/ /_/_/  \____/
`

// Typically set by the linker during the build process
var (
    version   = "v1.0.0"
    commit    = "d4c3db2e5f8a4b1e9f7c2a1b3d4e5f6a7b8c9d0e"
    date      = "2025-10-01"
    treeState = "clean"
)

func main() {
    info := gobuildinfo.New(
        gobuildinfo.WithVersion(version),
        gobuildinfo.WithCommit(commit),
        gobuildinfo.WithDate(date),
        gobuildinfo.WithTreeState(treeState),
        gobuildinfo.WithProject(gobuildinfo.Project{
            Name:        "BuildInfo Example",
            Description: "A simple example of using go-buildinfo",
            URL:         "https://example.com",
            ASCIILogo:   logo,
        }),
    )

    fmt.Println(info.String())
}

Example Output

    __          _ __    ___       ____    
   / /_  __  __(_) /___/ (_)___  / __/___ 
  / __ \/ / / / / / __  / / __ \/ /_/ __ \
 / /_/ / /_/ / / / /_/ / / / / / __/ /_/ /
/_.___/\__,_/_/_/\__,_/_/_/ /_/_/  \____/ 
BuildInfo Example: A simple example of using go-buildinfo
https://example.com

Version:    v1.0.0
Commit:     d4c3db2e5f8a4b1e9f7c2a1b3d4e5f6a7b8c9d0e
Date:       2023-10-01
TreeState:  clean
GoVersion:  go1.20.3
Compiler:   gc
Platform:   linux/amd64
ModuleSum:  h1:abc1234567890defghijklmnopqrstuvwxyz1234567890==

License

This project is licensed under the Apache License 2.0. See the LICENSE file for details.

Acknowledgments

Inspired by the go-version module.

Documentation

Overview

Package buildinfo provides information about the build.

This implementation was inspired by the go-version module.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Info

type Info struct {
	// contains filtered or unexported fields
}

func New

func New(opts ...Option) *Info

New creates a new Info instance with the provided options.

func (Info) String

func (i Info) String() string
Example
package main

import (
	"fmt"

	gobuildinfo "github.com/bartventer/go-buildinfo"
)

const logo = `
_|                  _|  _|        _|  _|                _|_|
_|_|_|    _|    _|      _|    _|_|_|      _|_|_|      _|        _|_|
_|    _|  _|    _|  _|  _|  _|    _|  _|  _|    _|  _|_|_|_|  _|    _|
_|    _|  _|    _|  _|  _|  _|    _|  _|  _|    _|    _|      _|    _|
_|_|_|      _|_|_|  _|  _|    _|_|_|  _|  _|    _|    _|        _|_|
`

func main() {
	info := gobuildinfo.New(
		gobuildinfo.WithVersion("v1.0.0"),
		gobuildinfo.WithCommit("a696fbbcb8ae009e3f88df2d7b00c09bea903c9e"),
		gobuildinfo.WithDate("2023-01-01"),
		gobuildinfo.WithTreeState("clean"),
		gobuildinfo.WithDisableRuntime(), // Comment this line to include runtime info
		gobuildinfo.WithProject(gobuildinfo.Project{
			Name:        "MyApp",
			Description: "This is a sample app",
			URL:         "https://example.com",
			ASCIILogo:   logo,
		}),
	)

	fmt.Println(info.String())
}
Output:

_|                  _|  _|        _|  _|                _|_|
_|_|_|    _|    _|      _|    _|_|_|      _|_|_|      _|        _|_|
_|    _|  _|    _|  _|  _|  _|    _|  _|  _|    _|  _|_|_|_|  _|    _|
_|    _|  _|    _|  _|  _|  _|    _|  _|  _|    _|    _|      _|    _|
_|_|_|      _|_|_|  _|  _|    _|_|_|  _|  _|    _|    _|        _|_|

MyApp: This is a sample app
https://example.com

Version:    v1.0.0
Commit:     a696fbbcb8ae009e3f88df2d7b00c09bea903c9e
Date:       2023-01-01
TreeState:  clean

type Option

type Option interface {
	// contains filtered or unexported methods
}

func WithCommit

func WithCommit(commit string) Option

func WithDate

func WithDate(date string) Option

func WithDisableRuntime added in v0.2.0

func WithDisableRuntime() Option

func WithProject

func WithProject(project Project) Option

func WithTreeState

func WithTreeState(treeState string) Option

func WithVersion

func WithVersion(version string) Option

type Options added in v0.2.0

type Options Info

type Project

type Project struct {
	Name        string
	Description string
	URL         string
}

Project implements the Option interface to set the project information.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL