github.com/aclements/go-misc@v0.0.0-20240129233631-2f6ede80790c/findflakes/xdg.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"os"
     9  	"os/user"
    10  	"path/filepath"
    11  	"runtime"
    12  )
    13  
    14  // xdgCacheDir returns the XDG Base Directory Specification cache
    15  // directory.
    16  func xdgCacheDir() string {
    17  	cache := os.Getenv("XDG_CACHE_HOME")
    18  	if cache == "" {
    19  		home := os.Getenv("HOME")
    20  		if home == "" {
    21  			u, err := user.Current()
    22  			if err != nil {
    23  				home = u.HomeDir
    24  			}
    25  		}
    26  		// Not XDG but standard for OS X.
    27  		if runtime.GOOS == "darwin" {
    28  			return filepath.Join(home, "Library/Caches")
    29  		}
    30  		cache = filepath.Join(home, ".cache")
    31  	}
    32  	return cache
    33  }
    34  
    35  // xdgCreateDir creates a directory and its parents in accordance with
    36  // the XDG Base Directory Specification.
    37  func xdgCreateDir(path string) error {
    38  	return os.MkdirAll(path, 0700)
    39  }