bog

package module
v0.0.0-...-19e3948 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2014 License: BSD-3-Clause Imports: 8 Imported by: 0

README

Bog

GoDoc

Bog is a tool to archive directories or files into Go source code. The directories and files data then will be built into a single Go executable file, therefore simplify deploy process.

This tool comes with 2 parts:

  • An executable command bog for archiving and extracting your files.
  • A library to access those files in your code. The API is nearly identical to os package, therefore compatible with almost all io, ioutil functions.

Installing

go get github.com/keimoon/bog
go install github.com/keimoon/bog/tool/bog

Archiving

To create an archive of a directory or a file:

bog archive /path/to/directory
bog archive /path/to/file

Or shorter:

bog a /path/to/directory
bog a /path/to/file

Extracting

To extract the data of archived source file to current directory:

bog extract directory-archive.go

Or:

bog e directory-archive.go

Ignore files

Bog supports the use of special file called .bogignore to make the generator ignore certain files or folders. It is nearly identical to .gitignore.

Setting package name

By default, bog will use current directory for package name in generated files. To change that, use -p switch:

bog -p mypackage a /path/to/directory

If you set the package name, the generated file will be put in mypackage folder. The only exception is if you use main as package name. In this case, the generated file will be put in current folder.

How to use generated file

In the generated file, a single public variable will be exported. This variable is normally named MyFolderArchive if your directory's name is my-folder. You can find this variable at the very end of the generated file.

This variable is of type bog.Archive, with methods nearly identical with os package.

Open a file

To open a file, use Open method:

f, err := MyFolderArchive.Open("path/to/my-file")

Read data from an openned file

The return value from Open method is of interface bog.File, which is identical with os.File except you cannot write to that.

To read data, you can simply use ioutil.ReadAll, bufio or whatever methods:

b, err := ioutil.ReadAll(f)

List files in a folder

To list all files in a folder, use ReadDir method, which is identical with ioutil.ReadDir:

fi, err := MyFolderArchive.ReadDir("path/to/subfolder")
fi, err := MyFolderArchive.ReadDir("") // will list files from root folder.

Development mode

Bog supports development mode, in which bog will not archive files, and read data from real files directly. To enable development mode, put -d switch when run bog command:

bog -d a /path/to/directory

Beside from reading directly from real folder, there is no difference between development mode and normal mode.

Setting root folder

When using development mode, bog relies on root folder, which is set to the path to the directory you want to archive. If for some reason the folder is moved, or you run on another machine where the path is different, the root folder can be set by using SetRoot method:

MyFolderArchive.SetRoot("/path/to/new-directory")

Please notice that SetRoot only works in development mode.

Contributing

Contributions and pull requests are always welcome.

License

Bog is available under BSD License

Documentation

Overview

Package bog is a tool to archive directories or files into Go source code. The directories and files data then will be built into a single Go executable file, therefore simplify deploy process.

This tool comes with 2 parts:

  • An executable command "bog" for archiving and extracting your files.
  • A library to access those files in your code. The API is nearly identical to "os" package, therefore compatible with almost all io, ioutil functions.

Installing

Install "bog" tool with:

go get github.com/keimoon/bog
go install github.com/keimoon/bog/tool/bog

Archiving

To create an archive of a directory or a file:

bog archive /path/to/directory
bog archive /path/to/file

Or shorter:

bog a /path/to/directory
bog a /path/to/file

Extracting

To extract the data of archived source file to current directory:

bog extract directory-archive.go

Or:

bog e directory-archive.go

Ignore files

Bog supports the use of special file called .bogignore to make the generator ignore certain files or folders. It is nearly identical to .gitignore.

Setting package name

By default, bog will use current directory for package name in generated files. To change that, use -p switch:

bog -p mypackage a /path/to/directory

If you set the package name, the generated file will be put in "mypackage" folder. The only exception is if you use "main" as package name. In this case, the generated file will be put in current folder.

How to use generated file

In the generated file, a single public variable will be exported. This variable is normally named "MyFolderArchive" if your directory's name is "my-folder". You can find this variable at the very end of the generated file.

