Skip to content

Commit

Permalink
Replace statik with the new embed feature
Browse files Browse the repository at this point in the history
https://golang.org/pkg/embed/

This makes smithy dependent on Golang 1.16 or higher.
  • Loading branch information
honza committed Jun 24, 2021
1 parent 2cf7173 commit 6f054af
Show file tree
Hide file tree
Showing 16 changed files with 17 additions and 37 deletions.
10 changes: 2 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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/[email protected]/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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/honza/smithy

go 1.15
go 1.16

require (
github.com/alecthomas/chroma v0.8.2
Expand Down
40 changes: 13 additions & 27 deletions pkg/smithy/smithy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 6f054af

Please sign in to comment.