From fcbbd3191ef2aeb775b174cf4674400f298de068 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Fernando=20Carri=C3=B3n?= Date: Fri, 27 Oct 2023 13:15:10 +0200 Subject: [PATCH] build with make & inject version on build --- .gitignore | 2 ++ .gitlab-ci.yml | 8 ++++---- Makefile | 34 ++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ main.go | 43 +++++++++++++++++++++---------------------- 6 files changed, 64 insertions(+), 26 deletions(-) create mode 100644 .gitignore create mode 100644 Makefile diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..42cc387 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +spry +spry.*.* diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index d7a4897..ee9595e 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -25,10 +25,10 @@ build-job: # This job runs in the build stage, which runs first. image: golang:1.21 script: - echo "Compiling..." - - GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-extldflags=-static" -o spry.linux.amd64 - - GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -ldflags="-extldflags=-static" -o spry.linux.arm64 - - GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-extldflags=-static" -o spry.darwin.amd64 - - GOOS=darwin GOARCH=arm64 CGO_ENABLED=0 go build -ldflags="-extldflags=-static" -o spry.darwin.arm64 + - GOOS=linux GOARCH=amd64 CGO_ENABLED=0 make build + - GOOS=linux GOARCH=arm64 CGO_ENABLED=0 make build + - GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 make build + - GOOS=darwin GOARCH=arm64 CGO_ENABLED=0 make build - echo "Compile complete." artifacts: untracked: false diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f8ae522 --- /dev/null +++ b/Makefile @@ -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 diff --git a/go.mod b/go.mod index 128e207..88caed1 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/Masterminds/sprig/v3 v3.2.3 github.com/peterbourgon/mergemap v0.0.1 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 ) diff --git a/go.sum b/go.sum index f8982bb..ab1c8aa 100644 --- a/go.sum +++ b/go.sum @@ -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= 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/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-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= diff --git a/main.go b/main.go index 624bd45..ca9006c 100644 --- a/main.go +++ b/main.go @@ -18,28 +18,10 @@ import ( yaml "gopkg.in/yaml.v3" htl "gitlab.com/CRThaze/helm-tmpl-lang" + s "gitlab.com/CRThaze/sugar" ) -type StrSet map[string]struct{} - -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 -} +var Version string = "0.0.0" type valFileSlice []string @@ -137,7 +119,14 @@ func init() { "filenameErr", true, "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() { 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") } + + 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) { @@ -180,7 +179,7 @@ func checkErr(err error) { func expandFiles(files []string) (expFiles []string) { expFiles = []string{} - fileSet := StrSet{} + fileSet := s.StrSet{} directories := []string{} // Ensure we don't process any provided values files as templates.