forked from SKYHOMEWORK/bazelisk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
129 lines (108 loc) · 2.69 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package config
import (
"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 := os.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,
}
}