goembed

package module
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2022 License: BSD-3-Clause Imports: 12 Imported by: 2

README

embed

go1.16 embed util

package main

import (
	"go/ast"
	"go/build"
	"go/parser"
	"go/token"
	"log"
	"path/filepath"

	"github.com/visualfc/goembed"
)

func main() {
	pkg, err := build.Import("github.com/visualfc/goembed", "", 0)
	if err != nil {
		log.Fatal(err)
	}
	fset := token.NewFileSet()
	var files []*ast.File
	for _, file := range pkg.TestGoFiles {
		f, err := parser.ParseFile(fset, filepath.Join(pkg.Dir, file), nil, 0)
		if err != nil {
			log.Fatal(err)
		}
		files = append(files, f)
	}
	ems := goembed.CheckEmbed(pkg.TestEmbedPatternPos, fset, files)
	r := goembed.NewResolve()
	for _, em := range ems {
		files, err := r.Load(pkg.Dir, em)
		if err != nil {
			log.Fatal("error load", em, err)
		}
		for _, f := range files {
			log.Println(f.Name, f.Data, f.Hash)
		}
	}
}

Documentation

Index

Constants

View Source
const (
	EmbedUnknown int = iota
	EmbedBytes
	EmbedString
	EmbedFiles
)

Variables

This section is empty.

Functions

func BytesToHex

func BytesToHex(data []byte) string

func BytesToList

func BytesToList(data []byte) string

func WriteToHex

func WriteToHex(data []byte, w io.Writer) (n int, err error)

Types

type Embed

type Embed struct {
	Name     string
	Kind     int
	Patterns []string
	Pos      token.Position
	Spec     *ast.ValueSpec
}

func CheckEmbed

func CheckEmbed(embedPatternPos map[string][]token.Position, fset *token.FileSet, files []*ast.File) (embeds []*Embed)

type File

type File struct {
	Name string
	Data []byte
	Hash [16]byte // truncated SHA256 hash
	Err  error
}

func BuildFS

func BuildFS(files []*File) []*File

type Resolve

type Resolve interface {
	Load(dir string, em *Embed) ([]*File, error)
	Files() []*File
}

func NewResolve

func NewResolve() Resolve

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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