noodle

package
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2020 License: MIT Imports: 17 Imported by: 0

README

Noodle embed static assets in go binary

Usage

See example Makefile and main.go

Generate from a single folder, using gommon generat noodle

gommon generate noodle --root assets --output gen/noodle.go --pkg gen --name YangChunMian

Generate multiple folders into one file, use gommon.yml

noodles:
- src: "assets"
  dst: "gen/noodle.go"
  name: "Assets"
  package: "gen"
- src: "third_party"
  dst: "gen/noodle.go"
  name: "ThirdParty"
  package: "gen"
import (
	"fmt"
	"log"
	"net/http"
	"os"
	"strings"

	"github.com/dyweb/gommon/noodle/_examples/embed/gen"
)

func main() {
	mode := "dev"
	if len(os.Args) > 1 {
		if strings.HasPrefix(os.Args[1], "p") {
			mode = "prod"
		}
	}
	var root http.FileSystem
	if mode == "dev" {
		localDir := "assets"
		root = http.Dir(localDir)
	} else {
		bowel1, err := gen.GetNoodleYangChunMian()
		if err != nil {
			log.Fatal(err)
		}
		root = &bowel1
	}
	addr := ":8080"
	fmt.Printf("listen on %s in %s mode\n", addr, mode)
	fmt.Printf("use http://localhost:8080/index.html")
	log.Fatal(http.ListenAndServe(addr, http.FileServer(root)))
}

References and Alternatives

Documentation

Overview

Package noodle helps embedding static assets into go binary, it supports using ignore file

Index

Constants

View Source
const (
	DefaultIgnoreFileName = ".noodleignore"
	DefaultName           = "Bowel"
)

Variables

This section is empty.

Functions

func CleanLine

func CleanLine(line string) string

CleanLine remove trailing space and \n, and anything following the first #

func GenerateEmbedBytes added in v0.0.8

func GenerateEmbedBytes(cfgs []EmbedConfig) ([]byte, error)

GenerateEmbedBytes return a single formatted go file as bytes from multiple source directories. Use GenerateEmbedFile if you just have one source directory and want to write to file directly. it's header + package + []GenerateEmbedPartial

func GenerateEmbedFile added in v0.0.8

func GenerateEmbedFile(cfg EmbedConfig) error

GenerateEmbedFile generates contents from cfg.Src and save it to a single go file in cfg.Dst. It's a wrapper around GenerateEmbedBytes

func GenerateEmbedPartial added in v0.0.8

func GenerateEmbedPartial(cfg EmbedConfig) ([]byte, error)

GenerateEmbedPartial return code WITHOUT header and import, use GenerateEmbedBytes if you want a full go file

func LineToPattern

func LineToPattern(line string) fsutil.IgnorePattern

func ReadIgnore

func ReadIgnore(reader io.Reader) (*fsutil.Ignores, error)

ReadIgnore reads ignore file and change to patterns reading is based on https://github.com/codeskyblue/dockerignore/blob/HEAD/ignore.go#L40

func ReadIgnoreFile

func ReadIgnoreFile(path string) (*fsutil.Ignores, error)

Types

type Bowel added in v0.0.8

type Bowel interface {
	http.FileSystem
}

Bowel is the container for different types of noodles

type EmbedBowel added in v0.0.8

type EmbedBowel struct {
	Dirs map[string]EmbedDir
	Data []byte
	// contains filtered or unexported fields
}

func (*EmbedBowel) ExtractFiles added in v0.0.8

func (b *EmbedBowel) ExtractFiles() error

func (*EmbedBowel) Open added in v0.0.8

func (b *EmbedBowel) Open(name string) (http.File, error)

type EmbedConfig added in v0.0.8

type EmbedConfig struct {
	Src     string `json:"src" yaml:"src"`
	Dst     string `json:"dst" yaml:"dst"`
	Name    string `json:"name" yaml:"name"`
	Package string `json:"package" yaml:"package"`
}

type EmbedDir

type EmbedDir struct {
	FileInfo
	Entries []FileInfo
}

func (*EmbedDir) Close

func (d *EmbedDir) Close() error

func (*EmbedDir) Read

func (d *EmbedDir) Read(p []byte) (int, error)

func (*EmbedDir) Readdir

func (d *EmbedDir) Readdir(count int) ([]os.FileInfo, error)

func (*EmbedDir) Seek

func (d *EmbedDir) Seek(offset int64, whence int) (int64, error)

func (*EmbedDir) Stat

func (d *EmbedDir) Stat() (os.FileInfo, error)

type EmbedFile

type EmbedFile struct {
	FileInfo
	Data []byte
	// contains filtered or unexported fields
}

func (*EmbedFile) Close

func (f *EmbedFile) Close() error

func (*EmbedFile) Read

func (f *EmbedFile) Read(p []byte) (int, error)

func (*EmbedFile) Readdir

func (f *EmbedFile) Readdir(count int) ([]os.FileInfo, error)

func (*EmbedFile) Seek

func (f *EmbedFile) Seek(offset int64, whence int) (int64, error)

func (*EmbedFile) Stat

func (f *EmbedFile) Stat() (os.FileInfo, error)

type FileInfo

type FileInfo struct {
	FileName    string
	FileSize    int64
	FileMode    os.FileMode
	FileModTime time.Time
	FileIsDir   bool
}

FileInfo is a concrete struct that implements os.FileInfo interface Its fields have the awkward File* prefix to avoid conflict with os.FileInfo interface

func NewFileInfo

func NewFileInfo(info os.FileInfo) *FileInfo

func (*FileInfo) IsDir

func (i *FileInfo) IsDir() bool

func (*FileInfo) ModTime

func (i *FileInfo) ModTime() time.Time

func (*FileInfo) Mode

func (i *FileInfo) Mode() os.FileMode

func (*FileInfo) Name

func (i *FileInfo) Name() string

func (*FileInfo) Size

func (i *FileInfo) Size() int64

func (*FileInfo) Sys

func (i *FileInfo) Sys() interface{}

type LocalFs

type LocalFs struct {
	// contains filtered or unexported fields
}

func NewLocal

func NewLocal(root string) *LocalFs

NewLocal returns a http.FileSystem using local directory, both list directory and index.html are disabled it is a wrapper around http.Dir

func NewLocalUnsafe

func NewLocalUnsafe(root string) *LocalFs

NewLocalUnsafe allows list directory and use http.Dir directly

func (*LocalFs) Open

func (fs *LocalFs) Open(name string) (http.File, error)

Open implements http.FileSystem interface, if list directory is not allowed, it will return os.ErrNotExist when it detects the file is a directory

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

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