diff --git a/cmd/root.go b/cmd/root.go index 468e3f2..83e4b3c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -33,7 +33,7 @@ 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")) @@ -41,7 +41,7 @@ selected.`, a.SetGlobal(global) a.SetVerbose(verbose) - + a.Execute(os.Args) } diff --git a/internal/app/app.go b/internal/app/app.go index 4e9b738..0e8cd10 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -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") @@ -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. @@ -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 @@ -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) @@ -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 }