build

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2022 License: MIT Imports: 7 Imported by: 0

README

Use GNU Make to build Golang projects with embedded build details

This is a project which is intended to demonstrate how to use Makefiles to build go projects. Build version tag will be detected automatically and updated in the compilation time.

Build Status codecov Go Report Card GoDoc

Install GNU Make

Debian based systems
sudo apt install make
Red Hat based systems
yum install make

How to use

  1. Import Makefile into your project root
  2. Update main function as follows.
package main

import (
    buildMeta "github.com/srimaln91/go-make"
)

func main() {

    // Print binary details and terminate the program when --version flag provided.
    buildMeta.CheckVersion()

    // Optionally we can pass a printer that we can use to control the output
    // It acceptes any type which implements the `Printer` interface
    buildMeta.CheckVersion(buildMeta.PRINTER_SINGLE_LINE)

    // Starting the bootstrpping process
    // bootstrap.Start()
}

Make Commands configured in this project

This project contains a sample Makefile with some build tasks.

# List configured make tasks
make help

# Run unit tests
make test

# Build project
make build

# Run project
make run

# Clean build directory
make clean

Check Binary Version

# Can check binary version by passing --version flag
./build/vx.x.x/go-build-linux-amd64 --version
# Output
+----------------+------------+------------------------------------------+-------------+-------------------------+
| BINARY VERSION | GO VERSION |                GIT COMMIT                |   OS/ARCH   |          BUILT          |
+----------------+------------+------------------------------------------+-------------+-------------------------+
| v0.6.0         | go1.12.9   | c8bf7b40e9d842769b580b704931904197e0b713 | linux/amd64 | 2019-10-05-14:01:35-UTC |
+----------------+------------+------------------------------------------+-------------+-------------------------+

Version tags

  • If the source code is exactly on a build tag, the binary will be created with a clean tag. Like vx.x.x
  • If the source code is modified and have uncommited changes, the build tag would be vx.x.x-dirty (latest build tag - dirty)
  • If the source code has any untagged changes and the working directory is clean, the build tag will be vx.x.x-34fdr54(latest build tag - latest commit SHA)

Documentation

Overview

Package build provides details of the built binary The details are set using ldflags.

The ldflags can be set manually for testing locally: `go build -ldflags "-X github.com/srimaln91/go-make.version=$(git describe --tags) -X github.com/srimaln91/go-make.date=$(date -u +%Y-%m-%d-%H:%M:%S-%Z)"`

Index

Constants

This section is empty.

Variables

View Source
var (
	PRINTER_TABLE       = TablePrinter{Writer: os.Stdout}
	PRINTER_STRING      = StringPrinter{Writer: os.Stdout}
	PRINTER_SINGLE_LINE = SingleLinePrinter{Writer: os.Stdout}
)

Functions

func CheckVersion

func CheckVersion(printer ...Printer)

CheckVersion checks --version os argument and prints the binary build details in the console

func JSON

func JSON() ([]byte, error)

JSON returns build details as a JSON string

func String

func String() string

String returns build details as a string with formatting suitable for console output. Ex: Build Details:

Version: v0.5.0
Go Runtime: go1.12.9
Git Commit: bc2e7ce8edc4aa85cc258890e0e4381630cbf5f8
OS/Arch: linux/amd64
Built On: 2019-10-05-12:17:29-UTC

func Table

func Table() string

Table returns build details as a table Suitable for console output Ex:

+----------------+------------+------------------------------------------+-------------+-------------------------+ | BINARY VERSION | GO VERSION | GIT COMMIT | OS/ARCH | BUILT | +----------------+------------+------------------------------------------+-------------+-------------------------+ | v0.5.0-dirty | go1.12.9 | bc2e7ce8edc4aa85cc258890e0e4381630cbf5f8 | linux/amd64 | 2019-10-05-12:17:29-UTC | +----------------+------------+------------------------------------------+-------------+-------------------------+

Types

type Details

type Details struct {
	Version   string `json:"version,omitempty"`
	GoRuntime string `json:"go_runtime,omitempty"`
	GitCommit string `json:"git_commit,omitempty"`
	OSArch    string `json:"os_arch,omitempty"`
	Date      string `json:"date,omitempty"`
}

Details represents known data for a given build

func Data

func Data() Details

Data returns build details as a struct

type Printer added in v1.2.0

type Printer interface {
	Print(details Details) error
}

type SingleLinePrinter added in v1.2.0

type SingleLinePrinter struct {
	Writer io.Writer
}

func (SingleLinePrinter) Print added in v1.2.0

func (p SingleLinePrinter) Print(details Details) error

type StringPrinter added in v1.2.0

type StringPrinter struct {
	Writer io.Writer
}

func (StringPrinter) Print added in v1.2.0

func (p StringPrinter) Print(details Details) error

type TablePrinter added in v1.2.0

type TablePrinter struct {
	Writer io.Writer
}

func (TablePrinter) Print added in v1.2.0

func (p TablePrinter) Print(details Details) error

Jump to

Keyboard shortcuts

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