github.com/quay/claircore@v1.5.28/pkg/tmp/file.go (about)

     1  package tmp
     2  
     3  import (
     4  	"os"
     5  )
     6  
     7  // File wraps a *os.File and also implements a Close method which cleans up the file
     8  // from the filesystem
     9  type File struct {
    10  	*os.File
    11  }
    12  
    13  func NewFile(dir, pattern string) (*File, error) {
    14  	f, err := os.CreateTemp(dir, pattern)
    15  	if err != nil {
    16  		return nil, err
    17  	}
    18  
    19  	return &File{f}, nil
    20  }
    21  
    22  // Close closes the file handle and removes the file from the filesystem
    23  func (t *File) Close() error {
    24  	if err := t.File.Close(); err != nil {
    25  		return err
    26  	}
    27  	return os.Remove(t.File.Name())
    28  }