gophertype/pvt-www/assets_embed.go

58 lines
1.2 KiB
Go
Raw Permalink Normal View History

// Package assets gets us off go-bindata by using Go 1.16 embed support.
package assets
import (
"embed"
"io/fs"
"strings"
)
//go:embed *
var Embedded embed.FS
// AssetDir returns the list of embedded files at the directory name.
func AssetDir(name string) ([]string, error) {
// normalize path separators, for Windows
name = strings.ReplaceAll(name, "\\", "/")
var result []string
name = strings.TrimPrefix(name, "assets/")
files, err := Embedded.ReadDir(name)
if err != nil {
return result, err
}
for _, file := range files {
if file.IsDir() {
continue
}
result = append(result, file.Name())
}
return result, nil
}
// Asset returns the byte data of an embedded asset.
func Asset(name string) ([]byte, error) {
// normalize path separators, for Windows
name = strings.ReplaceAll(name, "\\", "/")
return Embedded.ReadFile(strings.TrimPrefix(name, "assets/"))
}
// AssetNames dumps the names of all embedded assets,
// with their legacy "assets/" prefix from go-bindata.
func AssetNames() []string {
var result []string
fs.WalkDir(Embedded, ".", func(path string, d fs.DirEntry, err error) error {
if d != nil && !d.IsDir() {
result = append(result, "assets/"+path)
}
return nil
})
return result
}