gres

package
v2.3.2 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2023 License: MIT Imports: 22 Imported by: 104

Documentation

Overview

Package gres provides resource management and packing/unpacking feature between files and bytes.

Index

Constants

View Source
const (
	// DefaultName default group name for instance usage.
	DefaultName = "default"
)
View Source
const (
	// Separator for directories.
	Separator = "/"
)

Variables

This section is empty.

Functions

func Add

func Add(content string, prefix ...string) error

Add unpacks and adds the `content` into the default resource object. The unnecessary parameter `prefix` indicates the prefix for each file storing into current resource object.

func Contains

func Contains(path string) bool

Contains checks whether the `path` exists in the default resource object.

func Dump

func Dump()

Dump prints the files of the default resource object.

func Export

func Export(src, dst string, option ...ExportOption) error

Export exports and saves specified path `src` and all its sub files to specified system path `dst` recursively.

func GetContent

func GetContent(path string) []byte

GetContent directly returns the content of `path` in default resource object.

func IsEmpty

func IsEmpty() bool

IsEmpty checks and returns whether the resource manager is empty.

func Load

func Load(path string, prefix ...string) error

Load loads, unpacks and adds the data from `path` into the default resource object. The unnecessary parameter `prefix` indicates the prefix for each file storing into current resource object.

func Pack deprecated

func Pack(srcPaths string, keyPrefix ...string) ([]byte, error)

Pack packs the path specified by `srcPaths` into bytes. The unnecessary parameter `keyPrefix` indicates the prefix for each file packed into the result bytes.

Note that parameter `srcPaths` supports multiple paths join with ','.

Deprecated: use PackWithOption instead.

func PackToFile deprecated

func PackToFile(srcPaths, dstPath string, keyPrefix ...string) error

PackToFile packs the path specified by `srcPaths` to target file `dstPath`. The unnecessary parameter `keyPrefix` indicates the prefix for each file packed into the result bytes.

Note that parameter `srcPaths` supports multiple paths join with ','.

Deprecated: use PackToFileWithOption instead.

func PackToFileWithOption added in v2.2.1

func PackToFileWithOption(srcPaths, dstPath string, option Option) error

PackToFileWithOption packs the path specified by `srcPaths` to target file `dstPath`.

Note that parameter `srcPaths` supports multiple paths join with ','.

func PackToGoFile deprecated

func PackToGoFile(srcPath, goFilePath, pkgName string, keyPrefix ...string) error

PackToGoFile packs the path specified by `srcPaths` to target go file `goFilePath` with given package name `pkgName`.

The unnecessary parameter `keyPrefix` indicates the prefix for each file packed into the result bytes.

Note that parameter `srcPaths` supports multiple paths join with ','.

Deprecated: use PackToGoFileWithOption instead.

func PackToGoFileWithOption added in v2.2.1

func PackToGoFileWithOption(srcPath, goFilePath, pkgName string, option Option) error

PackToGoFileWithOption packs the path specified by `srcPaths` to target go file `goFilePath` with given package name `pkgName`.

Note that parameter `srcPaths` supports multiple paths join with ','.

func PackWithOption added in v2.2.1

func PackWithOption(srcPaths string, option Option) ([]byte, error)

PackWithOption packs the path specified by `srcPaths` into bytes.

Note that parameter `srcPaths` supports multiple paths join with ','.

Types

type ExportOption

type ExportOption struct {
	RemovePrefix string // Remove the prefix of file name from resource.
}

ExportOption is the option for function Export.

type File

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

func Get

func Get(path string) *File

Get returns the file with given path.

func GetWithIndex

func GetWithIndex(path string, indexFiles []string) *File

GetWithIndex searches file with `path`, if the file is directory it then does index files searching under this directory.

GetWithIndex is usually used for http static file service.

func ScanDir

func ScanDir(path string, pattern string, recursive ...bool) []*File

ScanDir returns the files under the given path, the parameter `path` should be a folder type.

The pattern parameter `pattern` supports multiple file name patterns, using the ',' symbol to separate multiple patterns.

It scans directory recursively if given parameter `recursive` is true.

func ScanDirFile

func ScanDirFile(path string, pattern string, recursive ...bool) []*File

