github.com/rogpeppe/juju@v0.0.0-20140613142852-6337964b789e/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  // jujuHome 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_HOME or
    18  // $HOME/.juju as default.
    19  var (
    20  	jujuHomeMu sync.Mutex
    21  	jujuHome   string
    22  )
    23  
    24  // SetJujuHome sets the value of juju home and
    25  // returns the current one.
    26  func SetJujuHome(newJujuHome string) string {
    27  	jujuHomeMu.Lock()
    28  	defer jujuHomeMu.Unlock()
    29  
    30  	oldJujuHome := jujuHome
    31  	jujuHome = newJujuHome
    32  	return oldJujuHome
    33  }
    34  
    35  // JujuHome returns the current juju home.
    36  func JujuHome() string {
    37  	jujuHomeMu.Lock()
    38  	defer jujuHomeMu.Unlock()
    39  	if jujuHome == "" {
    40  		panic("juju home hasn't been initialized")
    41  	}
    42  	return jujuHome
    43  }
    44  
    45  // JujuHomePath returns the path to a file in the
    46  // current juju home.
    47  func JujuHomePath(names ...string) string {
    48  	all := append([]string{JujuHome()}, names...)
    49  	return filepath.Join(all...)
    50  }
    51  
    52  // JujuHomeDir returns the directory where juju should store application-specific files
    53  func JujuHomeDir() string {
    54  	JujuHomeDir := os.Getenv(JujuHomeEnvKey)
    55  	if JujuHomeDir == "" {
    56  		if runtime.GOOS == "windows" {
    57  			JujuHomeDir = jujuHomeWin()
    58  		} else {
    59  			JujuHomeDir = jujuHomeLinux()
    60  		}
    61  	}
    62  	return JujuHomeDir
    63  }
    64  
    65  // jujuHomeLinux returns the directory where juju should store application-specific files on Linux.
    66  func jujuHomeLinux() string {
    67  	home := utils.Home()
    68  	if home == "" {
    69  		return ""
    70  	}
    71  	return filepath.Join(home, ".juju")
    72  }
    73  
    74  // jujuHomeWin returns the directory where juju should store application-specific files on Windows.
    75  func jujuHomeWin() string {
    76  	appdata := os.Getenv("APPDATA")
    77  	if appdata == "" {
    78  		return ""
    79  	}
    80  	return filepath.Join(appdata, "Juju")
    81  }