Skip to content

Commit

Permalink
replace statik with go:embed (#44)
Browse files Browse the repository at this point in the history
* replace statik with go:embed

* no longer required

* linter needs go 1.16 as well

* try with newer golangci-lint version

* try with newer golangci-lint version

* try with newer golangci-lint version

* try with newer golangci-lint version

* make golangci-lint run -p bugs happy

* Adopt linter changes

* simplify version header handling in test

Co-authored-by: Markus Fensterer <[email protected]>
  • Loading branch information
majst01 and mwindower authored Apr 26, 2021
1 parent fdd79ac commit fbc3f9d
Show file tree
Hide file tree
Showing 16 changed files with 246 additions and 187 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Go 1.16
uses: actions/[email protected]
with:
go-version: '1.16.x'
- name: Lint
uses: golangci/golangci-lint-action@v2
- name: Set up Go 1.15
uses: actions/[email protected]
with:
go-version: '1.15.x'
args: -p bugs
- name: Build project
run: make
- name: Docker Login
Expand Down
8 changes: 5 additions & 3 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@master
- name: Set up Go 1.16
uses: actions/[email protected]
with:
go-version: '1.16.x'
- name: Lint
uses: golangci/golangci-lint-action@v2
- name: Set up Go 1.15
uses: actions/[email protected]
with:
go-version: '1.15.x'
args: -p bugs
- name: Build project
run: make
- name: Docker Login
Expand Down
8 changes: 5 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@master
- name: Set up Go 1.16
uses: actions/[email protected]
with:
go-version: '1.16.x'
- name: Lint
uses: golangci/golangci-lint-action@v2
- name: Set up Go 1.15
uses: actions/[email protected]
with:
go-version: '1.15.x'
args: -p bugs
- name: Build project
run: make
- name: Create Release
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
.idea/
bin/
metal-networker.tgz
**/*/statik.go
25 changes: 2 additions & 23 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ endif
all:: release;

.PHONY: test
test: generate
go test -ldflags "-X 'github.com/metal-stack/v.Version='" -v -cover ./...
test:
GO_ENV=testing go test -ldflags -v -cover ./...

.PHONY: all
bin/$(BINARY): test
Expand All @@ -46,31 +46,10 @@ release: bin/$(BINARY) validate
validate:
./validate.sh

.PHONY: .generate
generate: statik
$(STATIK) -src=pkg/netconf/tpl -include='*.tpl' -dest=pkg/netconf/tpl

# Build the docker image
docker-build:
docker build . -t ${DOCKER_IMG}

# Push the docker image
docker-push:
docker push ${DOCKER_IMG}

# find or download statik
.PHONY: statik
statik:
ifeq (, $(shell which statik))
@{ \
set -e ;\
STATIK_TMP_DIR=$$(mktemp -d) ;\
cd $$STATIK_TMP_DIR ;\
go mod init tmp ;\
go get github.com/rakyll/statik ;\
rm -rf $$STATIK_TMP_DIR ;\
}
STATIK=$(GOBIN)/statik
else
STATIK=$(shell which statik)
endif
25 changes: 12 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
module github.com/metal-stack/metal-networker

go 1.15
go 1.16

require (
github.com/coreos/go-systemd/v22 v22.1.0
github.com/google/go-cmp v0.5.4
github.com/magiconair/properties v1.8.4 // indirect
github.com/metal-stack/metal-go v0.13.0
github.com/metal-stack/metal-lib v0.6.9
github.com/metal-stack/v v1.0.2
github.com/mitchellh/mapstructure v1.3.3 // indirect
github.com/coreos/go-systemd/v22 v22.3.1
github.com/google/go-cmp v0.5.5
github.com/magiconair/properties v1.8.5 // indirect
github.com/metal-stack/metal-go v0.14.0
github.com/metal-stack/metal-lib v0.7.2
github.com/metal-stack/v v1.0.3
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/pelletier/go-toml v1.8.1 // indirect
github.com/rakyll/statik v0.1.7
github.com/spf13/afero v1.4.1 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/cobra v1.1.1
github.com/spf13/cobra v1.1.3
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.7.0
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.16.0
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c // indirect
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
inet.af/netaddr v0.0.0-20210129185718-d0669448cef6
inet.af/netaddr v0.0.0-20210403172118-1e1430f727e0
)
308 changes: 196 additions & 112 deletions go.sum

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pkg/net/applier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func TestNetworkApplier_Compare(t *testing.T) {
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
n := &NetworkApplier{}
if got := n.Compare(tt.source, tt.target); got != tt.want {
Expand Down
1 change: 1 addition & 0 deletions pkg/netconf/frr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func TestFrrConfigApplier(t *testing.T) {
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
expected, err := ioutil.ReadFile(test.expectedOutput)
assert.NoError(t, err)
Expand Down
7 changes: 6 additions & 1 deletion pkg/netconf/knowledgebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"net"
"os"

"github.com/metal-stack/metal-go/api/models"
mn "github.com/metal-stack/metal-lib/pkg/net"
Expand Down Expand Up @@ -231,6 +232,10 @@ func (kb KnowledgeBase) allNonUnderlayNetworksHaveNonZeroVRF() bool {
}

func versionHeader(uuid string) string {
version := v.V.String()
if os.Getenv("GO_ENV") == "testing" {
version = ""
}
return fmt.Sprintf("# This file was auto generated for machine: '%s' by app version %s.\n# Do not edit.",
uuid, v.V.String())
uuid, version)
}
2 changes: 2 additions & 0 deletions pkg/netconf/knowledgebase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ func TestKnowledgeBase_Validate(t *testing.T) {
}

for i, test := range tests {
test := test
for _, kind := range test.kinds {
kind := kind
t.Run(fmt.Sprintf("testcase %d - kind %v", i, kind), func(t *testing.T) {
actualErr := test.kb.Validate(kind)
if test.expectedErrMsg == "" {
Expand Down
7 changes: 2 additions & 5 deletions pkg/netconf/nftables.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"github.com/metal-stack/metal-networker/pkg/exec"
"inet.af/netaddr"

"github.com/metal-stack/v"

"github.com/metal-stack/metal-networker/pkg/net"

mn "github.com/metal-stack/metal-lib/pkg/net"
Expand Down Expand Up @@ -46,9 +44,8 @@ type (
// NewNftablesConfigApplier constructs a new instance of this type.
func NewNftablesConfigApplier(kb KnowledgeBase, validator net.Validator) net.Applier {
data := NftablesData{
Comment: fmt.Sprintf("# This file was auto generated for machine: '%s' by app version %s.\n"+
"# Do not edit.", kb.Machineuuid, v.V.String()),
SNAT: getSNAT(kb),
Comment: versionHeader(kb.Machineuuid),
SNAT: getSNAT(kb),
}

return net.NewNetworkApplier(data, validator, nil)
Expand Down
1 change: 1 addition & 0 deletions pkg/netconf/nftables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestCompileNftRules(t *testing.T) {
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.input, func(t *testing.T) {
expected, err := ioutil.ReadFile(tt.expected)
assert.NoError(err)
Expand Down
1 change: 1 addition & 0 deletions pkg/netconf/routemap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ func Test_importRulesForNetwork(t *testing.T) {
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
kb := NewKnowledgeBase(tt.input)
err := kb.Validate(Firewall)
Expand Down
25 changes: 7 additions & 18 deletions pkg/netconf/template.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,20 @@
package netconf

import (
// including template files with statik
"io/ioutil"
"embed"
"path"
"text/template"

_ "github.com/metal-stack/metal-networker/pkg/netconf/tpl/statik"
"github.com/rakyll/statik/fs"
)

func mustReadTpl(tplName string) string {
statikFS, err := fs.New()
if err != nil {
log.Panic(err)
}
//go:embed tpl
var templates embed.FS

r, err := statikFS.Open("/" + tplName)
if err != nil {
log.Panic(err)
}
defer r.Close()

contents, err := ioutil.ReadAll(r)
func mustReadTpl(tplName string) string {
contents, err := templates.ReadFile(path.Join("tpl", tplName))
if err != nil {
log.Panic(err)
return ""
}

return string(contents)
}

Expand Down
5 changes: 0 additions & 5 deletions pkg/netconf/tpl/statik/doc.go

This file was deleted.

0 comments on commit fbc3f9d

Please sign in to comment.