Skip to content

Commit

Permalink
adds safeguards to avoid using '~' directory
Browse files Browse the repository at this point in the history
if homedir can not be guessed, we have to exit.
previously, '~' was used but this directory is not expanded so we can not use this.
  • Loading branch information
leucos committed Apr 25, 2022
1 parent 8998f05 commit 0dc8de9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 22 deletions.
30 changes: 26 additions & 4 deletions cmd/root.go
Expand Up @@ -90,13 +90,35 @@ selected.`,
},
}

// If we can not guess those directories, we have to bailout
dbin, err := app.GetDefaultBinDir()
if err != nil {
err = fmt.Errorf("unable to guess binaries directory: %v", err)
panic(err)
}
dlink, err := app.GetDefaultLinkDir()
if err != nil {
err = fmt.Errorf("unable to guess link directory: %v", err)
panic(err)
}
dcache, err := app.GetDefaultCacheDir()
if err != nil {
err = fmt.Errorf("unable to guess cache directory: %v", err)
panic(err)
}
dconf, err := app.GetDefaultConfDir()
if err != nil {
err = fmt.Errorf("unable to guess conf directory: %v", err)
panic(err)
}

rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose operation [BINENV_VERBOSE]")
rootCmd.PersistentFlags().BoolVarP(&global, "global", "g", false, "global mode [BINENV_GLOBAL]")

rootCmd.PersistentFlags().StringVarP(&bindir, "bindir", "B", app.GetDefaultBinDir(), "binaries directory [BINENV_BINDIR]")
rootCmd.PersistentFlags().StringVarP(&linkdir, "linkdir", "L", app.GetDefaultLinkDir(), "link directory [BINENV_LINKDIR]")
rootCmd.PersistentFlags().StringVarP(&cachedir, "cachedir", "K", app.GetDefaultCacheDir(), "cache directory [BINENV_CACHEDIR]")
rootCmd.PersistentFlags().StringVarP(&confdir, "confdir", "C", app.GetDefaultConfDir(), "distributions configuration directory [BINENV_CONFDIR]")
rootCmd.PersistentFlags().StringVarP(&bindir, "bindir", "B", dbin, "binaries directory [BINENV_BINDIR]")
rootCmd.PersistentFlags().StringVarP(&linkdir, "linkdir", "L", dlink, "link directory [BINENV_LINKDIR]")
rootCmd.PersistentFlags().StringVarP(&cachedir, "cachedir", "K", dcache, "cache directory [BINENV_CACHEDIR]")
rootCmd.PersistentFlags().StringVarP(&confdir, "confdir", "C", dconf, "distributions configuration directory [BINENV_CONFDIR]")

rootCmd.AddCommand(
completionCmd(),
Expand Down
36 changes: 18 additions & 18 deletions internal/app/utils.go
Expand Up @@ -10,51 +10,51 @@ import (
)

// GetDefaultBinDir returns the bin directory in usermode
func GetDefaultBinDir() string {
func GetDefaultBinDir() (string, error) {
d, err := homedir.Dir()
if err != nil {
d = "~"
return "", err
}
d += "/.binenv/"

return d
return d, nil
}

// GetDefaultLinkDir returns the bin directory in usermode
func GetDefaultLinkDir() string {
func GetDefaultLinkDir() (string, error) {
return GetDefaultBinDir()
}

// GetDefaultConfDir returns the config directory in usermode
func GetDefaultConfDir() string {
func GetDefaultConfDir() (string, error) {
var err error

dir := os.Getenv("XDG_CONFIG_HOME")
if dir == "" {
dir, err = homedir.Dir()
d := os.Getenv("XDG_CONFIG_HOME")
if d == "" {
d, err = homedir.Dir()
if err != nil {
dir = "~"
return "", err
}
dir += "/.config/binenv"
d += "/.config/binenv"
}

return dir
return d, nil
}

// GetDefaultCacheDir returns the cache directory in usermode
func GetDefaultCacheDir() string {
func GetDefaultCacheDir() (string, error) {
var err error

dir := os.Getenv("XDG_CACHE_HOME")
if dir == "" {
dir, err = homedir.Dir()
d := os.Getenv("XDG_CACHE_HOME")
if d == "" {
d, err = homedir.Dir()
if err != nil {
dir = "~"
return "", err
}
dir += "/.cache/binenv"
d += "/.cache/binenv"
}

return dir
return d, nil
}

func stringInSlice(st string, sl []string) bool {
Expand Down

0 comments on commit 0dc8de9

Please sign in to comment.