tempclean

package
v0.0.0-...-19f838f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 11, 2018 License: MIT Imports: 10 Imported by: 2

README

Like ioutil.TempFile/TempDir, but clean up files at exit.

If you don't use log.Fatal, this should cleanup tempfiles as they go out of scope and clean up directories at exit, even when there is an error.

NOTE if you want this libary to clean up, you can not use os.Exit() or log.Fatal. use panic() or tempclean.Exit or tempclean.Fatalf instead.

package main

import (
	"log"
	"os"
	"path/filepath"
	"strings"

	"github.com/brentp/go-athenaeum/tempclean"
)

// global tmpDir to be used by the program.
var tmpDir *tempclean.TmpDir

func init() {
	var err error
	tmpDir, err = tempclean.TempDir("", "mycustomdir")
	if err != nil {
		panic(err)
	}

}

func main() {

	// required in main())
	defer tempclean.Cleanup()
	var tmp *os.File
	var err error

	// create tempfiles in a subdirecotry of the default TMPDIR
	tmp, err = tempclean.TempFile("lumpy-smoother", ".vcf.gz")

	tmp, err = tempclean.TempFile("prefix", "suffix")
	if err != nil {
		log.Fatal(err)
	}

	tmp2, err := tempclean.TempFile("aprefix", "asuffix")
	if err != nil {
		log.Fatal(err)
	}

	if filepath.Dir(tmp.Name()) != filepath.Dir(tmp2.Name()) {
		log.Fatal("expected same path", tmp.Name(), " ", tmp2.Name())
	}

	// can also create a tmp sub-directory and make files inside of it:
	tmpD, err := tempclean.TempDir("./", "asdf")
	if err != nil {
		log.Fatal(err)
	}

	tmpF, err := tmpD.TempFile("my-file-prefix", ".txt")
	if err != nil {
		log.Fatal(err)
	}

	if !strings.HasSuffix(tmpF.Name(), ".txt") {
		panic("expected .txt suffix, got:" + tmpF.Name())
	}

	f, err := tmpDir.TempFile("prefix", ".suffix.txt")
	if err != nil {
		panic(err)
	}
	if !strings.HasSuffix(f.Name(), ".txt") {
		panic("expected .txt suffix, got:" + tmpF.Name())
	}

	log.Println(f.Name())

	// note that we can't recover a log.Fatal, but can still get a panic
	// YES
	panic("i can still cleanup")
	// NO
	// log.Fatal("i can't cleanup")
	// OK
	// tempclean.Cleanup(); log.Fatal("manual")
}

Documentation

Overview

tempclean implements a TempFile that makes a best-effort to clean up at exit

Index

Constants

This section is empty.

Variables

View Source
var DirPrefix = "tempclean-"

Functions

func Cleanup

func Cleanup()

Cleanup must be called via defer inside of main.

func main() {
   defer tempclean.Cleanup()
}

func Exit

func Exit(code int)

Exit the program after cleaning up.

func Fatalf

func Fatalf(pattern string, args ...interface{})

Fatalf calls log.Fatalf after cleaning up.

func TempFile

func TempFile(prefix, suffix string) (f *os.File, err error)

TempFile creates a new temp file using ioutil.TempFile and registers it for cleanup when the program exits if `dir` does not exist, it will be created and used as the base directory in which temp files are created.

Types

type TmpDir

type TmpDir struct {
	// contains filtered or unexported fields
}

func TempDir

func TempDir(dir, prefix string) (t *TmpDir, err error)

TempDir creates a new temp directory using ioutil.TempDir and registers it for cleanup when the program exits.

func (*TmpDir) Remove

func (t *TmpDir) Remove() error

func (*TmpDir) TempFile

func (t *TmpDir) TempFile(prefix, suffix string) (f *os.File, err error)

TempFile creates a new temp file in the directory.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL