use 'go' command style recursion indicators on path inputs rather than a recursive flag

This commit is contained in:
Alan Shreve 2013-11-08 17:14:25 +02:00
parent 965fbe401b
commit 2c7ab48825
3 changed files with 34 additions and 8 deletions

View File

@ -10,6 +10,17 @@ import (
"path/filepath" "path/filepath"
) )
// InputConfig defines options on a asset directory to be convert.
type InputConfig struct {
// Path defines a directory containing asset files to be included
// in the generated output.
Path string
// Recusive defines whether subdirectories of Path
// should be recursively included in the conversion.
Recursive bool
}
// Config defines a set of options for the asset conversion. // Config defines a set of options for the asset conversion.
type Config struct { type Config struct {
// Name of the package to use. Defaults to 'main'. // Name of the package to use. Defaults to 'main'.
@ -21,10 +32,9 @@ type Config struct {
// and must follow the build tags syntax specified by the go tool. // and must follow the build tags syntax specified by the go tool.
Tags string Tags string
// Input defines the directory path, containing all asset files. // Input defines the directory path, containing all asset files as
// This may contain sub directories, which will be included in the // well as whether to recursively process assets in any sub directories.
// conversion. Input InputConfig
Input string
// Output defines the output file for the generated code. // Output defines the output file for the generated code.
// If left empty, this defaults to 'bindata.go' in the current // If left empty, this defaults to 'bindata.go' in the current
@ -130,7 +140,7 @@ func (c *Config) validate() error {
return fmt.Errorf("Missing package name") return fmt.Errorf("Missing package name")
} }
stat, err := os.Lstat(c.Input) stat, err := os.Lstat(c.Input.Path)
if err != nil { if err != nil {
return fmt.Errorf("Input path: %v", err) return fmt.Errorf("Input path: %v", err)
} }

View File

@ -26,7 +26,7 @@ func Translate(c *Config) error {
} }
// Locate all the assets. // Locate all the assets.
err = findFiles(c.Input, c.Prefix, c.Recursive, &toc) err = findFiles(c.Input.Path, c.Prefix, c.Input.Recursive, &toc)
if err != nil { if err != nil {
return err return err
} }

View File

@ -10,6 +10,7 @@ import (
"github.com/jteeuwen/go-bindata" "github.com/jteeuwen/go-bindata"
"os" "os"
"path/filepath" "path/filepath"
"strings"
) )
func main() { func main() {
@ -41,7 +42,6 @@ func parseArgs() *bindata.Config {
flag.StringVar(&c.Tags, "tags", c.Tags, "Optional set of uild tags to include.") flag.StringVar(&c.Tags, "tags", c.Tags, "Optional set of uild tags to include.")
flag.StringVar(&c.Prefix, "prefix", c.Prefix, "Optional path prefix to strip off asset names.") flag.StringVar(&c.Prefix, "prefix", c.Prefix, "Optional path prefix to strip off asset names.")
flag.StringVar(&c.Package, "pkg", c.Package, "Package name to use in the generated code.") flag.StringVar(&c.Package, "pkg", c.Package, "Package name to use in the generated code.")
flag.BoolVar(&c.Recursive, "r", c.Recursive, "Recursive processing of the target directory and all its sub-directories.")
flag.BoolVar(&c.NoMemCopy, "nomemcopy", c.NoMemCopy, "Use a .rodata hack to get rid of unnecessary memcopies. Refer to the documentation to see what implications this carries.") flag.BoolVar(&c.NoMemCopy, "nomemcopy", c.NoMemCopy, "Use a .rodata hack to get rid of unnecessary memcopies. Refer to the documentation to see what implications this carries.")
flag.BoolVar(&c.NoCompress, "nocompress", c.NoCompress, "Assets will *not* be GZIP compressed when this flag is specified.") flag.BoolVar(&c.NoCompress, "nocompress", c.NoCompress, "Assets will *not* be GZIP compressed when this flag is specified.")
flag.BoolVar(&version, "version", false, "Displays version information.") flag.BoolVar(&version, "version", false, "Displays version information.")
@ -58,11 +58,27 @@ func parseArgs() *bindata.Config {
os.Exit(1) os.Exit(1)
} }
c.Input = filepath.Clean(flag.Arg(0)) input := filepath.Clean(flag.Arg(0))
c.Input = parseInput(input)
if flag.NArg() > 1 { if flag.NArg() > 1 {
c.Output = filepath.Clean(flag.Arg(1)) c.Output = filepath.Clean(flag.Arg(1))
} }
return c return c
} }
// parseRecursive determines whether the given path has a recrusive indicator and
// returns a new path with the recursive indicator chopped off if it does.
//
// ex:
// /path/to/foo/... -> (/path/to/foo, true)
// /path/to/bar -> (/path/to/bar, false)
func parseInput(path string) bindata.InputConfig {
if strings.HasSuffix(path, "/...") {
return bindata.InputConfig{Path: path[:len(path)-4], Recursive: true}
} else {
return bindata.InputConfig{Path: path, Recursive: false}
}
}