package
0.0.0-20180711164918-19f838fd53de
Repository: https://github.com/brentp/go-athenaeum.git
Documentation: pkg.go.dev

# 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")
}

# Functions

Cleanup must be called via defer inside of main.
Exit the program after cleaning up.
Fatalf calls log.Fatalf after cleaning up.
TempDir creates a new temp directory using ioutil.TempDir and registers it for cleanup when the program exits.
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.

# Variables

No description provided by the author

# Structs

No description provided by the author