This variable is of type bog.Archive, with methods nearly identical with "os" package.

Open a file

To open a file, use Open method:

f, err := MyFolderArchive.Open("path/to/my-file")

Read data from an openned file

The return value from Open method is of interface bog.File, which is identical with os.File except you cannot write to that.

To read data, you can simply use ioutil.ReadAll, bufio or whatever methods:

b, err := ioutil.ReadAll(f)

List files in a folder

To list all files in a folder, use ReadDir method, which is identical with ioutil.ReadDir:

fi, err := MyFolderArchive.ReadDir("path/to/subfolder")
fi, err := MyFolderArchive.ReadDir("") // will list files from root folder.

Development mode

Bog supports development mode, in which bog will not archive files, and read data from the real files directly. To enable development mode, put -d switch when run bog command:

bog -d a /path/to/directory

Beside from reading directly from real folder, there is no difference between development mode and normal mode.

Setting root folder

When using development mode, bog relies on "root folder", which is set to the path to the directory you want to archive. If for some reason the folder is moved, or you run on another machine where the path is different, the root folder can be set by using SetRoot method:

MyFolderArchive.SetRoot("/path/to/new-directory")

Please notice that SetRoot only works in development mode.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Archive

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

Archive is representation os archived folders/files in Go code.

func NewArchive

func NewArchive(files map[string]File, dev bool, isFile bool, root string) *Archive

NewArchive creates new Archive. This function is called by the generator.

func (*Archive) Extract

func (a *Archive) Extract() error

Extract extracts the content of the archive to current folder.

func (*Archive) Open

func (a *Archive) Open(name string) (File, error)

Open opens the named file for reading. If successful, methods on the returned file can be used for reading. If there is an error, it will be of type *PathError

func (*Archive) ReadDir

func (a *Archive) ReadDir(dirname string) ([]os.FileInfo, error)

ReadDir reads the directory named by dirname and returns a list of directory entries.

func (*Archive) ReadFile

func (a *Archive) ReadFile(name string) ([]byte, error)

ReadFile reads the file named by filename and returns the contents. A successful call returns err == nil, not err == EOF. Because ReadFile reads the whole file, it does not treat an EOF from Read as an error to be reported.

func (*Archive) SetRoot

func (a *Archive) SetRoot(path string)

SetRoot sets the root folder when using development mode.

func (*Archive) Stat

func (a *Archive) Stat(name string) (fi os.FileInfo, err error)

Stat returns a FileInfo describing the named file. If there is an error, it will be of type *PathError.

type File

type File interface {
	Close() error
	Name() string
	Read(b []byte) (n int, err error)
	ReadAt(b []byte, off int64) (n int, err error)
	Readdir(n int) (fi []os.FileInfo, err error)
	Readdirnames(n int) (names []string, err error)
	Seek(offset int64, whence int) (ret int64, err error)
	Stat() (fi os.FileInfo, err error)
}

File represents an open file in the archive.

func NewBogFile

func NewBogFile(data []byte, info os.FileInfo) File

NewBogFile creates a File. Use internally by generator.

func NewBogFolder

func NewBogFolder(children []File, info os.FileInfo) File

NewBogFolder creates a File that represents a folder. Use internally by generator.

type FileInfo

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

A FileInfo describes a file, and implements os.FileInfo interface.

func (*FileInfo) IsDir

func (fi *FileInfo) IsDir() bool

IsDir is abbreviation for Mode().IsDir()

func (*FileInfo) ModTime

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

ModTime returns modification time

func (*FileInfo) Mode

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

Mode returns file mode bits

func (*FileInfo) Name

func (fi *FileInfo) Name() string

Name returns base name of the file.

func (*FileInfo) Size

func (fi *FileInfo) Size() int64

Size returns length in bytes for regular files; system-dependent for others.

func (*FileInfo) Sys

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

Sys is always nil

Directories

Path Synopsis
tool
bog
Bog command is used for archiving and extracting your files.
Bog command is used for archiving and extracting your files.

Jump to

Keyboard shortcuts

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