feat: create templates archive on go generate

This commit is contained in:
Luke Kingland 2021-03-22 23:41:07 +09:00
parent 876b0dd0f7
commit 63b7f11471
No known key found for this signature in database
GPG Key ID: 4896F75BAF2E1966
2 changed files with 92 additions and 9 deletions

View File

@ -1,9 +1,9 @@
REPO := quay.io/boson/func REPO := quay.io/boson/func
BIN := func
DARWIN=$(BIN)_darwin_amd64 BIN := func
LINUX=$(BIN)_linux_amd64 DARWIN :=$(BIN)_darwin_amd64
WINDOWS=$(BIN)_windows_amd64.exe LINUX :=$(BIN)_linux_amd64
WINDOWS :=$(BIN)_windows_amd64.exe
CODE := $(shell find . -name '*.go') CODE := $(shell find . -name '*.go')
DATE := $(shell date -u +"%Y%m%dT%H%M%SZ") DATE := $(shell date -u +"%Y%m%dT%H%M%SZ")
@ -14,6 +14,8 @@ VTAG := $(shell git tag --points-at HEAD)
# unless explicitly, synchronously tagging as is done in ci.yaml # unless explicitly, synchronously tagging as is done in ci.yaml
VERS ?= $(shell [ -z $(VTAG) ] && echo 'tip' || echo $(VTAG) ) VERS ?= $(shell [ -z $(VTAG) ] && echo 'tip' || echo $(VTAG) )
LDFLAGS := -X main.date=$(DATE) -X main.vers=$(VERS) -X main.hash=$(HASH)
build: all build: all
all: $(BIN) all: $(BIN)
@ -33,16 +35,20 @@ linux: $(LINUX) ## Build for Linux
windows: $(WINDOWS) ## Build for Windows windows: $(WINDOWS) ## Build for Windows
$(BIN): $(CODE) ## Build using environment defaults $(BIN): $(CODE) ## Build using environment defaults
env CGO_ENABLED=0 go build -ldflags "-X main.date=$(DATE) -X main.vers=$(VERS) -X main.hash=$(HASH)" ./cmd/$(BIN) go generate
env CGO_ENABLED=0 go build -ldflags "$(LDFLAGS)" ./cmd/$(BIN)
$(DARWIN): $(DARWIN):
env CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o $(DARWIN) -ldflags "-X main.date=$(DATE) -X main.vers=$(VERS) -X main.hash=$(HASH)" ./cmd/$(BIN) go generate
env CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o $(DARWIN) -ldflags "$(LDFLAGS)" ./cmd/$(BIN)
$(LINUX): $(LINUX):
env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o $(LINUX) -ldflags "-X main.date=$(DATE) -X main.vers=$(VERS) -X main.hash=$(HASH)" ./cmd/$(BIN) go generate
env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o $(LINUX) -ldflags "$(LDFLAGS)" ./cmd/$(BIN)
$(WINDOWS): $(WINDOWS):
env CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o $(WINDOWS) -ldflags "-X main.date=$(DATE) -X main.vers=$(VERS) -X main.hash=$(HASH)" ./cmd/$(BIN) go generate
env CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o $(WINDOWS) -ldflags "$(LDFLAGS)" ./cmd/$(BIN)
test: test-binary test-node test-python test-quarkus test-go test: test-binary test-node test-python test-quarkus test-go
@ -102,5 +108,5 @@ cluster: ## Set up a local cluster for integraiton tests.
./hack/allocate.sh && ./hack/configure.sh ./hack/allocate.sh && ./hack/configure.sh
clean: clean:
rm -f $(BIN) $(WINDOWS) $(LINUX) $(DARWIN) rm -f $(BIN) $(WINDOWS) $(LINUX) $(DARWIN) $(TEMPLATES)
-rm -f coverage.out -rm -f coverage.out

77
generate.go Normal file
View File

@ -0,0 +1,77 @@
// +build generate
package main
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
)
const (
archive = "templates.tgz"
files = "templates"
)
// on 'go generate' create templates archive (tar -czf templates.tgz templates)
func main() {
if err := create(archive, files); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func create(name, source string) (err error) {
// Create file on disk
tarball, err := os.Create(name)
if err != nil {
return
}
defer tarball.Close()
// A gzip compressor which writes to the file
compressor := gzip.NewWriter(tarball)
defer compressor.Close()
// A tar writer which writes to gzip compressor
w := tar.NewWriter(compressor)
defer w.Close()
// File walking function which writes tar entries for each file.
return filepath.WalkDir(source, func(path string, d fs.DirEntry, e error) (err error) {
if e != nil {
return e // abort on any failed ReadDir calls.
}
// Header
fi, err := d.Info()
if err != nil {
return
}
h, err := tar.FileInfoHeader(fi, d.Name())
if err != nil {
return
}
h.Name = filepath.ToSlash(path)
if err = w.WriteHeader(h); err != nil {
return
}
// Done if directory
if d.IsDir() {
return
}
// Data
data, err := os.Open(path)
if err != nil {
return
}
_, err = io.Copy(w, data)
return
})
}