github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/juju/osenv/home.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package osenv
     5  
     6  import (
     7  	"os"
     8  	"path/filepath"
     9  	"runtime"
    10  	"sync"
    11  
    12  	"github.com/juju/utils"
    13  )
    14  
    15  // jujuXDGDataHome stores the path to the juju configuration
    16  // folder, which is only meaningful when running the juju
    17  // CLI tool, and is typically defined by $JUJU_DATA or
    18  // $XDG_DATA_HOME/juju or ~/.local/share/juju as default if none
    19  // of the aforementioned variables are defined.
    20  var (
    21  	jujuXDGDataHomeMu sync.Mutex
    22  	jujuXDGDataHome   string
    23  )
    24  
    25  // SetJujuXDGDataHome sets the value of juju home and
    26  // returns the current one.
    27  func SetJujuXDGDataHome(newJujuXDGDataHomeHome string) string {
    28  	jujuXDGDataHomeMu.Lock()
    29  	defer jujuXDGDataHomeMu.Unlock()
    30  
    31  	oldJujuXDGDataHomeHome := jujuXDGDataHome
    32  	jujuXDGDataHome = newJujuXDGDataHomeHome
    33  	return oldJujuXDGDataHomeHome
    34  }
    35  
    36  // JujuXDGDataHome returns the current juju home.
    37  func JujuXDGDataHome() string {
    38  	jujuXDGDataHomeMu.Lock()
    39  	defer jujuXDGDataHomeMu.Unlock()
    40  	return jujuXDGDataHome
    41  }
    42  
    43  // JujuXDGDataHomePath returns the path to a file in the
    44  // current juju home.
    45  func JujuXDGDataHomePath(names ...string) string {
    46  	all := append([]string{JujuXDGDataHomeDir()}, names...)
    47  	return filepath.Join(all...)
    48  }
    49  
    50  // JujuXDGDataHomeDir returns the directory where juju should store application-specific files
    51  func JujuXDGDataHomeDir() string {
    52  	JujuXDGDataHomeDir := JujuXDGDataHome()
    53  	if JujuXDGDataHomeDir != "" {
    54  		return JujuXDGDataHomeDir
    55  	}
    56  	JujuXDGDataHomeDir = os.Getenv(JujuXDGDataHomeEnvKey)
    57  	if JujuXDGDataHomeDir == "" {
    58  		if runtime.GOOS == "windows" {
    59  			JujuXDGDataHomeDir = jujuXDGDataHomeWin()
    60  		} else {
    61  			JujuXDGDataHomeDir = jujuXDGDataHomeLinux()
    62  		}
    63  	}
    64  	return JujuXDGDataHomeDir
    65  }
    66  
    67  // jujuXDGDataHomeLinux returns the directory where juju should store application-specific files on Linux.
    68  func jujuXDGDataHomeLinux() string {
    69  	xdgConfig := os.Getenv(XDGDataHome)
    70  	if xdgConfig != "" {
    71  		return filepath.Join(xdgConfig, "juju")
    72  	}
    73  	// If xdg config home is not defined, the standard indicates that its default value
    74  	// is $HOME/.local/share
    75  	home := utils.Home()
    76  	return filepath.Join(home, ".local", "share", "juju")
    77  }
    78  
    79  // jujuXDGDataHomeWin returns the directory where juju should store application-specific files on Windows.
    80  func jujuXDGDataHomeWin() string {
    81  	appdata := os.Getenv("APPDATA")
    82  	if appdata == "" {
    83  		return ""
    84  	}
    85  	return filepath.Join(appdata, "Juju")
    86  }