github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/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 // IsJujuHomeSet is a way to check if SetJuuHome has been called. 46 func IsJujuHomeSet() bool { 47 jujuHomeMu.Lock() 48 defer jujuHomeMu.Unlock() 49 return jujuHome != "" 50 } 51 52 // JujuHomePath returns the path to a file in the 53 // current juju home. 54 func JujuHomePath(names ...string) string { 55 all := append([]string{JujuHome()}, names...) 56 return filepath.Join(all...) 57 } 58 59 // JujuHomeDir returns the directory where juju should store application-specific files 60 func JujuHomeDir() string { 61 JujuHomeDir := os.Getenv(JujuHomeEnvKey) 62 if JujuHomeDir == "" { 63 if runtime.GOOS == "windows" { 64 JujuHomeDir = jujuHomeWin() 65 } else { 66 JujuHomeDir = jujuHomeLinux() 67 } 68 } 69 return JujuHomeDir 70 } 71 72 // jujuHomeLinux returns the directory where juju should store application-specific files on Linux. 73 func jujuHomeLinux() string { 74 home := utils.Home() 75 if home == "" { 76 return "" 77 } 78 return filepath.Join(home, ".juju") 79 } 80 81 // jujuHomeWin returns the directory where juju should store application-specific files on Windows. 82 func jujuHomeWin() string { 83 appdata := os.Getenv("APPDATA") 84 if appdata == "" { 85 return "" 86 } 87 return filepath.Join(appdata, "Juju") 88 }