From 0cd33e9b7d4ca33ba2525fbd42ee862f17c37399 Mon Sep 17 00:00:00 2001 From: Jim Teeuwen Date: Thu, 1 Aug 2013 12:10:59 +0200 Subject: [PATCH] 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 --- main.go | 59 +++++++++++++++++++---------------------- testdata/bindata-toc.go | 2 +- toc.go | 7 +++-- 3 files changed, 32 insertions(+), 36 deletions(-) diff --git a/main.go b/main.go index 618f461..3541d92 100644 --- a/main.go +++ b/main.go @@ -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,43 +115,39 @@ 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 + 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. + count := 0 + + for { + filename = path.Join(out, fmt.Sprintf("%s.%d.go", in, count)) + _, err = os.Lstat(filename) + + if err != nil { + break + } + + count++ + } + } } else { - out, _ = filepath.Abs(filepath.Clean(out)) + filename, _ = filepath.Abs(filepath.Clean(out)) } // Ensure output directory exists while we're here. - stat, err := os.Lstat(out) + dir, _ := filepath.Split(filename) + _, err := os.Lstat(dir) 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) - - if err == nil { - // File already exists. Pad name with a sequential number until we - // find a name that is available. - count := 0 - - for { - filename = path.Join(out, fmt.Sprintf("%s.%d.go", in, count)) - _, err = os.Lstat(filename) - - if err != nil { - break - } - - count++ - } + os.MkdirAll(dir, 0755) } return filename diff --git a/testdata/bindata-toc.go b/testdata/bindata-toc.go index 3d98521..65141e5 100644 --- a/testdata/bindata-toc.go +++ b/testdata/bindata-toc.go @@ -4,4 +4,4 @@ package main // After startup of the program, all generated data files will // put themselves in this map. The key is the full filename, as // supplied to go-bindata. -var go_bindata = make(map[string] func() []byte) \ No newline at end of file +var go_bindata = make(map[string]func() []byte) \ No newline at end of file diff --git a/toc.go b/toc.go index 451f3ef..54c73b7 100644 --- a/toc.go +++ b/toc.go @@ -13,16 +13,15 @@ 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. // After startup of the program, all generated data files will // put themselves in this map. The key is the full filename, as // supplied to go-bindata. -var go_bindata = make(map[string] func() []byte)`, pkgname) +var go_bindata = make(map[string]func() []byte)`, pkgname) return ioutil.WriteFile(file, []byte(code), 0600) }