Add support for Windows paths, fix file handling errors for Windows

This commit is contained in:
Connor Peet
2016-04-13 17:27:56 -04:00
parent 92fdbb21de
commit 6a66ca1770
3 changed files with 44 additions and 4 deletions
+9 -4
View File
@@ -119,9 +119,13 @@ func (g *Generator) writeMain() (path string, err error) {
fmt.Fprintln(f, " }")
fmt.Fprintln(f, "}")
p := f.Name()
os.Rename(p, p+".go")
return p + ".go", f.Close()
src := f.Name()
if err := f.Close(); err != nil {
return src, err
}
dest := src + ".go"
return dest, os.Rename(src, dest)
}
func (g *Generator) Run() error {
@@ -144,7 +148,6 @@ func (g *Generator) Run() error {
if err != nil {
return err
}
defer f.Close()
if !g.LeaveTemps {
defer os.Remove(f.Name()) // will not remove after rename
}
@@ -156,6 +159,8 @@ func (g *Generator) Run() error {
return err
}
f.Close()
if !g.NoFormat {
cmd = exec.Command("gofmt", "-w", f.Name())
cmd.Stderr = os.Stderr
+2
View File
@@ -1,3 +1,5 @@
// +build !windows
package parser
import (
+33
View File
@@ -0,0 +1,33 @@
package parser
import (
"fmt"
"os"
"path"
"strings"
)
func normalizePath(path string) string {
return strings.Replace(path, "\\", "/", -1)
}
func getPkgPath(fname string) (string, error) {
if !path.IsAbs(fname) {
pwd, err := os.Getwd()
if err != nil {
return "", err
}
fname = path.Join(pwd, fname)
}
fname = normalizePath(fname)
for _, p := range strings.Split(os.Getenv("GOPATH"), ";") {
prefix := path.Join(normalizePath(p), "src") + "/"
if rel := strings.TrimPrefix(fname, prefix); rel != fname {
return path.Dir(rel), nil
}
}
return "", fmt.Errorf("file '%v' is not in GOPATH", fname)
}