forked from mirror/enumer
Write to tempfile first then rename
This commit is contained in:
parent
a4db2f8dd0
commit
2615c451ce
19
stringer.go
19
stringer.go
|
@ -126,16 +126,31 @@ func main() {
|
||||||
// Format the output.
|
// Format the output.
|
||||||
src := g.format()
|
src := g.format()
|
||||||
|
|
||||||
// Write to file.
|
// Figure out filename to write to
|
||||||
outputName := *output
|
outputName := *output
|
||||||
if outputName == "" {
|
if outputName == "" {
|
||||||
baseName := fmt.Sprintf("%s_enumer.go", types[0])
|
baseName := fmt.Sprintf("%s_enumer.go", types[0])
|
||||||
outputName = filepath.Join(dir, strings.ToLower(baseName))
|
outputName = filepath.Join(dir, strings.ToLower(baseName))
|
||||||
}
|
}
|
||||||
err := ioutil.WriteFile(outputName, src, 0644)
|
|
||||||
|
// Write to tmpfile first
|
||||||
|
tmpFile, err := ioutil.TempFile("", fmt.Sprintf("%s_enumer_", types[0]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Fatalf("creating temporary file for output: %s", err)
|
||||||
|
}
|
||||||
|
_, err = tmpFile.Write(src)
|
||||||
|
if err != nil {
|
||||||
|
tmpFile.Close()
|
||||||
|
os.Remove(tmpFile.Name())
|
||||||
log.Fatalf("writing output: %s", err)
|
log.Fatalf("writing output: %s", err)
|
||||||
}
|
}
|
||||||
|
tmpFile.Close()
|
||||||
|
|
||||||
|
// Rename tmpfile to output file
|
||||||
|
err = os.Rename(tmpFile.Name(), outputName)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("moving tempfile to output file: %s", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// isDirectory reports whether the named file is a directory.
|
// isDirectory reports whether the named file is a directory.
|
||||||
|
|
Loading…
Reference in New Issue