Enhance build taget into Makefile, add --version support

This commit is contained in:
RouxAntoine 2022-01-08 02:35:24 +01:00
parent 32c218f8ce
commit c6230ca794
Signed by: antoine
GPG Key ID: 098FB66FC0475E70
5 changed files with 70 additions and 3 deletions

4
.gitignore vendored
View File

@ -1 +1,3 @@
docker-multi-arch-builder
# Ignore compilation result
bin/

View File

@ -1,5 +1,20 @@
.PHONY: build
GOARCH=amd64
# GOARCH=arm
GOOS=darwin
# GOOS=linux
LDFLAGS=-w -s -X go/weather/internal/version.Version=$$(git rev-list -1 HEAD)
GO_BUILD_FLAGS=-tags dev
CGO_ENABLED=0
build:
go build
build: dependencies
@echo "build for os $$GOOS and arch $$GOARCH"
go build -o bin/docker-multi-arch-builder-$(GOOS)-$(GOARCH) -ldflags="$(LDFLAGS)" $(GO_BUILD_FLAGS) main.go
dependencies:
go mod download
go mod verify
version:
./bin/docker-multi-arch-builder-darwin-amd64 --version

View File

@ -1,6 +1,7 @@
package cmd
import (
. "antoine-roux.tk/docker-multi-arch-builder/internal/version"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
@ -15,6 +16,7 @@ var (
Short: "dmab is an oci multi architecture builder",
Long: `docker multi architecture builder is a go program use to build docker manifest
with list of layer. Each layer point to docker image with annotated os, arch and variant information`,
Version: Version,
}
)

21
internal/version/dev.go Normal file
View File

@ -0,0 +1,21 @@
//go:build dev
package version
import (
"fmt"
)
//Version ...
var Version = "0.1.0"
//Prerelease such as "dev" (in development), "beta", "rc1", etc.
var Prerelease = "dev"
// String returns the complete version string, including prerelease
func String() string {
if Prerelease != "" {
return fmt.Sprintf("%s-%s", Version, Prerelease)
}
return Version
}

27
internal/version/prod.go Normal file
View File

@ -0,0 +1,27 @@
//go:build prod
package version
import (
"fmt"
version "github.com/hashicorp/go-version"
)
//Version ...
var Version = "1.0.0"
//Prerelease such as "dev" (in development), "beta", "rc1", etc.
var Prerelease = "prod"
//SemVer management
var SemVer *version.Version
func init() {
SemVer = version.Must(version.NewSemver(fmt.Sprintf("%s-%s", Version, Prerelease)))
}
// String returns the complete version string, including prerelease
func String() string {
return SemVer.String()
}