Skip to content

Commit

Permalink
fixes bindir issue
Browse files Browse the repository at this point in the history
should fix #192
  • Loading branch information
leucos committed Apr 25, 2022
1 parent e4e979d commit 12faba0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
4 changes: 2 additions & 2 deletions cmd/root.go
Expand Up @@ -33,15 +33,15 @@ selected.`,

rootCmd.PersistentFlags().BoolP("verbose", "v", false, "Verbose operation (env: BINENV_VERBOSE)")
rootCmd.PersistentFlags().BoolP("global", "g", false, "Global mode (env: BINENV_GLOBAL)")
rootCmd.PersistentFlags().StringP("bindir", "b", "~/.binenv", "Binaries directory")
rootCmd.PersistentFlags().StringP("bindir", "b", "", "Binaries directory")

if !strings.HasSuffix(os.Args[0], "binenv") {
verbose := truthify(os.Getenv("BINENV_VERBOSE"))
global := truthify(os.Getenv("BINENV_GLOBAL"))

a.SetGlobal(global)
a.SetVerbose(verbose)

a.Execute(os.Args)
}

Expand Down
15 changes: 12 additions & 3 deletions internal/app/app.go
Expand Up @@ -421,7 +421,7 @@ func (a *App) install(dist, version string) (string, error) {

// Install new shim version if needed
if dist == "binenv" {
a.logger.Info().Msg("executing self install")
a.logger.Info().Msgf("executing self install using bindir %s", a.bindir)
err = a.selfInstall(version)
if err != nil {
a.logger.Error().Err(err).Msg("unable to set-up myself")
Expand Down Expand Up @@ -830,6 +830,7 @@ func (a *App) Execute(args []string) {
Str("cachedir", a.cachedir).
Str("configdir", a.configdir).
Msg("directory settings")

// Check if args[0] is managed by us. If not write an error and exit. This
// should not happen since, if we are here, we must have used a symlink to
// the shim.
Expand Down Expand Up @@ -867,6 +868,8 @@ func (a *App) selfInstall(version string) error {
if a.global {
mode = 0755
}

a.logger.Debug().Msgf("creating bindir %s", a.bindir)
err := os.MkdirAll(a.bindir, mode)
if err != nil {
return err
Expand All @@ -884,6 +887,8 @@ func (a *App) selfInstall(version string) error {
shim := filepath.Join(a.bindir, "/shim")
shimnew := shim + ".new"

a.logger.Debug().Msgf("installing shim in %s", shim)

if _, err := os.Stat(shim); os.IsExist(err) {
shimold := shim + ".old"
rerr := os.Rename(shim, shimold)
Expand Down Expand Up @@ -1281,12 +1286,16 @@ func WithBinDir(dir string) func(*App) error {

// SetBinDir sets bin directory to use
func (a *App) SetBinDir(d string) error {
if d == "" {
return nil
}

a.bindir = d

a.logger.Debug().
Str("bindir", a.bindir).
Msg("setting configuration")

a.bindir = d

return nil
}

Expand Down

0 comments on commit 12faba0

Please sign in to comment.