-
Notifications
You must be signed in to change notification settings - Fork 315
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ import ( | |
"sort" | ||
"strconv" | ||
"strings" | ||
"sync" | ||
"syscall" | ||
"time" | ||
|
||
|
@@ -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() { | ||
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, "#") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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 | ||
} | ||
|
@@ -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) | ||
} | ||
|
@@ -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 { | ||
|
@@ -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 | ||
} | ||
|
||
|
@@ -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 | ||
} | ||
|
@@ -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 | ||
} | ||
|
@@ -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 { | ||
|
@@ -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 | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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