From 6f054af768b54fd31519d38a31a96cd37b279dbc Mon Sep 17 00:00:00 2001 From: Honza Pokorny Date: Mon, 15 Mar 2021 08:57:04 -0300 Subject: [PATCH] Replace statik with the new embed feature https://golang.org/pkg/embed/ This makes smithy dependent on Golang 1.16 or higher. --- Makefile | 10 +---- README.md | 2 +- go.mod | 2 +- pkg/smithy/smithy.go | 40 ++++++------------- {include => pkg/smithy/static}/style.css | 0 {include => pkg/smithy/templates}/404.html | 0 {include => pkg/smithy/templates}/500.html | 0 {include => pkg/smithy/templates}/blob.html | 0 {include => pkg/smithy/templates}/commit.html | 0 {include => pkg/smithy/templates}/footer.html | 0 {include => pkg/smithy/templates}/header.html | 0 {include => pkg/smithy/templates}/index.html | 0 {include => pkg/smithy/templates}/log.html | 0 {include => pkg/smithy/templates}/refs.html | 0 .../smithy/templates}/repo-index.html | 0 {include => pkg/smithy/templates}/tree.html | 0 16 files changed, 17 insertions(+), 37 deletions(-) rename {include => pkg/smithy/static}/style.css (100%) rename {include => pkg/smithy/templates}/404.html (100%) rename {include => pkg/smithy/templates}/500.html (100%) rename {include => pkg/smithy/templates}/blob.html (100%) rename {include => pkg/smithy/templates}/commit.html (100%) rename {include => pkg/smithy/templates}/footer.html (100%) rename {include => pkg/smithy/templates}/header.html (100%) rename {include => pkg/smithy/templates}/index.html (100%) rename {include => pkg/smithy/templates}/log.html (100%) rename {include => pkg/smithy/templates}/refs.html (100%) rename {include => pkg/smithy/templates}/repo-index.html (100%) rename {include => pkg/smithy/templates}/tree.html (100%) diff --git a/Makefile b/Makefile index 9ebc233..789e300 100644 --- a/Makefile +++ b/Makefile @@ -13,8 +13,7 @@ export CGO_ENABLED=0 all: smithy smithy.yml -smithy: bin/statik include/*.html - bin/statik -src=include -dest=pkg -f -m +smithy: pkg/smithy/* go build -ldflags $(LDFLAGS) -o smithy main.go smithy.yml: @@ -35,16 +34,11 @@ uninstall: all rm -r $(DESTDIR)$(BINDIR)/smithy rm -fr $(DESTDIR)$(SHAREDIR) -bin/statik: - mkdir -p bin - go mod download - go build -o bin/statik $(MODCACHE)/github.com/rakyll/statik@v0.1.7/statik.go - gofmt: go fmt ./pkg/... ./cmd/... clean: - rm -rf smithy smithy.yml pkg/statik smithy.1 smithy.yml.5 + rm -rf smithy smithy.yml smithy.1 smithy.yml.5 .PHONY: smithy smithy.yml clean diff --git a/README.md b/README.md index 1e209a0..c002701 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ the like. Building ------------------------------------------------------------------------------- -The only dependency is [Golang](https://golang.org/) 1.15 or higher. +The only dependency is [Golang](https://golang.org/) 1.16 or higher. Contributors to smithy should have the optional [scdoc](https://sr.ht/~sircmpwn/scdoc) for generating documentation. diff --git a/go.mod b/go.mod index 2fd36ea..229da4b 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/honza/smithy -go 1.15 +go 1.16 require ( github.com/alecthomas/chroma v0.8.2 diff --git a/pkg/smithy/smithy.go b/pkg/smithy/smithy.go index 839a12a..7dff62d 100644 --- a/pkg/smithy/smithy.go +++ b/pkg/smithy/smithy.go @@ -39,15 +39,20 @@ import ( "github.com/go-git/go-git/v5/plumbing/filemode" "github.com/go-git/go-git/v5/plumbing/object" "github.com/go-git/go-git/v5/plumbing/storer" - "github.com/rakyll/statik/fs" "github.com/yuin/goldmark" - "github.com/yuin/goldmark-highlighting" + highlighting "github.com/yuin/goldmark-highlighting" - githttp "github.com/honza/smithy/pkg/go-git-http" + "embed" - _ "github.com/honza/smithy/pkg/statik" + githttp "github.com/honza/smithy/pkg/go-git-http" ) +//go:embed templates +var templatefiles embed.FS + +//go:embed static +var staticfiles embed.FS + const PAGE_SIZE int = 100 type RepositoryWithName struct { @@ -850,19 +855,12 @@ func InitFileSystemHandler(smithyConfig SmithyConfig) http.Handler { var handler http.Handler if smithyConfig.Static.Root == "" { - fileServer, err := fs.New() - - if err != nil { - return http.NotFoundHandler() - } - - handler = http.FileServer(fileServer) + handler = http.FileServer(http.FS(staticfiles)) } else { handler = http.FileServer(http.Dir(smithyConfig.Static.Root)) + handler = http.StripPrefix(smithyConfig.Static.Prefix, handler) } - handler = http.StripPrefix(smithyConfig.Static.Prefix, handler) - return handler } @@ -917,19 +915,7 @@ func loadTemplates(smithyConfig SmithyConfig) (*template.Template, error) { return t.ParseGlob(smithyConfig.Templates.Dir) } - statikFS, err := fs.New() - - if err != nil { - return t, err - } - - root, err := statikFS.Open("/") - - if err != nil { - return t, err - } - - files, err := root.Readdir(0) + files, err := templatefiles.ReadDir("templates") if err != nil { return t, err @@ -939,7 +925,7 @@ func loadTemplates(smithyConfig SmithyConfig) (*template.Template, error) { if !strings.HasSuffix(file.Name(), ".html") { continue } - f, err := statikFS.Open("/" + file.Name()) + f, err := templatefiles.Open("templates/" + file.Name()) if err != nil { return t, err } diff --git a/include/style.css b/pkg/smithy/static/style.css similarity index 100% rename from include/style.css rename to pkg/smithy/static/style.css diff --git a/include/404.html b/pkg/smithy/templates/404.html similarity index 100% rename from include/404.html rename to pkg/smithy/templates/404.html diff --git a/include/500.html b/pkg/smithy/templates/500.html similarity index 100% rename from include/500.html rename to pkg/smithy/templates/500.html diff --git a/include/blob.html b/pkg/smithy/templates/blob.html similarity index 100% rename from include/blob.html rename to pkg/smithy/templates/blob.html diff --git a/include/commit.html b/pkg/smithy/templates/commit.html similarity index 100% rename from include/commit.html rename to pkg/smithy/templates/commit.html diff --git a/include/footer.html b/pkg/smithy/templates/footer.html similarity index 100% rename from include/footer.html rename to pkg/smithy/templates/footer.html diff --git a/include/header.html b/pkg/smithy/templates/header.html similarity index 100% rename from include/header.html rename to pkg/smithy/templates/header.html diff --git a/include/index.html b/pkg/smithy/templates/index.html similarity index 100% rename from include/index.html rename to pkg/smithy/templates/index.html diff --git a/include/log.html b/pkg/smithy/templates/log.html similarity index 100% rename from include/log.html rename to pkg/smithy/templates/log.html diff --git a/include/refs.html b/pkg/smithy/templates/refs.html similarity index 100% rename from include/refs.html rename to pkg/smithy/templates/refs.html diff --git a/include/repo-index.html b/pkg/smithy/templates/repo-index.html similarity index 100% rename from include/repo-index.html rename to pkg/smithy/templates/repo-index.html diff --git a/include/tree.html b/pkg/smithy/templates/tree.html similarity index 100% rename from include/tree.html rename to pkg/smithy/templates/tree.html