ScanDirFile returns all sub-files with absolute paths of given `path`, It scans directory recursively if given parameter `recursive` is true.

Note that it returns only files, exclusive of directories.

func Unpack

func Unpack(path string) ([]*File, error)

Unpack unpacks the content specified by `path` to []*File.

func UnpackContent

func UnpackContent(content string) ([]*File, error)

UnpackContent unpacks the content to []*File.

func (*File) Close

func (f *File) Close() error

Close implements interface of http.File.

func (*File) Content

func (f *File) Content() []byte

Content returns the content of the file.

func (*File) Export added in v2.1.2

func (f *File) Export(dst string, option ...ExportOption) error

Export exports and saves all its sub files to specified system path `dst` recursively.

func (*File) FileInfo

func (f *File) FileInfo() os.FileInfo

FileInfo returns an os.FileInfo for the FileHeader.

func (File) MarshalJSON

func (f File) MarshalJSON() ([]byte, error)

MarshalJSON implements the interface MarshalJSON for json.Marshal.

func (*File) Name

func (f *File) Name() string

Name returns the name of the file.

func (*File) Open

func (f *File) Open() (io.ReadCloser, error)

Open returns a ReadCloser that provides access to the File's contents. Multiple files may be read concurrently.

func (*File) Read

func (f *File) Read(b []byte) (n int, err error)

Read implements the io.Reader interface.

func (*File) Readdir

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

Readdir implements Readdir interface of http.File.

func (*File) Seek

func (f *File) Seek(offset int64, whence int) (n int64, err error)

Seek implements the io.Seeker interface.

func (*File) Stat

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

Stat implements Stat interface of http.File.

type Option added in v2.2.1

type Option struct {
	Prefix   string // The file path prefix for each file item in resource manager.
	KeepPath bool   // Keep the passed path when packing, usually for relative path.
}

Option contains the extra options for Pack functions.

type Resource

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

func Instance

func Instance(name ...string) *Resource

Instance returns an instance of Resource. The parameter `name` is the name for the instance.

func New

func New() *Resource

New creates and returns a new resource object.

func (*Resource) Add

func (r *Resource) Add(content string, prefix ...string) error

Add unpacks and adds the `content` into current resource object. The unnecessary parameter `prefix` indicates the prefix for each file storing into current resource object.

func (*Resource) Contains

func (r *Resource) Contains(path string) bool

Contains checks whether the `path` exists in current resource object.

func (*Resource) Dump

func (r *Resource) Dump()

Dump prints the files of current resource object.

func (*Resource) Export

func (r *Resource) Export(src, dst string, option ...ExportOption) error

Export exports and saves specified path `srcPath` and all its sub files to specified system path `dstPath` recursively.

func (*Resource) Get

func (r *Resource) Get(path string) *File

Get returns the file with given path.

func (*Resource) GetContent

func (r *Resource) GetContent(path string) []byte

GetContent directly returns the content of `path`.

func (*Resource) GetWithIndex

func (r *Resource) GetWithIndex(path string, indexFiles []string) *File

GetWithIndex searches file with `path`, if the file is directory it then does index files searching under this directory.

GetWithIndex is usually used for http static file service.

func (*Resource) IsEmpty

func (r *Resource) IsEmpty() bool

IsEmpty checks and returns whether the resource manager is empty.

func (*Resource) Load

func (r *Resource) Load(path string, prefix ...string) error

Load loads, unpacks and adds the data from `path` into current resource object. The unnecessary parameter `prefix` indicates the prefix for each file storing into current resource object.

func (*Resource) ScanDir

func (r *Resource) ScanDir(path string, pattern string, recursive ...bool) []*File

ScanDir returns the files under the given path, the parameter `path` should be a folder type.

The pattern parameter `pattern` supports multiple file name patterns, using the ',' symbol to separate multiple patterns.

It scans directory recursively if given parameter `recursive` is true.

Note that the returned files does not contain given parameter `path`.

func (*Resource) ScanDirFile

func (r *Resource) ScanDirFile(path string, pattern string, recursive ...bool) []*File

ScanDirFile returns all sub-files with absolute paths of given `path`, It scans directory recursively if given parameter `recursive` is true.

Note that it returns only files, exclusive of directories.

Jump to

Keyboard shortcuts

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