github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/utils/consts/temp.go (about)

     1  package consts
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/lmorg/murex/app"
     7  )
     8  
     9  // TempDir is the location of temp directory
    10  var TempDir string
    11  
    12  func init() {
    13  	var err error
    14  
    15  	TempDir, err = os.MkdirTemp("", app.Name)
    16  	if err != nil || TempDir == "" {
    17  		TempDir = tempDir
    18  	}
    19  
    20  	if TempDir[len(TempDir)-1:] != PathSlash {
    21  		TempDir += PathSlash
    22  	}
    23  
    24  	createDirIfNotExist(TempDir)
    25  }
    26  
    27  func createDirIfNotExist(dir string) {
    28  	if _, err := os.Stat(dir); os.IsNotExist(err) {
    29  		err = os.MkdirAll(dir, 0755)
    30  		if err != nil {
    31  			_, err = os.Stderr.WriteString("WARNING: temp directory doesn't exist and unable to create it. This might cause problems.\nTemp directory: " + dir)
    32  
    33  			if err != nil {
    34  				panic("Unable to create tmp directories, unable to write to STDERR. Something is amiss")
    35  			}
    36  		}
    37  	}
    38  }