github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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  	if jujuXDGDataHome == "" {
    41  		panic("juju home hasn't been initialized")
    42  	}
    43  	return jujuXDGDataHome
    44  }
    45  
    46  // IsJujuXDGDataHomeSet is a way to check if SetJuuHome has been called.
    47  func IsJujuXDGDataHomeSet() bool {
    48  	jujuXDGDataHomeMu.Lock()
    49  	defer jujuXDGDataHomeMu.Unlock()
    50  	return jujuXDGDataHome != ""
    51  }
    52  
    53  // JujuXDGDataHomePath returns the path to a file in the
    54  // current juju home.
    55  func JujuXDGDataHomePath(names ...string) string {
    56  	all := append([]string{JujuXDGDataHome()}, names...)
    57  	return filepath.Join(all...)
    58  }
    59  
    60  // JujuXDGDataHomeDir returns the directory where juju should store application-specific files
    61  func JujuXDGDataHomeDir() string {
    62  	JujuXDGDataHomeDir := os.Getenv(JujuXDGDataHomeEnvKey)
    63  	if JujuXDGDataHomeDir == "" {
    64  		if runtime.GOOS == "windows" {
    65  			JujuXDGDataHomeDir = jujuXDGDataHomeWin()
    66  		} else {
    67  			JujuXDGDataHomeDir = jujuXDGDataHomeLinux()
    68  		}
    69  	}
    70  	return JujuXDGDataHomeDir
    71  }
    72  
    73  // jujuXDGDataHomeLinux returns the directory where juju should store application-specific files on Linux.
    74  func jujuXDGDataHomeLinux() string {
    75  	xdgConfig := os.Getenv(XDGDataHome)
    76  	if xdgConfig != "" {
    77  		return filepath.Join(xdgConfig, "juju")
    78  	}
    79  	// If xdg config home is not defined, the standard indicates that its default value
    80  	// is $HOME/.local/share
    81  	home := utils.Home()
    82  	return filepath.Join(home, ".local/share", "juju")
    83  }
    84  
    85  // jujuXDGDataHomeWin returns the directory where juju should store application-specific files on Windows.
    86  func jujuXDGDataHomeWin() string {
    87  	appdata := os.Getenv("APPDATA")
    88  	if appdata == "" {
    89  		return ""
    90  	}
    91  	return filepath.Join(appdata, "Juju")
    92  }