Skip to content

Commit

Permalink
bazelisk.go: add simple .bazeliskrc file support
Browse files Browse the repository at this point in the history
Replace `os.Getenv` with `getEnvOrConfig`, which falls back to parsing
key=value pairs from a `.bazeliskrc` file in the workspace
root. Environment variables take precedence.

Aims to solve #119 as simply as possible.

Closes #127.
  • Loading branch information
zellyn authored and philwo committed Jul 2, 2020
1 parent 7300c32 commit c45bd1d
Showing 1 changed file with 54 additions and 8 deletions.
62 changes: 54 additions & 8 deletions bazelisk.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"sort"
"strconv"
"strings"
"sync"
"syscall"
"time"

Expand All @@ -50,12 +51,57 @@ var (
// BazeliskVersion is filled in via x_defs when building a release.
BazeliskVersion = "development"
DefaultTransport = http.DefaultTransport

fileConfig map[string]string
fileConfigOnce sync.Once
)

func getClient() *http.Client {
return &http.Client{Transport: DefaultTransport}
}

// 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, "#") {
// 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 @@ -84,7 +130,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 @@ -186,7 +232,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 @@ -435,7 +481,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 @@ -519,7 +565,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 @@ -673,7 +719,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 @@ -691,7 +737,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 @@ -793,7 +839,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 @@ -836,7 +882,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

0 comments on commit c45bd1d

Please sign in to comment.