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

bazelisk.go: add simple .bazeliskrc file support #127

Closed
wants to merge 1 commit into from
Closed
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
62 changes: 54 additions & 8 deletions bazelisk.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"sort"
"strconv"
"strings"
"sync"
"syscall"
"time"

Expand All @@ -48,8 +49,53 @@ const (
var (
// BazeliskVersion is filled in via x_defs when building a release.
BazeliskVersion = "development"

fileConfig map[string]string
fileConfigOnce sync.Once
)

// getEnvOrConfig will read a configuration value from the environment, but fall back to reading it from .bazeliskrc in the workspace root.
func getEnvOrConfig(name string) string {
if val := os.Getenv(name); val != "" {
return val
}

// Parse .bazeliskrc in the workspace root, once, if it can be found.
fileConfigOnce.Do(func() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: for my taste a bit too long for an anonymous function

workingDirectory, err := os.Getwd()
if err != nil {
return
}
workspaceRoot := findWorkspaceRoot(workingDirectory)
if workspaceRoot == "" {
return
}
rcFilePath := filepath.Join(workspaceRoot, ".bazeliskrc")
contents, err := ioutil.ReadFile(rcFilePath)
if err != nil {
if os.IsNotExist(err) {
return
}
log.Fatal(err)
}
fileConfig = make(map[string]string)
for _, line := range strings.Split(string(contents), "\n") {
parts := strings.SplitN(line, "=", 2)
if len(parts) < 2 {
continue
}
key := strings.TrimSpace(parts[0])
if strings.HasPrefix(key, "#") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slightly more efficient if this check is the first line inside the loop

// comments
continue
}
fileConfig[key] = strings.TrimSpace(parts[1])
}
})

return fileConfig[name]
}

func findWorkspaceRoot(root string) string {
if _, err := os.Stat(filepath.Join(root, "WORKSPACE")); err == nil {
return root
Expand Down Expand Up @@ -78,7 +124,7 @@ func getBazelVersion() (string, error) {
// - workspace_root/.bazelversion exists -> read contents, that version.
// - workspace_root/WORKSPACE contains a version -> that version. (TODO)
// - fallback: latest release
bazelVersion := os.Getenv("USE_BAZEL_VERSION")
bazelVersion := getEnvOrConfig("USE_BAZEL_VERSION")
if len(bazelVersion) != 0 {
return bazelVersion, nil
}
Expand Down Expand Up @@ -180,7 +226,7 @@ func maybeDownload(bazeliskHome, url, filename, description string) ([]byte, err
}

// We could also use go-github here, but I can't get it to build with Bazel's rules_go and it pulls in a lot of dependencies.
body, err := readRemoteFile(url, os.Getenv("BAZELISK_GITHUB_TOKEN"))
body, err := readRemoteFile(url, getEnvOrConfig("BAZELISK_GITHUB_TOKEN"))
if err != nil {
return nil, fmt.Errorf("could not download %s: %v", description, err)
}
Expand Down Expand Up @@ -382,7 +428,7 @@ func determineBazelFilename(version string) (string, error) {
}

func determineURL(fork string, version string, isCommit bool, filename string) string {
baseURL := os.Getenv("BAZELISK_BASE_URL")
baseURL := getEnvOrConfig("BAZELISK_BASE_URL")

if isCommit {
if len(baseURL) == 0 {
Expand Down Expand Up @@ -466,7 +512,7 @@ func downloadBazel(fork string, version string, isCommit bool, directory string)
}

func maybeDelegateToWrapper(bazel string) string {
if os.Getenv(skipWrapperEnv) != "" {
if getEnvOrConfig(skipWrapperEnv) != "" {
return bazel
}

Expand Down Expand Up @@ -616,7 +662,7 @@ func insertArgs(baseArgs []string, newArgs []string) []string {
}

func shutdownIfNeeded(bazelPath string) {
bazeliskClean := os.Getenv("BAZELISK_SHUTDOWN")
bazeliskClean := getEnvOrConfig("BAZELISK_SHUTDOWN")
if len(bazeliskClean) == 0 {
return
}
Expand All @@ -634,7 +680,7 @@ func shutdownIfNeeded(bazelPath string) {
}

func cleanIfNeeded(bazelPath string) {
bazeliskClean := os.Getenv("BAZELISK_CLEAN")
bazeliskClean := getEnvOrConfig("BAZELISK_CLEAN")
if len(bazeliskClean) == 0 {
return
}
Expand Down Expand Up @@ -736,7 +782,7 @@ func dirForURL(url string) string {
}

func main() {
bazeliskHome := os.Getenv("BAZELISK_HOME")
bazeliskHome := getEnvOrConfig("BAZELISK_HOME")
if len(bazeliskHome) == 0 {
userCacheDir, err := os.UserCacheDir()
if err != nil {
Expand Down Expand Up @@ -779,7 +825,7 @@ func main() {
log.Fatalf("could not resolve the version '%s' to an actual version number: %v", bazelVersion, err)
}

bazelForkOrURL := dirForURL(os.Getenv("BAZELISK_BASE_URL"))
bazelForkOrURL := dirForURL(getEnvOrConfig("BAZELISK_BASE_URL"))
if len(bazelForkOrURL) == 0 {
bazelForkOrURL = bazelFork
}
Expand Down