github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/internal/acme/autocert/cache.go (about)

     1  package autocert
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"runtime"
     7  )
     8  
     9  func homeDir() string {
    10  	if runtime.GOOS == "windows" {
    11  		return os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
    12  	}
    13  	if h := os.Getenv("HOME"); h != "" {
    14  		return h
    15  	}
    16  	return "/"
    17  }
    18  
    19  func cacheDir() string {
    20  	const base = "autocert"
    21  	switch runtime.GOOS {
    22  	case "darwin":
    23  		return filepath.Join(homeDir(), "Library", "Caches", base)
    24  	case "windows":
    25  		for _, ev := range []string{"APPDATA", "CSIDL_APPDATA", "TEMP", "TMP"} {
    26  			if v := os.Getenv(ev); v != "" {
    27  				return filepath.Join(v, base)
    28  			}
    29  		}
    30  		// Worst case:
    31  		return filepath.Join(homeDir(), base)
    32  	}
    33  	if xdg := os.Getenv("XDG_CACHE_HOME"); xdg != "" {
    34  		return filepath.Join(xdg, base)
    35  	}
    36  	return filepath.Join(homeDir(), ".cache", base)
    37  }