Skip to content

Commit 0ef8ad2

Browse files
authored
remove packr in favor of embed (#761)
1 parent 8b07f75 commit 0ef8ad2

File tree

8 files changed

+25
-627
lines changed

8 files changed

+25
-627
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
goldilocks
22
cover*
33
goreleaser.deb
4-
*-packr.go
54
dist
6-
packrd
75
coverage.txt
86
*.out
97
debug

.goreleaser.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
before:
22
hooks:
33
- go mod tidy
4-
- go install github.com/gobuffalo/packr/v2/packr2@latest
5-
- packr2
64
builds:
75
- id: goldilocks
86
ldflags:

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ clean:
2424
$(GOCLEAN)
2525
$(GOCMD) fmt ./...
2626
rm -f $(BINARY_NAME)
27-
packr2 clean
2827
rm -rf e2e/results/*
2928
rm *-report*
3029
rm coverage.txt

go.mod

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ go 1.22.7
55
require (
66
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
77
github.com/fairwindsops/controller-utils v0.3.4
8-
github.com/gobuffalo/packr/v2 v2.8.3
98
github.com/google/uuid v1.6.0
109
github.com/gorilla/mux v1.8.1
1110
github.com/samber/lo v1.47.0
@@ -28,8 +27,6 @@ require (
2827
github.com/go-openapi/jsonpointer v0.19.6 // indirect
2928
github.com/go-openapi/jsonreference v0.20.2 // indirect
3029
github.com/go-openapi/swag v0.22.4 // indirect
31-
github.com/gobuffalo/logger v1.0.6 // indirect
32-
github.com/gobuffalo/packd v1.0.1 // indirect
3330
github.com/gogo/protobuf v1.3.2 // indirect
3431
github.com/golang/protobuf v1.5.4 // indirect
3532
github.com/google/gnostic-models v0.6.8 // indirect
@@ -39,19 +36,13 @@ require (
3936
github.com/inconshreveable/mousetrap v1.1.0 // indirect
4037
github.com/josharian/intern v1.0.0 // indirect
4138
github.com/json-iterator/go v1.1.12 // indirect
42-
github.com/karrick/godirwalk v1.16.1 // indirect
4339
github.com/mailru/easyjson v0.7.7 // indirect
44-
github.com/markbates/errx v1.1.0 // indirect
45-
github.com/markbates/oncer v1.0.0 // indirect
46-
github.com/markbates/safe v1.0.1 // indirect
4740
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
4841
github.com/modern-go/reflect2 v1.0.2 // indirect
4942
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
5043
github.com/pkg/errors v0.9.1 // indirect
5144
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
52-
github.com/sirupsen/logrus v1.8.1 // indirect
5345
github.com/x448/float16 v0.8.4 // indirect
54-
golang.org/x/crypto v0.31.0 // indirect
5546
golang.org/x/net v0.33.0 // indirect
5647
golang.org/x/oauth2 v0.21.0 // indirect
5748
golang.org/x/sys v0.28.0 // indirect

go.sum

Lines changed: 3 additions & 560 deletions
Large diffs are not rendered by default.

pkg/dashboard/assets.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
11
package dashboard
22

33
import (
4+
"embed"
45
"net/http"
56

6-
"github.com/gobuffalo/packr/v2"
77
"k8s.io/klog/v2"
88
)
99

10-
var assetBox = (*packr.Box)(nil)
11-
12-
// getAssetBox returns a binary-friendly set of assets packaged from disk
13-
func getAssetBox() *packr.Box {
14-
if assetBox == (*packr.Box)(nil) {
15-
assetBox = packr.New("Assets", "assets")
16-
}
17-
return assetBox
18-
}
10+
//go:embed all:assets
11+
var assetsFS embed.FS
1912

2013
// Asset replies with the contents of the loaded asset from disk
2114
func Asset(assetPath string) http.Handler {
2215
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
23-
asset, err := getAssetBox().Find(assetPath)
16+
asset, err := assetsFS.ReadFile(assetPath)
2417
if err != nil {
2518
klog.Errorf("Error getting asset: %v", err)
2619
http.Error(w, "Error getting asset", http.StatusInternalServerError)
@@ -32,9 +25,3 @@ func Asset(assetPath string) http.Handler {
3225
}
3326
})
3427
}
35-
36-
// StaticAssets replies with a FileServer for all assets, the prefix is used to strip the URL path
37-
func StaticAssets(prefix string) http.Handler {
38-
klog.V(3).Infof("stripping prefix: %s", prefix)
39-
return http.StripPrefix(prefix, http.FileServer(getAssetBox()))
40-
}

pkg/dashboard/router.go

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,15 @@
1515
package dashboard
1616

1717
import (
18+
"io/fs"
1819
"net/http"
1920
"path"
2021
"strings"
2122

22-
"k8s.io/klog/v2"
23-
24-
packr "github.com/gobuffalo/packr/v2"
2523
"github.com/gorilla/mux"
24+
"k8s.io/klog/v2"
2625
)
2726

28-
var (
29-
markdownBox = (*packr.Box)(nil)
30-
)
31-
32-
// GetMarkdownBox returns a binary-friendly set of markdown files with error details
33-
func GetMarkdownBox() *packr.Box {
34-
if markdownBox == (*packr.Box)(nil) {
35-
markdownBox = packr.New("Markdown", "../../docs")
36-
}
37-
return markdownBox
38-
}
39-
40-
func GetAssetBox() *packr.Box {
41-
if assetBox == (*packr.Box)(nil) {
42-
assetBox = packr.New("Assets", "assets")
43-
}
44-
return assetBox
45-
}
46-
4727
// GetRouter returns a mux router serving all routes necessary for the dashboard
4828
func GetRouter(setters ...Option) *mux.Router {
4929
opts := defaultOptions()
@@ -58,8 +38,18 @@ func GetRouter(setters ...Option) *mux.Router {
5838
router.Handle("/healthz", Healthz())
5939

6040
// assets
61-
router.Handle("/favicon.ico", Asset("/images/favicon-32x32.png"))
62-
fileServer := http.FileServer(GetAssetBox())
41+
router.Handle("/favicon.ico", Asset("assets/images/favicon-32x32.png"))
42+
43+
subAssetsFS, err := fs.Sub(assetsFS, "assets")
44+
if err != nil {
45+
klog.Fatalf("error creating sub filesystem for assets: %v", err)
46+
}
47+
48+
if subAssetsFS == nil {
49+
klog.Fatal("subAssetsFS is nil, this should not happen")
50+
}
51+
52+
fileServer := http.FileServerFS(subAssetsFS)
6353
router.PathPrefix("/static/").Handler(http.StripPrefix(path.Join(opts.BasePath, "/static/"), fileServer))
6454

6555
// dashboard

pkg/dashboard/templates.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,17 @@ package dashboard
22

33
import (
44
"bytes"
5+
"embed"
56
"encoding/json"
67
"fmt"
78
"html/template"
89
"net/http"
910
"strings"
1011

1112
"github.com/fairwindsops/goldilocks/pkg/dashboard/helpers"
12-
"github.com/gobuffalo/packr/v2"
1313
"k8s.io/klog/v2"
1414
)
1515

16-
var templateBox = (*packr.Box)(nil)
17-
1816
// templates
1917
const (
2018
ContainerTemplateName = "container.gohtml"
@@ -50,13 +48,8 @@ type baseTemplateData struct {
5048
JSON template.JS
5149
}
5250

53-
// getTemplateBox returns a binary-friendly set of templates for rendering the dash
54-
func getTemplateBox() *packr.Box {
55-
if templateBox == (*packr.Box)(nil) {
56-
templateBox = packr.New("Templates", "templates")
57-
}
58-
return templateBox
59-
}
51+
//go:embed all:templates
52+
var templatedFS embed.FS
6053

6154
// getTemplate puts together a template. Individual pieces can be overridden before rendering.
6255
func getTemplate(name string, opts Options, includedTemplates ...string) (*template.Template, error) {
@@ -83,9 +76,8 @@ func getTemplate(name string, opts Options, includedTemplates ...string) (*templ
8376

8477
// parseTemplateFiles combines the template with the included templates into one parsed template
8578
func parseTemplateFiles(tmpl *template.Template, includedTemplates []string) (*template.Template, error) {
86-
templateBox := getTemplateBox()
8779
for _, fname := range includedTemplates {
88-
templateFile, err := templateBox.Find(fmt.Sprintf("%s.gohtml", fname))
80+
templateFile, err := templatedFS.ReadFile(fmt.Sprintf("templates/%s.gohtml", fname))
8981
if err != nil {
9082
return nil, err
9183
}

0 commit comments

Comments
 (0)