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
This commit is contained in:
parent
98703b5677
commit
0cd33e9b7d
59
main.go
59
main.go
|
@ -18,7 +18,7 @@ import (
|
||||||
var (
|
var (
|
||||||
pipe = false
|
pipe = false
|
||||||
in = ""
|
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.")
|
pkgname = flag.String("pkg", "main", "Name of the package to generate.")
|
||||||
funcname = flag.String("func", "", "Optional name of the function 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.")
|
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
|
// Append the TOC init function to the end of the output file and
|
||||||
// write the `bindata-toc.go` file, if applicable.
|
// write the `bindata-toc.go` file, if applicable.
|
||||||
if *toc {
|
if *toc {
|
||||||
err := createTOC(*out, *pkgname)
|
dir, _ := filepath.Split(*out)
|
||||||
|
err := createTOC(dir, *pkgname)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "[e] %s\n", err)
|
fmt.Fprintf(os.Stderr, "[e] %s\n", err)
|
||||||
|
@ -114,43 +115,39 @@ func parseArgs() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// safeFilename creates a safe output filename from the given
|
// 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 {
|
func safeFilename(out, in string) string {
|
||||||
dir, in := filepath.Split(in)
|
var filename string
|
||||||
|
|
||||||
if len(out) == 0 {
|
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 {
|
} else {
|
||||||
out, _ = filepath.Abs(filepath.Clean(out))
|
filename, _ = filepath.Abs(filepath.Clean(out))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure output directory exists while we're here.
|
// Ensure output directory exists while we're here.
|
||||||
stat, err := os.Lstat(out)
|
dir, _ := filepath.Split(filename)
|
||||||
|
_, err := os.Lstat(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
os.MkdirAll(out, 0755)
|
os.MkdirAll(dir, 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++
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return filename
|
return filename
|
||||||
|
|
2
testdata/bindata-toc.go
vendored
2
testdata/bindata-toc.go
vendored
|
@ -4,4 +4,4 @@ package main
|
||||||
// After startup of the program, all generated data files will
|
// After startup of the program, all generated data files will
|
||||||
// put themselves in this map. The key is the full filename, as
|
// put themselves in this map. The key is the full filename, as
|
||||||
// supplied to go-bindata.
|
// supplied to go-bindata.
|
||||||
var go_bindata = make(map[string] func() []byte)
|
var go_bindata = make(map[string]func() []byte)
|
7
toc.go
7
toc.go
|
@ -13,16 +13,15 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// createTOC writes a table of contents file to the given location.
|
// createTOC writes a table of contents file to the given location.
|
||||||
func createTOC(file, pkgname string) error {
|
func createTOC(dir, pkgname string) error {
|
||||||
dir, _ := filepath.Split(file)
|
file := filepath.Join(dir, "bindata-toc.go")
|
||||||
file = filepath.Join(dir, "bindata-toc.go")
|
|
||||||
code := fmt.Sprintf(`package %s
|
code := fmt.Sprintf(`package %s
|
||||||
|
|
||||||
// Global Table of Contents map. Generated by go-bindata.
|
// Global Table of Contents map. Generated by go-bindata.
|
||||||
// After startup of the program, all generated data files will
|
// After startup of the program, all generated data files will
|
||||||
// put themselves in this map. The key is the full filename, as
|
// put themselves in this map. The key is the full filename, as
|
||||||
// supplied to go-bindata.
|
// 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)
|
return ioutil.WriteFile(file, []byte(code), 0600)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user