embet

package module
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 19, 2021 License: MIT Imports: 4 Imported by: 0

README

embet

Golang's "embed" package has only naive features.
embet is a tool to easily implement use cases such as listing only files contained in embed.FS or exporting files in embed.FS.

Usage

To use embet in your Go code:

import "github.com/imishinist/embet"

For further details see GoDev documentation.

Example

$ tree assets/
assets/
├── index.html
└── main.css

0 directories, 2 files

This is an example of writing an "embed" files to a different directory.

package main

import (
	"embed"
	"log"
	"os"

	"github.com/imishinist/embet"
)

//go:embed assets/*
var assets embed.FS

func main() {
	dest := "dest"
	if err := os.Mkdir(dest, 0755); err != nil {
		log.Fatal(err)
	}
	if err := embet.WriteEmbedFiles(assets, "assets", dest); err != nil {
		log.Fatal(err)
	}
}
$ ls
assets/  go.mod   go.sum   main.go
$ go run main.go
$ ls
assets/  dest/    go.mod   go.sum   main.go
$ tree dest
dest
└── assets
    ├── index.html
    └── main.css
        
1 directory, 2 files        

This is an example of displaying a list of "embed" files.

package main

import (
	"embed"
	"fmt"
	"log"

	"github.com/imishinist/embet"
)

//go:embed assets/*
var asset embed.FS

func main() {
	// print embedded files
	list, err := embet.List(asset, "assets")
	if err != nil {
		log.Fatal(err)
	}
	for _, f := range list {
		fmt.Println(f)
	}
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// DirPerm is defined to write embed files.
	// Because embed file system is read-only.
	DirPerm  = os.FileMode(0755)
	FilePerm = os.FileMode(0644)
)

Functions

func List

func List(dir embed.FS, prefix string) ([]string, error)

List returns file list of embedded files.

Example
list, err := embet.List(asset, "assets")
if err != nil {
	log.Fatal(err)
}
for _, f := range list {
	fmt.Println(f)
}
Output:

func WriteEmbedFiles

func WriteEmbedFiles(dir embed.FS, prefix, dest string) error

WriteEmbedFiles write embed files to the real file system.

Example
package main

import (
	"embed"
	"log"
	"os"

	"github.com/imishinist/embet"
)

//go:embed assets/*
var assets embed.FS

func main() {
	dest := "dest"
	if err := os.Mkdir(dest, 0755); err != nil {
		log.Fatal(err)
	}
	if err := embet.WriteEmbedFiles(assets, "assets", dest); err != nil {
		log.Fatal(err)
	}
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL