@ -17,47 +17,49 @@ limitations under the License.
package helmpath
package helmpath
import (
import (
"fmt"
"github.com/casimir/xdg-go"
"github.com/casimir/xdg-go"
"io"
"k8s.io/client-go/util/homedir"
"k8s.io/client-go/util/homedir"
"os"
"os"
"path/filepath"
"path/filepath"
)
)
// Old default helm home, it's old good ~/.helm
var oldDefaultHelmHome = filepath . Join ( homedir . HomeDir ( ) , ".helm" )
// New default helm home, with different paths for different OS:
// New default helm home, with different paths for different OS:
// - %APPDATA%\helm on Windows
// - %APPDATA%\helm on Windows
// - ~/Library/Preferences/helm on OSX
// - ~/Library/Preferences/helm on OSX
// - $XDG_CONFIG_DIR/helm (typically ~/.config/helm for linux)
// - $XDG_CONFIG_DIR/helm (typically ~/.config/helm for linux)
var defaultHelmHome = filepath . Join ( xdg . ConfigHome ( ) , "helm" )
var defaultHelmHome = filepath . Join ( xdg . ConfigHome ( ) , "helm" )
func DirExists ( path string ) bool {
// Old default helm home, it's old good ~/.helm
osStat , err := os . Stat ( path )
var oldDefaultHelmHome = filepath . Join ( homedir . HomeDir ( ) , ".helm" )
return err == nil && osStat . IsDir ( )
type DefaultConfigHomePath interface {
xdgHomeExists ( ) bool
basicHomeExists ( ) bool
}
}
// Check whether new default helm home exists
type FSConfigHomePath struct { DefaultConfigHomePath }
// TODO: improve me
var DefaultHelmHomeExists = func ( ) bool {
// Checks whether $XDG_CONFIG_HOME/helm exists
func ( FSConfigHomePath ) xdgHomeExists ( ) bool {
return DirExists ( defaultHelmHome )
return DirExists ( defaultHelmHome )
}
}
// Checks whether old-style ~/.helm exists
// Checks whether ~/.helm exists
// TODO: improve me
func ( FSConfigHomePath ) basicHomeExists ( ) bool {
var OldDefaultHelmHomeExists = func ( ) bool {
return DirExists ( oldDefaultHelmHome )
return DirExists ( oldDefaultHelmHome )
}
}
var ConfigPath DefaultConfigHomePath = FSConfigHomePath { }
func DirExists ( path string ) bool {
osStat , err := os . Stat ( path )
return err == nil && osStat . IsDir ( )
}
// Get configuration home dir.
// Get configuration home dir.
//
func GetDefaultConfigHome ( ) string {
// Note: Temporal until all migrate to XDG Base Directory spec
if ConfigPath . xdgHomeExists ( ) || ! ConfigPath . basicHomeExists ( ) {
func GetDefaultConfigHome ( out io . Writer ) string {
if DefaultHelmHomeExists ( ) || ! OldDefaultHelmHomeExists ( ) {
return defaultHelmHome
return defaultHelmHome
}
}
fmt . Fprintf ( out , "WARNING: using old-style configuration directory. Please, consider moving it to %s\n" , defaultHelmHome )
return oldDefaultHelmHome
return oldDefaultHelmHome
}
}