-
Notifications
You must be signed in to change notification settings - Fork 317
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract config fetching into its own package (#462)
My goal here is to be able to pass my own config implementation when calling RunBazelisk from code without setting environment variables, because they cause repository rule cache invalidations. As a side-effect, it helps towards the TODO in core.go to split functionality into packages.
- Loading branch information
1 parent
a4dca9c
commit d0e93d2
Showing
10 changed files
with
353 additions
and
247 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "go_default_library", | ||
srcs = ["config.go"], | ||
importpath = "github.com/bazelbuild/bazelisk/config", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//ws:go_default_library", | ||
"@com_github_mitchellh_go_homedir//:go_default_library", | ||
], | ||
) |
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 |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package config | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/bazelbuild/bazelisk/ws" | ||
) | ||
|
||
const rcFileName = ".bazeliskrc" | ||
|
||
// Config allows getting Bazelisk configuration values. | ||
type Config interface { | ||
Get(name string) string | ||
} | ||
|
||
// FromEnv returns a Config which gets config values from environment variables. | ||
func FromEnv() Config { | ||
return &fromEnv{} | ||
} | ||
|
||
type fromEnv struct{} | ||
|
||
func (c *fromEnv) Get(name string) string { | ||
return os.Getenv(name) | ||
} | ||
|
||
// FromFile returns a Config which gets config values from a Bazelisk config file. | ||
func FromFile(path string) (Config, error) { | ||
values, err := parseFileConfig(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &static{ | ||
values: values, | ||
}, nil | ||
} | ||
|
||
type static struct { | ||
values map[string]string | ||
} | ||
|
||
func (c *static) Get(name string) string { | ||
return c.values[name] | ||
} | ||
|
||
// parseFileConfig parses a .bazeliskrc file as a map of key-value configuration values. | ||
func parseFileConfig(rcFilePath string) (map[string]string, error) { | ||
config := make(map[string]string) | ||
|
||
contents, err := ioutil.ReadFile(rcFilePath) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
// Non-critical error. | ||
return config, nil | ||
} | ||
return nil, err | ||
} | ||
|
||
for _, line := range strings.Split(string(contents), "\n") { | ||
if strings.HasPrefix(line, "#") { | ||
// comments | ||
continue | ||
} | ||
parts := strings.SplitN(line, "=", 2) | ||
if len(parts) < 2 { | ||
continue | ||
} | ||
key := strings.TrimSpace(parts[0]) | ||
config[key] = strings.TrimSpace(parts[1]) | ||
} | ||
|
||
return config, nil | ||
} | ||
|
||
// LocateUserConfigFile locates a .bazeliskrc file in the user's home directory. | ||
func LocateUserConfigFile() (string, error) { | ||
home, err := os.UserHomeDir() | ||
if err != nil { | ||
return "", err | ||
} | ||
return filepath.Join(home, rcFileName), nil | ||
} | ||
|
||
// LocateWorkspaceConfigFile locates a .bazeliskrc file in the current workspace root. | ||
func LocateWorkspaceConfigFile() (string, error) { | ||
workingDirectory, err := os.Getwd() | ||
if err != nil { | ||
return "", err | ||
} | ||
workspaceRoot := ws.FindWorkspaceRoot(workingDirectory) | ||
if workspaceRoot == "" { | ||
return "", err | ||
} | ||
return filepath.Join(workspaceRoot, rcFileName), nil | ||
} | ||
|
||
// Layered returns a Config which gets config values from the first of a series of other Config values which sets the config. | ||
func Layered(configs ...Config) Config { | ||
return &layered{ | ||
configs: configs, | ||
} | ||
} | ||
|
||
type layered struct { | ||
configs []Config | ||
} | ||
|
||
func (c *layered) Get(name string) string { | ||
for _, config := range c.configs { | ||
if value := config.Get(name); value != "" { | ||
return value | ||
} | ||
} | ||
return "" | ||
} | ||
|
||
// Null returns a Config with no config values. | ||
func Null() Config { | ||
return &static{} | ||
} | ||
|
||
// Static returns a Config with static values. | ||
func Static(values map[string]string) Config { | ||
return &static{ | ||
values: values, | ||
} | ||
} |
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
Oops, something went wrong.