|
|
|
@ -11,29 +11,29 @@ import (
|
|
|
|
|
// The file is kept as open, the caller should close the file handle,
|
|
|
|
|
// and remove the file by name.
|
|
|
|
|
func TempFileWithText(text string) (*os.File, error) {
|
|
|
|
|
tmpfile, err := os.CreateTemp(os.TempDir(), hash.Md5Hex([]byte(text)))
|
|
|
|
|
tmpFile, err := os.CreateTemp(os.TempDir(), hash.Md5Hex([]byte(text)))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := os.WriteFile(tmpfile.Name(), []byte(text), os.ModeTemporary); err != nil {
|
|
|
|
|
if err := os.WriteFile(tmpFile.Name(), []byte(text), os.ModeTemporary); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tmpfile, nil
|
|
|
|
|
return tmpFile, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TempFilenameWithText creates the file with the given content,
|
|
|
|
|
// and returns the filename (full path).
|
|
|
|
|
// The caller should remove the file after use.
|
|
|
|
|
func TempFilenameWithText(text string) (string, error) {
|
|
|
|
|
tmpfile, err := TempFileWithText(text)
|
|
|
|
|
tmpFile, err := TempFileWithText(text)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
filename := tmpfile.Name()
|
|
|
|
|
if err = tmpfile.Close(); err != nil {
|
|
|
|
|
filename := tmpFile.Name()
|
|
|
|
|
if err = tmpFile.Close(); err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|