Skip to content

Commit

Permalink
fix default overriding bug (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhelmich authored May 24, 2021
1 parent 8074690 commit d8e620d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 20 deletions.
42 changes: 28 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

This package converts any file into go code. It is heavily inspired by the excellent [go-bindata](github.com/jteeuwen/go-bindata).

## Why another library?

I found myself trying a archive quite a few (as in 1000s) of small files. Other libraries make a trade off to compress all files individually in order to speed up access.
This library will archive and compress all files together to decrease the size of the resulting file. On access, it will decompress all files and keep them in memory for frequent access.

## Usage

`bindata` can be used as library or as a commandline tool.

## Usage as Library
Expand All @@ -10,11 +17,9 @@ Import the following package to use bindata in your code:

`github.com/mhelmich/bindata`

### Usage

The following snippet explains how to use bindata to create a bindata file from your Go program.

```
```golang
import bd "github.com/mhelmich/bindata"

...
Expand All @@ -32,33 +37,40 @@ err = bd.New(

Run the following to install bindata:

`go get -u github.com/mhelmich/bindata/cmd/bindata`

### Usage
```shell
> go get -u github.com/mhelmich/bindata/cmd/bindata
```

The simplest call of `bindata` passes only file paths to it.
The simplest call of `bindata` passes only file paths to it. In this invocation, the package name and the output file will be defaulted to `bindata` and `bindata/bindata.go` respectively.

`$ bindata dir/file1.json dir/file2.json`
```shell
> bindata dir/file1.json dir/file2.json
```

However `bindata` paths can contain wildcards/globs:

`$ bindata dir/*.json dir/**/*.json`
```shell
> bindata dir/*.json dir/**/*.json
```

`bindata` can be configured to write the output file to a particular path. In this case, `bindata` automatically uses the last element of the path as package name:

`$ bindata -o mypackage/bindata_files.go dir/*.json dir/**/*.json`
```shell
> bindata -o mypackage/bindata_files.go dir/*.json dir/**/*.json
```

If for some reason you need to configure the package name of the generated bindata file independently from the given path, use the `package` flag:

`$ bindata -o mypackage/bindata_files.go -package otherpackage dir/*.json dir/**/*.json`
```shell
> bindata -o mypackage/bindata_files.go -package otherpackage dir/*.json dir/**/*.json
```

## Configuration

Right now bindata supports the following configurations:

* TarBz - a bz compressed tar archive


## Accessing a File

The generated `bindata` file exports two public functions `FileNames` and `ReadFile`.
Expand All @@ -68,7 +80,8 @@ The generated `bindata` file exports two public functions `FileNames` and `ReadF
`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.

It can be used like this:
```

```golang
data, err := bindata.ReadFile(an)
if err != nil {
// err != nil if the file wasn't found in the bindata file
Expand All @@ -80,7 +93,8 @@ if err != nil {
`FileNames` returns a list of all files in this bindata file.

It can be used like this:
```

```golang
for _, fileName := range bindata.FileNames() {
// fileName contains a file name now
}
Expand Down
8 changes: 7 additions & 1 deletion bindata.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,12 @@ func (b *bindata) Archive() error {
return err
}

err = b.renderFile(input, b.opts.OutputFile, TarBz)
absOutputPath, err := filepath.Abs(b.opts.OutputFile)
if err != nil {
return err
}

err = b.renderFile(input, absOutputPath, TarBz)
if err != nil {
return err
}
Expand Down Expand Up @@ -180,6 +185,7 @@ func (b *bindata) resolvePathsToFiles(paths []string) ([]*pathInfo, error) {
if len(paths) > 0 {
var matchedPaths []string
for _, path := range paths {
path = strings.TrimSpace(path)
if strings.HasPrefix(path, "..") {
glog.Infof("won't include path starting with '..'. Skipping %s", path)
continue
Expand Down
9 changes: 5 additions & 4 deletions cmd/bindata/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var (
func main() {
err := parseFlags()
if err != nil {
_ = fmt.Errorf("error generating bin data file; %s", err.Error())
fmt.Printf("error generating bin data file; %s", err.Error())
os.Exit(1)
}

Expand All @@ -29,9 +29,10 @@ func main() {
bindata.Compressor(bindata.Bz),
).Archive()
if err != nil {
_ = fmt.Errorf("error generating bin data file; %s", err.Error())
fmt.Printf("error generating bin data file; %s", err.Error())
os.Exit(1)
}
fmt.Printf("Successfully generated bindata file at '%s'", *outputFlag)
}

func parseFlags() error {
Expand All @@ -41,11 +42,11 @@ func parseFlags() error {
}

if packageNameFlag == nil || *packageNameFlag == "" {
return fmt.Errorf("package flag not set")
*packageNameFlag = "bindata"
}

if outputFlag == nil || *outputFlag == "" {
return fmt.Errorf("output flag not set")
*outputFlag = "bindata/bindata.go"
}

return nil
Expand Down
7 changes: 6 additions & 1 deletion render.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
)

Expand Down Expand Up @@ -47,9 +48,13 @@ func (b *bindata) renderFile(input *templateInput, outputFile string, typ Type)

func (b *bindata) mkdir(outputFile string) error {
dir := filepath.Dir(outputFile)
_, err := os.Stat(dir)
stat, err := os.Stat(dir)
if os.IsNotExist(err) {
return os.MkdirAll(dir, 0700)
} else if !stat.IsDir() {
tokens := strings.Split(dir, string(filepath.Separator))
p := strings.Join(tokens[:len(tokens)-1], string(filepath.Separator))
return fmt.Errorf("can't create directory '%s' - found file with the same name '%s' in folder '%s'", filepath.Base(dir), filepath.Base(dir), p)
}

return nil
Expand Down

0 comments on commit d8e620d

Please sign in to comment.