Replace go-bindata with go:embed

This commit is contained in:
Peter Rifel 2021-03-20 15:39:46 -05:00
parent 1b7dd3b5b8
commit ff2c520d22
No known key found for this signature in database
GPG Key ID: BC6469E5B16DB2B6
2 changed files with 24 additions and 44 deletions

View File

@ -8,27 +8,9 @@ go_library(
], ],
importpath = "k8s.io/kops/upup/models", importpath = "k8s.io/kops/upup/models",
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
deps = ["//util/pkg/vfs:go_default_library"], deps = [
) "//util/pkg/vfs:go_default_library",
"//vendor/embed:go_default_library",
genrule( "//vendor/io/fs:go_default_library",
name = "bindata",
srcs = glob(
[
"cloudup/**",
"nodeup/**",
],
),
outs = ["bindata.go"],
cmd = """
$(location //vendor/github.com/go-bindata/go-bindata/v3/go-bindata:go-bindata) \
-o "$(OUTS)" -pkg models \
-nometadata \
-nocompress \
-prefix $$(pwd) \
-prefix upup/models $(SRCS)
""",
tools = [
"//vendor/github.com/go-bindata/go-bindata/v3/go-bindata",
], ],
) )

View File

@ -17,17 +17,21 @@ limitations under the License.
package models package models
import ( import (
"embed"
"errors" "errors"
"io" "io"
"io/fs"
"os" "os"
"path" "path"
"strings"
"k8s.io/kops/util/pkg/vfs" "k8s.io/kops/util/pkg/vfs"
) )
var ReadOnlyError = errors.New("AssetPath is read-only") var ReadOnlyError = errors.New("AssetPath is read-only")
//go:embed cloudup
var content embed.FS
type AssetPath struct { type AssetPath struct {
location string location string
} }
@ -68,28 +72,24 @@ func (p *AssetPath) WriteTo(out io.Writer) (int64, error) {
// ReadFile implements Path::ReadFile // ReadFile implements Path::ReadFile
func (p *AssetPath) ReadFile() ([]byte, error) { func (p *AssetPath) ReadFile() ([]byte, error) {
data, err := Asset(p.location) data, err := content.ReadFile(p.location)
if err != nil { if _, ok := err.(*fs.PathError); ok {
// Yuk return nil, os.ErrNotExist
if strings.Contains(err.Error(), "not found") {
return nil, os.ErrNotExist
}
} }
return data, err return data, err
} }
func (p *AssetPath) ReadDir() ([]vfs.Path, error) { func (p *AssetPath) ReadDir() ([]vfs.Path, error) {
files, err := AssetDir(p.location) files, err := content.ReadDir(p.location)
if err != nil { if err != nil {
// Yuk if _, ok := err.(*fs.PathError); ok {
if strings.Contains(err.Error(), "not found") {
return nil, os.ErrNotExist return nil, os.ErrNotExist
} }
return nil, err return nil, err
} }
var paths []vfs.Path var paths []vfs.Path
for _, f := range files { for _, f := range files {
paths = append(paths, NewAssetPath(path.Join(p.location, f))) paths = append(paths, NewAssetPath(path.Join(p.location, f.Name())))
} }
return paths, nil return paths, nil
} }
@ -104,25 +104,23 @@ func (p *AssetPath) ReadTree() ([]vfs.Path, error) {
} }
func readTree(base string, dest *[]vfs.Path) error { func readTree(base string, dest *[]vfs.Path) error {
files, err := AssetDir(base) files, err := content.ReadDir(base)
if err != nil { if err != nil {
// Yuk if _, ok := err.(*fs.PathError); ok {
if strings.Contains(err.Error(), "not found") {
return os.ErrNotExist return os.ErrNotExist
} }
return err return err
} }
for _, f := range files { for _, f := range files {
p := path.Join(base, f) p := path.Join(base, f.Name())
*dest = append(*dest, NewAssetPath(p)) if f.IsDir() {
childFiles, err := NewAssetPath(p).ReadTree()
// We always assume a directory, but ignore if not found if err != nil {
// This is because go-bindata doesn't support FileInfo on directories :-(
{
err = readTree(p, dest)
if err != nil && !os.IsNotExist(err) {
return err return err
} }
*dest = append(*dest, childFiles...)
} else {
*dest = append(*dest, NewAssetPath(p))
} }
} }
return nil return nil