build with make & inject version on build

This commit is contained in:
Diego Fernando Carrión 2023-10-27 13:15:10 +02:00
parent 0b5646cf68
commit fcbbd3191e
No known key found for this signature in database
GPG Key ID: 811EF2E03998BFC4
6 changed files with 64 additions and 26 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
spry
spry.*.*

View File

@ -25,10 +25,10 @@ build-job: # This job runs in the build stage, which runs first.
image: golang:1.21 image: golang:1.21
script: script:
- echo "Compiling..." - echo "Compiling..."
- GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-extldflags=-static" -o spry.linux.amd64 - GOOS=linux GOARCH=amd64 CGO_ENABLED=0 make build
- GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -ldflags="-extldflags=-static" -o spry.linux.arm64 - GOOS=linux GOARCH=arm64 CGO_ENABLED=0 make build
- GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-extldflags=-static" -o spry.darwin.amd64 - GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 make build
- GOOS=darwin GOARCH=arm64 CGO_ENABLED=0 go build -ldflags="-extldflags=-static" -o spry.darwin.arm64 - GOOS=darwin GOARCH=arm64 CGO_ENABLED=0 make build
- echo "Compile complete." - echo "Compile complete."
artifacts: artifacts:
untracked: false untracked: false

34
Makefile Normal file
View File

@ -0,0 +1,34 @@
.DEFAULT_GOAL := all
#############
# Variables #
#############
EXECUTABLE := spry
ifdef GOOS
EXECUTABLE := $(EXECUTABLE).$(GOOS)
endif
ifdef GOARCH
EXECUTABLE := $(EXECUTABLE).$(GOARCH)
endif
VERSION ?= $(shell ./.version/calculate-version.sh)
###########
# TARGETS #
###########
$(EXECUTABLE):
go build -ldflags="-extldflags=-static -X main.Version=$(VERSION)" -o $(EXECUTABLE) .
.PHONY: clean-build
clean-build:
rm -f $(EXECUTABLE)
.PHONY: build
build: clean-build $(EXECUTABLE)
.PHONY: all
all: build

1
go.mod
View File

@ -6,6 +6,7 @@ require (
github.com/Masterminds/sprig/v3 v3.2.3 github.com/Masterminds/sprig/v3 v3.2.3
github.com/peterbourgon/mergemap v0.0.1 github.com/peterbourgon/mergemap v0.0.1
gitlab.com/CRThaze/helm-tmpl-lang v0.0.0-20231023121015-db8999e64395 gitlab.com/CRThaze/helm-tmpl-lang v0.0.0-20231023121015-db8999e64395
gitlab.com/CRThaze/sugar v0.0.2
gopkg.in/yaml.v3 v3.0.1 gopkg.in/yaml.v3 v3.0.1
) )

2
go.sum
View File

@ -126,6 +126,8 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
gitlab.com/CRThaze/helm-tmpl-lang v0.0.0-20231023121015-db8999e64395 h1:gRGvQGJhtG5dHV7F2C+amtdhyT5eE195lsdpz9ew37o= gitlab.com/CRThaze/helm-tmpl-lang v0.0.0-20231023121015-db8999e64395 h1:gRGvQGJhtG5dHV7F2C+amtdhyT5eE195lsdpz9ew37o=
gitlab.com/CRThaze/helm-tmpl-lang v0.0.0-20231023121015-db8999e64395/go.mod h1:AJdDOAsr2yJZ5j4k5KGeBb/gcXbwpRzGmELX4fZWFQY= gitlab.com/CRThaze/helm-tmpl-lang v0.0.0-20231023121015-db8999e64395/go.mod h1:AJdDOAsr2yJZ5j4k5KGeBb/gcXbwpRzGmELX4fZWFQY=
gitlab.com/CRThaze/sugar v0.0.2 h1:kJvrruckSF15kj9v8HtHFusCmFmOCdHbcHZGgwyw6G8=
gitlab.com/CRThaze/sugar v0.0.2/go.mod h1:iEbJlix4YEoPONzHzzXMptItMeLeItVMvp0TdEmAzBE=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=

43
main.go
View File

@ -18,28 +18,10 @@ import (
yaml "gopkg.in/yaml.v3" yaml "gopkg.in/yaml.v3"
htl "gitlab.com/CRThaze/helm-tmpl-lang" htl "gitlab.com/CRThaze/helm-tmpl-lang"
s "gitlab.com/CRThaze/sugar"
) )
type StrSet map[string]struct{} var Version string = "0.0.0"
func (s *StrSet) Add(str string) {
(*s)[str] = struct{}{}
}
func (s *StrSet) Has(str string) bool {
if _, ok := (*s)[str]; ok {
return true
}
return false
}
func (s *StrSet) AddHas(str string) bool {
if ok := s.Has(str); ok {
return ok
}
s.Add(str)
return false
}
type valFileSlice []string type valFileSlice []string
@ -137,7 +119,14 @@ func init() {
"filenameErr", true, "filenameErr", true,
"Whether to print filename to STDERR.", "Whether to print filename to STDERR.",
) )
flag.Parse() shortVersion := flag.Bool(
"v", false,
"Print the version and exit.",
)
longVersion := flag.Bool(
"version", false,
"Print the version and exit.",
)
flag.Usage = func() { flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s [Options] [Template Files]:\n", os.Args[0]) fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s [Options] [Template Files]:\n", os.Args[0])
@ -169,6 +158,16 @@ func init() {
) )
fmt.Fprintf(flag.CommandLine.Output(), "\n") fmt.Fprintf(flag.CommandLine.Output(), "\n")
} }
flag.Parse()
if *shortVersion || *longVersion {
fmt.Fprintf(
flag.CommandLine.Output(),
"%s - Version %s\n\n", os.Args[0], Version,
)
os.Exit(0)
}
} }
func checkErr(err error) { func checkErr(err error) {
@ -180,7 +179,7 @@ func checkErr(err error) {
func expandFiles(files []string) (expFiles []string) { func expandFiles(files []string) (expFiles []string) {
expFiles = []string{} expFiles = []string{}
fileSet := StrSet{} fileSet := s.StrSet{}
directories := []string{} directories := []string{}
// Ensure we don't process any provided values files as templates. // Ensure we don't process any provided values files as templates.