1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-05-17 15:06:47 +08:00

Remove gobindata

Since go1.16, there are built in tools that allow for embeding
filesystem inside the binary. We now make use of the `embed` package to
have all files put into the binary, removing the need to generate the
files and removes dependencies

Co-authored-by: Jorropo <jorropo.pgm@gmail.com>
This commit is contained in:
Franky W
2022-03-31 12:25:49 +02:00
committed by Jorropo
parent 9bd346e250
commit 9210c08fa6
9 changed files with 45 additions and 518 deletions

View File

@ -5,15 +5,11 @@ This directory contains the go-ipfs assets:
* Getting started documentation (`init-doc`).
* Directory listing HTML template (`dir-index-html`).
These assets are compiled into `bindata.go` with `go generate`.
## Re-generating
Do not edit the .go files directly.
Instead, edit the source files and use `go generate` from within the
Edit the source files and use `go generate` from within the
assets directory:
```
go generate .
```
```

View File

@ -1,23 +1,30 @@
//go:generate npm run build --prefix ./dir-index-html/
//go:generate go run github.com/go-bindata/go-bindata/v3/go-bindata -mode=0644 -modtime=1403768328 -pkg=assets init-doc dir-index-html/dir-index.html dir-index-html/knownIcons.txt
//go:generate gofmt -s -w bindata.go
//go:generate sh -c "sed -i \"s/.*BindataVersionHash.*/BindataVersionHash=\\\"$(git hash-object bindata.go)\\\"/\" bindata_version_hash.go"
//go:generate gofmt -s -w bindata_version_hash.go
package assets
import (
"embed"
"fmt"
"io"
"io/fs"
"path/filepath"
"strconv"
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/coreapi"
"github.com/cespare/xxhash"
cid "github.com/ipfs/go-cid"
files "github.com/ipfs/go-ipfs-files"
options "github.com/ipfs/interface-go-ipfs-core/options"
"github.com/ipfs/interface-go-ipfs-core/path"
)
//go:embed init-doc dir-index-html/dir-index.html dir-index-html/knownIcons.txt
var dir embed.FS
// AssetHash a non-cryptographic hash of all embedded assets
var AssetHash string
// initDocPaths lists the paths for the docs we want to seed during --init
var initDocPaths = []string{
filepath.Join("init-doc", "about"),
@ -29,6 +36,35 @@ var initDocPaths = []string{
filepath.Join("init-doc", "ping"),
}
func init() {
sum := xxhash.New()
err := fs.WalkDir(Asset, ".", func(path string, d fs.DirEntry, err error) error {
if d.IsDir() {
return nil
}
file, err := dir.Open(path)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(sum, file)
return err
})
if err != nil {
panic("error creating asset sum: " + err.Error())
}
AssetHash = strconv.FormatUint(sum.Sum64(), 32)
}
// Asset loads and returns the asset for the given name.
// It returns an error if the asset could not be found or
// could not be loaded.
func Asset(f string) ([]byte, error) {
return dir.ReadFile(f)
}
// SeedInitDocs adds the list of embedded init documentation to the passed node, pins it and returns the root key
func SeedInitDocs(nd *core.IpfsNode) (cid.Cid, error) {
return addAssetList(nd, initDocPaths)

View File

@ -1,53 +0,0 @@
package assets
import (
"bytes"
"io/ioutil"
"sync"
"testing"
)
// TestEmbeddedDocs makes sure we don't forget to regenerate after documentation change
func TestEmbeddedDocs(t *testing.T) {
testNFiles(initDocPaths, 5, t, "documents")
}
func testNFiles(fs []string, wantCnt int, t *testing.T, ftype string) {
if len(fs) < wantCnt {
t.Fatalf("expected %d %s. got %d", wantCnt, ftype, len(fs))
}
var wg sync.WaitGroup
for _, f := range fs {
wg.Add(1)
// compare asset
go func(f string) {
defer wg.Done()
testOneFile(f, t)
}(f)
}
wg.Wait()
}
func testOneFile(f string, t *testing.T) {
// load data from filesystem (git)
vcsData, err := ioutil.ReadFile(f)
if err != nil {
t.Errorf("asset %s: could not read vcs file: %s", f, err)
return
}
// load data from emdedded source
embdData, err := Asset(f)
if err != nil {
t.Errorf("asset %s: could not read vcs file: %s", f, err)
return
}
if !bytes.Equal(vcsData, embdData) {
t.Errorf("asset %s: vcs and embedded data isn't equal", f)
return
}
t.Logf("checked %s", f)
}

File diff suppressed because one or more lines are too long

View File

@ -1,9 +0,0 @@
//+build never,!never
package assets
import (
// Make sure go mod tracks these deps but avoid including them in the
// actual build.
_ "github.com/go-bindata/go-bindata/v3"
)

View File

@ -1,6 +0,0 @@
// File generated together with 'bindata.go' when running `go generate .` DO NOT EDIT. (@generated)
package assets
const (
BindataVersionHash = "512eb789cd905714e03f29d4e04de7549e8c9c3e"
)

View File

@ -94,7 +94,7 @@ func (i *gatewayHandler) serveDirectory(ctx context.Context, w http.ResponseWrit
w.Header().Set("Content-Type", "text/html")
// Generated dir index requires custom Etag (it may change between go-ipfs versions)
if assets.BindataVersionHash != "" {
if assets.AssetHash != "" {
dirEtag := getDirListingEtag(resolvedPath.Cid())
w.Header().Set("Etag", dirEtag)
if r.Header.Get("If-None-Match") == dirEtag {
@ -208,5 +208,5 @@ func (i *gatewayHandler) serveDirectory(ctx context.Context, w http.ResponseWrit
}
func getDirListingEtag(dirCid cid.Cid) string {
return `"DirIndex-` + assets.BindataVersionHash + `_CID-` + dirCid.String() + `"`
return `"DirIndex-` + assets.AssetHash + `_CID-` + dirCid.String() + `"`
}

2
go.mod
View File

@ -5,6 +5,7 @@ require (
contrib.go.opencensus.io/exporter/prometheus v0.4.0
github.com/blang/semver/v4 v4.0.0
github.com/ceramicnetwork/go-dag-jose v0.1.0
github.com/cespare/xxhash v1.1.0
github.com/cheggaaa/pb v1.0.29
github.com/coreos/go-systemd/v22 v22.3.2
github.com/dustin/go-humanize v1.0.0
@ -12,7 +13,6 @@ require (
github.com/facebookgo/atomicfile v0.0.0-20151019160806-2de1f203e7d5
github.com/fsnotify/fsnotify v1.5.1
github.com/gabriel-vasile/mimetype v1.4.0
github.com/go-bindata/go-bindata/v3 v3.1.3
github.com/hashicorp/go-multierror v1.1.1
github.com/ipfs/go-bitswap v0.6.0
github.com/ipfs/go-block-format v0.0.3

4
go.sum
View File

@ -241,8 +241,6 @@ github.com/gabriel-vasile/mimetype v1.4.0 h1:Cn9dkdYsMIu56tGho+fqzh7XmvY2YyGU0Fn
github.com/gabriel-vasile/mimetype v1.4.0/go.mod h1:fA8fi6KUiG7MgQQ+mEWotXoEOvmxRtOJlERCzSmRvr8=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gliderlabs/ssh v0.1.1/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
github.com/go-bindata/go-bindata/v3 v3.1.3 h1:F0nVttLC3ws0ojc7p60veTurcOm//D4QBODNM7EGrCI=
github.com/go-bindata/go-bindata/v3 v3.1.3/go.mod h1:1/zrpXsLD8YDIbhZRqXzm1Ghc7NhEvIN9+Z6R5/xH4I=
github.com/go-check/check v0.0.0-20180628173108-788fd7840127/go.mod h1:9ES+weclKsC9YodN5RgxqK/VD9HM9JsCSh7rNhMZE98=
github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
@ -667,7 +665,6 @@ github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8
github.com/kami-zh/go-capturer v0.0.0-20171211120116-e492ea43421d/go.mod h1:P2viExyCEfeWGU259JnaQ34Inuec4R38JCyBx2edgD0=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/errcheck v1.5.0 h1:e8esj/e4R+SAOwFwN+n3zr0nYeCyeweozKfO23MvHzY=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4=
@ -1625,7 +1622,6 @@ golang.org/x/lint v0.0.0-20190909230951-414d861bb4ac/go.mod h1:6SW0HCj/g11FgYtHl
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f/go.mod h1:5qLYkcX4OjUUV8bRuDixDT3tpyyb+LUpUlRWLxfhWrs=
golang.org/x/lint v0.0.0-20200130185559-910be7a94367/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b h1:Wh+f8QHJXR411sJR8/vRBTZ7YapZaRvUcLFFJhusH0k=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=