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