Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.bazeliskrc: Add Support for Env Var and Tilde Expansion #587

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"runtime"
"sort"
"strings"
"sync"
"syscall"

"github.com/bazelbuild/bazelisk/config"
Expand All @@ -43,9 +42,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 @@ -98,17 +94,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 @@ -221,6 +212,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