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",
visibility = ["//visibility:public"],
deps = ["//util/pkg/vfs:go_default_library"],
)
genrule(
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",
deps = [
"//util/pkg/vfs:go_default_library",
"//vendor/embed:go_default_library",
"//vendor/io/fs:go_default_library",
],
)

View File

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