Skip to content

Commit 63b7f11

Browse files
committed
feat: create templates archive on go generate
1 parent 876b0dd commit 63b7f11

File tree

2 files changed

+92
-9
lines changed

2 files changed

+92
-9
lines changed

Makefile

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
REPO := quay.io/boson/func
2-
BIN := func
32

4-
DARWIN=$(BIN)_darwin_amd64
5-
LINUX=$(BIN)_linux_amd64
6-
WINDOWS=$(BIN)_windows_amd64.exe
3+
BIN := func
4+
DARWIN :=$(BIN)_darwin_amd64
5+
LINUX :=$(BIN)_linux_amd64
6+
WINDOWS :=$(BIN)_windows_amd64.exe
77

88
CODE := $(shell find . -name '*.go')
99
DATE := $(shell date -u +"%Y%m%dT%H%M%SZ")
@@ -14,6 +14,8 @@ VTAG := $(shell git tag --points-at HEAD)
1414
# unless explicitly, synchronously tagging as is done in ci.yaml
1515
VERS ?= $(shell [ -z $(VTAG) ] && echo 'tip' || echo $(VTAG) )
1616

17+
LDFLAGS := -X main.date=$(DATE) -X main.vers=$(VERS) -X main.hash=$(HASH)
18+
1719
build: all
1820
all: $(BIN)
1921

@@ -33,16 +35,20 @@ linux: $(LINUX) ## Build for Linux
3335
windows: $(WINDOWS) ## Build for Windows
3436

3537
$(BIN): $(CODE) ## Build using environment defaults
36-
env CGO_ENABLED=0 go build -ldflags "-X main.date=$(DATE) -X main.vers=$(VERS) -X main.hash=$(HASH)" ./cmd/$(BIN)
38+
go generate
39+
env CGO_ENABLED=0 go build -ldflags "$(LDFLAGS)" ./cmd/$(BIN)
3740

3841
$(DARWIN):
39-
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)
42+
go generate
43+
env CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o $(DARWIN) -ldflags "$(LDFLAGS)" ./cmd/$(BIN)
4044

4145
$(LINUX):
42-
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)
46+
go generate
47+
env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o $(LINUX) -ldflags "$(LDFLAGS)" ./cmd/$(BIN)
4348

4449
$(WINDOWS):
45-
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)
50+
go generate
51+
env CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o $(WINDOWS) -ldflags "$(LDFLAGS)" ./cmd/$(BIN)
4652

4753
test: test-binary test-node test-python test-quarkus test-go
4854

@@ -102,5 +108,5 @@ cluster: ## Set up a local cluster for integraiton tests.
102108
./hack/allocate.sh && ./hack/configure.sh
103109

104110
clean:
105-
rm -f $(BIN) $(WINDOWS) $(LINUX) $(DARWIN)
111+
rm -f $(BIN) $(WINDOWS) $(LINUX) $(DARWIN) $(TEMPLATES)
106112
-rm -f coverage.out

generate.go

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// +build generate
2+
3+
package main
4+
5+
import (
6+
"archive/tar"
7+
"compress/gzip"
8+
"fmt"
9+
"io"
10+
"io/fs"
11+
"os"
12+
"path/filepath"
13+
)
14+
15+
const (
16+
archive = "templates.tgz"
17+
files = "templates"
18+
)
19+
20+
// on 'go generate' create templates archive (tar -czf templates.tgz templates)
21+
func main() {
22+
if err := create(archive, files); err != nil {
23+
fmt.Fprintln(os.Stderr, err)
24+
os.Exit(1)
25+
}
26+
}
27+
28+
func create(name, source string) (err error) {
29+
// Create file on disk
30+
tarball, err := os.Create(name)
31+
if err != nil {
32+
return
33+
}
34+
defer tarball.Close()
35+
36+
// A gzip compressor which writes to the file
37+
compressor := gzip.NewWriter(tarball)
38+
defer compressor.Close()
39+
40+
// A tar writer which writes to gzip compressor
41+
w := tar.NewWriter(compressor)
42+
defer w.Close()
43+
44+
// File walking function which writes tar entries for each file.
45+
return filepath.WalkDir(source, func(path string, d fs.DirEntry, e error) (err error) {
46+
if e != nil {
47+
return e // abort on any failed ReadDir calls.
48+
}
49+
50+
// Header
51+
fi, err := d.Info()
52+
if err != nil {
53+
return
54+
}
55+
h, err := tar.FileInfoHeader(fi, d.Name())
56+
if err != nil {
57+
return
58+
}
59+
h.Name = filepath.ToSlash(path)
60+
if err = w.WriteHeader(h); err != nil {
61+
return
62+
}
63+
64+
// Done if directory
65+
if d.IsDir() {
66+
return
67+
}
68+
69+
// Data
70+
data, err := os.Open(path)
71+
if err != nil {
72+
return
73+
}
74+
_, err = io.Copy(w, data)
75+
return
76+
})
77+
}

0 commit comments

Comments
 (0)