Reverts change to -out flag. One should specify the full path

of the target file, instead of just the target directory.

This allows us to overwrite a generated file from a previous
build cycle. This addresses issue #8
pull/4/head
Jim Teeuwen 2013-08-01 12:10:59 +02:00
parent 98703b5677
commit 0cd33e9b7d
3 changed files with 32 additions and 36 deletions

37
main.go
View File

@ -18,7 +18,7 @@ import (
var (
pipe = false
in = ""
out = flag.String("out", "", "Optional path to the output directory.")
out = flag.String("out", "", "Optional path and name of the output file.")
pkgname = flag.String("pkg", "main", "Name of the package to generate.")
funcname = flag.String("func", "", "Optional name of the function to generate.")
prefix = flag.String("prefix", "", "Optional path prefix to strip off map keys and function names.")
@ -59,7 +59,8 @@ func main() {
// Append the TOC init function to the end of the output file and
// write the `bindata-toc.go` file, if applicable.
if *toc {
err := createTOC(*out, *pkgname)
dir, _ := filepath.Split(*out)
err := createTOC(dir, *pkgname)
if err != nil {
fmt.Fprintf(os.Stderr, "[e] %s\n", err)
@ -114,28 +115,14 @@ func parseArgs() {
}
// safeFilename creates a safe output filename from the given
// output base directory and input filename.
// output and input paths.
func safeFilename(out, in string) string {
dir, in := filepath.Split(in)
var filename string
if len(out) == 0 {
out = dir
} else {
out, _ = filepath.Abs(filepath.Clean(out))
}
// Ensure output directory exists while we're here.
stat, err := os.Lstat(out)
if err != nil {
os.MkdirAll(out, 0755)
} else if !stat.IsDir() {
fmt.Fprintf(os.Stderr, "Output path is an existing file.\n")
os.Exit(1)
}
filename := path.Join(out, in+".go")
_, err = os.Lstat(filename)
filename = in + ".go"
_, err := os.Lstat(filename)
if err == nil {
// File already exists. Pad name with a sequential number until we
// find a name that is available.
@ -152,6 +139,16 @@ func safeFilename(out, in string) string {
count++
}
}
} else {
filename, _ = filepath.Abs(filepath.Clean(out))
}
// Ensure output directory exists while we're here.
dir, _ := filepath.Split(filename)
_, err := os.Lstat(dir)
if err != nil {
os.MkdirAll(dir, 0755)
}
return filename
}

5
toc.go
View File

@ -13,9 +13,8 @@ import (
)
// createTOC writes a table of contents file to the given location.
func createTOC(file, pkgname string) error {
dir, _ := filepath.Split(file)
file = filepath.Join(dir, "bindata-toc.go")
func createTOC(dir, pkgname string) error {
file := filepath.Join(dir, "bindata-toc.go")
code := fmt.Sprintf(`package %s
// Global Table of Contents map. Generated by go-bindata.