Skip to content

Commit 98a1cca

Browse files
wadermildred
authored andcommitted
Replace go.rice module with std embed
1 parent baea0cb commit 98a1cca

File tree

14 files changed

+64
-159
lines changed

14 files changed

+64
-159
lines changed

fileserver/fileserver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ func localRedirect(w http.ResponseWriter, r *http.Request, newPath string) {
229229
// To use the operating system's file system implementation,
230230
// use http.Dir:
231231
//
232-
// http.Handle("/", &fileserver.FileServer{Root: http.Dir("/tmp")})
232+
// http.Handle("/", &fileserver.FileServer{Root: http.Dir("/tmp")})
233233
type FileServer struct {
234234
Version string
235235
Root http.FileSystem

fileserver/fileserver_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ import (
2323
"testing"
2424
"time"
2525

26-
rice "github.com/GeertJohan/go.rice"
26+
"github.com/cortesi/devd/fstmpl"
2727
"github.com/cortesi/devd/inject"
28-
"github.com/cortesi/devd/ricetemp"
2928
"github.com/cortesi/devd/routespec"
29+
"github.com/cortesi/devd/templates"
3030
"github.com/cortesi/termlog"
3131
)
3232

@@ -40,7 +40,7 @@ func ServeFile(w http.ResponseWriter, r *http.Request, name string) {
4040
"version",
4141
http.Dir(dir),
4242
inject.CopyInject{},
43-
ricetemp.MustMakeTemplates(rice.MustFindBox("../templates")),
43+
fstmpl.MustMakeTemplates(templates.FS),
4444
[]routespec.RouteSpec{},
4545
"",
4646
}
@@ -168,7 +168,7 @@ func TestFSRedirect(t *testing.T) {
168168
"version",
169169
http.Dir("."),
170170
inject.CopyInject{},
171-
ricetemp.MustMakeTemplates(rice.MustFindBox("../templates")),
171+
fstmpl.MustMakeTemplates(templates.FS),
172172
[]routespec.RouteSpec{},
173173
"",
174174
},
@@ -208,7 +208,7 @@ func _TestFileServerCleans(t *testing.T) {
208208
},
209209
},
210210
inject.CopyInject{},
211-
ricetemp.MustMakeTemplates(rice.MustFindBox("../templates")),
211+
fstmpl.MustMakeTemplates(templates.FS),
212212
[]routespec.RouteSpec{},
213213
"",
214214
}
@@ -250,7 +250,7 @@ func TestFileServerImplicitLeadingSlash(t *testing.T) {
250250
"version",
251251
http.Dir(tempDir),
252252
inject.CopyInject{},
253-
ricetemp.MustMakeTemplates(rice.MustFindBox("../templates")),
253+
fstmpl.MustMakeTemplates(templates.FS),
254254
[]routespec.RouteSpec{},
255255
"",
256256
}
@@ -414,7 +414,7 @@ func TestServeIndexHtml(t *testing.T) {
414414
"version",
415415
http.Dir("."),
416416
inject.CopyInject{},
417-
ricetemp.MustMakeTemplates(rice.MustFindBox("../templates")),
417+
fstmpl.MustMakeTemplates(templates.FS),
418418
[]routespec.RouteSpec{},
419419
"",
420420
}
@@ -443,7 +443,7 @@ func TestFileServerZeroByte(t *testing.T) {
443443
"version",
444444
http.Dir("."),
445445
inject.CopyInject{},
446-
ricetemp.MustMakeTemplates(rice.MustFindBox("../templates")),
446+
fstmpl.MustMakeTemplates(templates.FS),
447447
[]routespec.RouteSpec{},
448448
"",
449449
}
@@ -538,7 +538,7 @@ func TestNotFoundOverride(t *testing.T) {
538538
"version",
539539
fsys,
540540
inject.CopyInject{},
541-
ricetemp.MustMakeTemplates(rice.MustFindBox("../templates")),
541+
fstmpl.MustMakeTemplates(templates.FS),
542542
[]routespec.RouteSpec{
543543
{Host: "", Path: "/", Value: "foo.html"},
544544
},
@@ -610,7 +610,7 @@ func TestDirectoryIfNotModified(t *testing.T) {
610610
"version",
611611
fsys,
612612
inject.CopyInject{},
613-
ricetemp.MustMakeTemplates(rice.MustFindBox("../templates")),
613+
fstmpl.MustMakeTemplates(templates.FS),
614614
[]routespec.RouteSpec{},
615615
"",
616616
}
Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
// Package ricetemp makes templates from a ricebox.
2-
package ricetemp
1+
// Package embedtmpl makes templates from a fs.FS
2+
package fstmpl
33

44
import (
55
"html/template"
6+
"io"
7+
"io/fs"
68
"os"
79
"strings"
810

9-
"github.com/GeertJohan/go.rice"
1011
"github.com/dustin/go-humanize"
1112
)
1213

@@ -25,16 +26,16 @@ func fileType(f os.FileInfo) string {
2526
}
2627

2728
// MustMakeTemplates makes templates, and panic on error
28-
func MustMakeTemplates(rb *rice.Box) *template.Template {
29-
templates, err := MakeTemplates(rb)
29+
func MustMakeTemplates(fs fs.ReadDirFS) *template.Template {
30+
templates, err := MakeTemplates(fs)
3031
if err != nil {
3132
panic(err)
3233
}
3334
return templates
3435
}
3536

3637
// MakeTemplates takes a rice.Box and returns a html.Template
37-
func MakeTemplates(rb *rice.Box) (*template.Template, error) {
38+
func MakeTemplates(fs fs.ReadDirFS) (*template.Template, error) {
3839
tmpl := template.New("")
3940

4041
funcMap := template.FuncMap{
@@ -44,14 +45,20 @@ func MakeTemplates(rb *rice.Box) (*template.Template, error) {
4445
}
4546
tmpl.Funcs(funcMap)
4647

47-
err := rb.Walk("", func(path string, info os.FileInfo, err error) error {
48-
if !info.IsDir() {
49-
_, err := tmpl.New(path).Parse(rb.MustString(path))
50-
if err != nil {
51-
return err
52-
}
48+
ds, err := fs.ReadDir(".")
49+
if err != nil {
50+
return nil, err
51+
}
52+
53+
for _, d := range ds {
54+
f, _ := fs.Open(d.Name())
55+
fbs, _ := io.ReadAll(f)
56+
57+
_, err := tmpl.New(d.Name()).Parse(string(fbs))
58+
if err != nil {
59+
return nil, err
5360
}
54-
return nil
55-
})
61+
}
62+
5663
return tmpl, err
5764
}

go.mod

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
module github.com/cortesi/devd
22

3-
go 1.12
3+
go 1.16
44

55
require (
6-
github.com/GeertJohan/go.rice v1.0.2
76
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
87
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect
98
github.com/cortesi/moddwatch v0.0.0-20210323234936-df014e95c743
109
github.com/cortesi/termlog v0.0.0-20210222042314-a1eec763abec
11-
github.com/daaku/go.zipexe v1.0.1 // indirect
10+
github.com/davecgh/go-spew v1.1.1 // indirect
1211
github.com/dustin/go-humanize v1.0.0
1312
github.com/fatih/color v1.13.0
1413
github.com/goji/httpauth v0.0.0-20160601135302-2da839ab0f4d

go.sum

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
github.com/GeertJohan/go.incremental v1.0.0/go.mod h1:6fAjUhbVuX1KcMD3c8TEgVUqmo4seqhv0i0kdATSkM0=
2-
github.com/GeertJohan/go.rice v1.0.2 h1:PtRw+Tg3oa3HYwiDBZyvOJ8LdIyf6lAovJJtr7YOAYk=
3-
github.com/GeertJohan/go.rice v1.0.2/go.mod h1:af5vUNlDNkCjOZeSGFgIJxDje9qdjsO6hshx0gTmZt4=
4-
github.com/akavel/rsrc v0.8.0/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c=
51
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM=
62
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
73
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d h1:UQZhZ2O0vMHr2cI+DC1Mbh0TJxzA3RcLoMsFw+aXw7E=
@@ -13,9 +9,6 @@ github.com/cortesi/moddwatch v0.0.0-20210323234936-df014e95c743/go.mod h1:DBqag+
139
github.com/cortesi/termlog v0.0.0-20210222042314-a1eec763abec h1:v7D8uHsIKsyjfyhhNdY4qivqN558Ejiq+CDXiUljZ+4=
1410
github.com/cortesi/termlog v0.0.0-20210222042314-a1eec763abec/go.mod h1:10Fm2kasJmcKf1FSMQGSWb976sfR29hejNtfS9AydB4=
1511
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
16-
github.com/daaku/go.zipexe v1.0.0/go.mod h1:z8IiR6TsVLEYKwXAoE/I+8ys/sDkgTzSL0CLnGVd57E=
17-
github.com/daaku/go.zipexe v1.0.1 h1:wV4zMsDOI2SZ2m7Tdz1Ps96Zrx+TzaK15VbUaGozw0M=
18-
github.com/daaku/go.zipexe v1.0.1/go.mod h1:5xWogtqlYnfBXkSB1o9xysukNP9GTvaNkqzUZbt3Bw8=
1912
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2013
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2114
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -30,7 +23,6 @@ github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
3023
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
3124
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
3225
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
33-
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
3426
github.com/juju/ratelimit v1.0.2 h1:sRxmtRiajbvrcLQT7S+JbqU0ntsb9W2yhSdNN8tWfaI=
3527
github.com/juju/ratelimit v1.0.2/go.mod h1:qapgC/Gy+xNh9UxzV13HGGl/6UXNN+ct+vwSgWNm/qk=
3628
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
@@ -49,7 +41,6 @@ github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG
4941
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
5042
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
5143
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
52-
github.com/nkovacs/streamquote v1.0.0/go.mod h1:BN+NaZ2CmdKqUuTUXUEm9j95B2TRbpOWpxbJYzzgUsc=
5344
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5445
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5546
github.com/rjeczalik/notify v0.0.0-20181126183243-629144ba06a1 h1:FLWDC+iIP9BWgYKvWKKtOUZux35LIQNAuIzp/63RQJU=
@@ -60,8 +51,6 @@ github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H
6051
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
6152
github.com/toqueteos/webbrowser v1.2.0 h1:tVP/gpK69Fx+qMJKsLE7TD8LuGWPnEV71wBN9rrstGQ=
6253
github.com/toqueteos/webbrowser v1.2.0/go.mod h1:XWoZq4cyp9WeUeak7w7LXRUQf1F1ATJMir8RTqb4ayM=
63-
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
64-
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
6554
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
6655
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
6756
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
@@ -107,7 +96,6 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
10796
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
10897
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
10998
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
110-
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
11199
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
112100
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
113101
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
File renamed without changes.
File renamed without changes.

livereload/livereload.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,23 @@
33
package livereload
44

55
import (
6+
_ "embed"
67
"net/http"
78
"regexp"
89
"strings"
910
"sync"
1011

11-
"github.com/GeertJohan/go.rice"
1212
"github.com/cortesi/devd/inject"
1313
"github.com/cortesi/termlog"
1414
"github.com/gorilla/websocket"
1515
)
1616

17+
//go:embed client.js
18+
var clientJS []byte
19+
20+
//go:embed LICENSE
21+
var license []byte
22+
1723
// Reloader triggers a reload
1824
type Reloader interface {
1925
Reload(paths []string)
@@ -130,8 +136,7 @@ func (s *Server) Watch(ch chan []string) {
130136
// ServeScript is a handler function that serves the livereload JavaScript file
131137
func (s *Server) ServeScript(rw http.ResponseWriter, req *http.Request) {
132138
rw.Header().Set("Content-Type", "application/javascript")
133-
clientBox := rice.MustFindBox("static")
134-
_, err := rw.Write(clientBox.MustBytes("client.js"))
139+
_, err := rw.Write(clientJS)
135140
if err != nil {
136141
s.logger.Warn("Error serving livereload script: %s", err)
137142
}

0 commit comments

Comments
 (0)