add a writeTemp helper func

We had two nearly identical copies of the code to open a temp file with
CreateTemp, write to it, and handle closing properly. Unify them.

With the added docs this isn't exactly a net win, but we want the long
funcs such as transformCompile to be easy to follow, and this helps.
pull/284/head
Daniel Martí 4 years ago committed by lu4p
parent 13e4ba2ae0
commit a1f11fb231

@ -479,23 +479,35 @@ func transformAsm(args []string) ([]string, error) {
} }
// TODO: do the original asm filenames ever matter? // TODO: do the original asm filenames ever matter?
tempFile, err := os.CreateTemp(sharedTempDir, "*.s") if path, err := writeTemp("*.s", buf.Bytes()); err != nil {
if err != nil {
return nil, err return nil, err
} else {
newPaths = append(newPaths, path)
} }
defer tempFile.Close() }
if _, err := tempFile.Write(buf.Bytes()); err != nil { return append(flags, newPaths...), nil
return nil, err }
}
if err := tempFile.Close(); err != nil {
return nil, err
}
newPaths = append(newPaths, tempFile.Name()) // writeTemp is a mix between os.CreateTemp and os.WriteFile, as it writes a
// temporary file in sharedTempDir given an input buffer.
//
// This helper func also makes the "defer" more truthful, as it's often used
// within a loop.
func writeTemp(name string, content []byte) (string, error) {
tempFile, err := os.CreateTemp(sharedTempDir, name)
if err != nil {
return "", err
} }
defer tempFile.Close()
return append(flags, newPaths...), nil if _, err := tempFile.Write(content); err != nil {
return "", err
}
if err := tempFile.Close(); err != nil {
return "", err
}
return tempFile.Name(), nil
} }
func transformCompile(args []string) ([]string, error) { func transformCompile(args []string) ([]string, error) {
@ -677,13 +689,10 @@ func transformCompile(args []string) ([]string, error) {
// fmt.Fprintf(os.Stderr, "\n-- %s/%s --\n%s", curPkg.ImportPath, origName, src) // fmt.Fprintf(os.Stderr, "\n-- %s/%s --\n%s", curPkg.ImportPath, origName, src)
// } // }
tempFile, err := os.CreateTemp(sharedTempDir, name+".*.go") if path, err := writeTemp(name+".*.go", src); err != nil {
if err != nil {
return nil, err
}
defer tempFile.Close()
if _, err := tempFile.Write(src); err != nil {
return nil, err return nil, err
} else {
newPaths = append(newPaths, path)
} }
if opts.DebugDir != "" { if opts.DebugDir != "" {
osPkgPath := filepath.FromSlash(curPkg.ImportPath) osPkgPath := filepath.FromSlash(curPkg.ImportPath)
@ -697,12 +706,6 @@ func transformCompile(args []string) ([]string, error) {
return nil, err return nil, err
} }
} }
if err := tempFile.Close(); err != nil {
return nil, err
}
newPaths = append(newPaths, tempFile.Name())
} }
flags = flagSetValue(flags, "-importcfg", newImportCfg) flags = flagSetValue(flags, "-importcfg", newImportCfg)

Loading…
Cancel
Save