Skip to content

Commit

Permalink
.bazeliskrc: Add Support for Env Var and Tilde Expansion (bazelbuil…
Browse files Browse the repository at this point in the history
…d#587)

* core: removed unused vars 'fileConfig' and 'fileConfigOnce'

* extract bazeliskHome definition to separate function and add support to handle env variables and tilde expansion in .bazeliskrc
  • Loading branch information
albertocavalcante authored Jul 9, 2024
1 parent cf1ecc9 commit 19ee002
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"runtime"
"sort"
"strings"
"sync"
"syscall"

"github.com/bazelbuild/bazelisk/config"
Expand All @@ -42,9 +41,6 @@ const (
var (
// BazeliskVersion is filled in via x_defs when building a release.
BazeliskVersion = "development"

fileConfig map[string]string
fileConfigOnce sync.Once
)

// ArgsFunc is a function that receives a resolved Bazel version and returns the arguments to invoke
Expand Down Expand Up @@ -97,17 +93,12 @@ func RunBazeliskWithArgsFuncAndConfig(argsFunc ArgsFunc, repos *Repositories, co
func RunBazeliskWithArgsFuncAndConfigAndOut(argsFunc ArgsFunc, repos *Repositories, config config.Config, out io.Writer) (int, error) {
httputil.UserAgent = getUserAgent(config)

bazeliskHome := config.Get("BAZELISK_HOME")
if len(bazeliskHome) == 0 {
userCacheDir, err := os.UserCacheDir()
if err != nil {
return -1, fmt.Errorf("could not get the user's cache directory: %v", err)
}

bazeliskHome = filepath.Join(userCacheDir, "bazelisk")
bazeliskHome, err := getBazeliskHome(config)
if err != nil {
return -1, fmt.Errorf("could not determine Bazelisk home directory: %v", err)
}

err := os.MkdirAll(bazeliskHome, 0755)
err = os.MkdirAll(bazeliskHome, 0755)
if err != nil {
return -1, fmt.Errorf("could not create directory %s: %v", bazeliskHome, err)
}
Expand Down Expand Up @@ -220,6 +211,32 @@ func getBazelCommand(args []string) (string, error) {
return "", fmt.Errorf("could not find a valid Bazel command in %q. Please run `bazel help` if you need help on how to use Bazel", strings.Join(args, " "))
}

// getBazeliskHome returns the path to the Bazelisk home directory.
func getBazeliskHome(config config.Config) (string, error) {
bazeliskHome := config.Get("BAZELISK_HOME")
if len(bazeliskHome) == 0 {
userCacheDir, err := os.UserCacheDir()
if err != nil {
return "", fmt.Errorf("could not get the user's cache directory: %v", err)
}

bazeliskHome = filepath.Join(userCacheDir, "bazelisk")
} else {
// If a custom BAZELISK_HOME is set, handle tilde and var expansion
// before creating the Bazelisk home directory.
var err error

bazeliskHome, err = homedir.Expand(bazeliskHome)
if err != nil {
return "", fmt.Errorf("could not expand home directory in path: %v", err)
}

bazeliskHome = os.ExpandEnv(bazeliskHome)
}

return bazeliskHome, nil
}

func getUserAgent(config config.Config) string {
agent := config.Get("BAZELISK_USER_AGENT")
if len(agent) > 0 {
Expand Down

0 comments on commit 19ee002

Please sign in to comment.