forked from direnv/direnv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
196 lines (166 loc) · 4.95 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
toml "github.com/BurntSushi/toml"
"github.com/direnv/direnv/xdg"
)
// Config represents the direnv configuration and state.
type Config struct {
Env Env
WorkDir string // Current directory
ConfDir string
DataDir string
SelfPath string
BashPath string
RCDir string
TomlPath string
DisableStdin bool
StrictEnv bool
WarnTimeout time.Duration
WhitelistPrefix []string
WhitelistExact map[string]bool
}
type tomlDuration struct {
time.Duration
}
func (d *tomlDuration) UnmarshalText(text []byte) error {
var err error
d.Duration, err = time.ParseDuration(string(text))
return err
}
type tomlConfig struct {
*tomlGlobal // For backward-compatibility
Global *tomlGlobal `toml:"global"`
Whitelist tomlWhitelist `toml:"whitelist"`
}
type tomlGlobal struct {
BashPath string `toml:"bash_path"`
DisableStdin bool `toml:"disable_stdin"`
StrictEnv bool `toml:"strict_env"`
WarnTimeout tomlDuration `toml:"warn_timeout"`
}
type tomlWhitelist struct {
Prefix []string
Exact []string
}
// LoadConfig opens up the direnv configuration from the Env.
func LoadConfig(env Env) (config *Config, err error) {
config = &Config{
Env: env,
}
config.ConfDir = env[DIRENV_CONFIG]
if config.ConfDir == "" {
config.ConfDir = xdg.ConfigDir(env, "direnv")
}
if config.ConfDir == "" {
err = fmt.Errorf("couldn't find a configuration directory for direnv")
return
}
var exePath string
if exePath, err = os.Executable(); err != nil {
err = fmt.Errorf("LoadConfig() os.Executable() failed: %q", err)
return
}
// Fix for mingsys
exePath = strings.Replace(exePath, "\\", "/", -1)
config.SelfPath = exePath
if config.WorkDir, err = os.Getwd(); err != nil {
err = fmt.Errorf("LoadConfig() Getwd failed: %q", err)
return
}
config.RCDir = env[DIRENV_DIR]
if len(config.RCDir) > 0 && config.RCDir[0:1] == "-" {
config.RCDir = config.RCDir[1:]
}
config.WhitelistPrefix = make([]string, 0)
config.WhitelistExact = make(map[string]bool)
// Load the TOML config
config.TomlPath = filepath.Join(config.ConfDir, "direnv.toml")
if _, statErr := os.Stat(config.TomlPath); statErr != nil {
config.TomlPath = filepath.Join(config.ConfDir, "config.toml")
if _, statErr := os.Stat(config.TomlPath); statErr != nil {
config.TomlPath = ""
}
}
if config.TomlPath != "" {
// Declare global once and then share it between the top-level and Global
// keys. The goal here is to let the decoder fill global regardless of if
// the values are in the [global] section or not. The reason we do that is
var global tomlGlobal
tomlConf := tomlConfig{
tomlGlobal: &global,
Global: &global,
}
if _, err = toml.DecodeFile(config.TomlPath, &tomlConf); err != nil {
err = fmt.Errorf("LoadConfig() failed to parse %s: %q", config.TomlPath, err)
return
}
config.WhitelistPrefix = append(config.WhitelistPrefix, tomlConf.Whitelist.Prefix...)
for _, path := range tomlConf.Whitelist.Exact {
if !strings.HasSuffix(path, "/.envrc") {
path = filepath.Join(path, ".envrc")
}
config.WhitelistExact[path] = true
}
config.BashPath = tomlConf.BashPath
config.DisableStdin = tomlConf.DisableStdin
config.StrictEnv = tomlConf.StrictEnv
config.WarnTimeout = tomlConf.WarnTimeout.Duration
}
if config.WarnTimeout == 0 {
timeout, err := time.ParseDuration(env.Fetch("DIRENV_WARN_TIMEOUT", "5s"))
if err != nil {
logError("invalid DIRENV_WARN_TIMEOUT: " + err.Error())
timeout = 5 * time.Second
}
config.WarnTimeout = timeout
}
if config.BashPath == "" {
if env[DIRENV_BASH] != "" {
config.BashPath = env[DIRENV_BASH]
} else if bashPath != "" {
config.BashPath = bashPath
} else if config.BashPath, err = exec.LookPath("bash"); err != nil {
err = fmt.Errorf("can't find bash: %q", err)
return
}
}
if config.DataDir == "" {
config.DataDir = xdg.DataDir(env, "direnv")
}
if config.DataDir == "" {
err = fmt.Errorf("couldn't find a data directory for direnv")
return
}
return
}
// AllowDir is the folder where all the "allow" files are stored.
func (config *Config) AllowDir() string {
return filepath.Join(config.DataDir, "allow")
}
// LoadedRC returns a RC file if any has been loaded
func (config *Config) LoadedRC() *RC {
if config.RCDir == "" {
logDebug("RCDir is blank - loadedRC is nil")
return nil
}
rcPath := filepath.Join(config.RCDir, ".envrc")
timesString := config.Env[DIRENV_WATCHES]
return RCFromEnv(rcPath, timesString, config)
}
// FindRC looks for a RC file in the config environment
func (config *Config) FindRC() (*RC, error) {
return FindRC(config.WorkDir, config)
}
// EnvDiff returns the recorded environment diff that was stored if any.
func (config *Config) EnvDiff() (*EnvDiff, error) {
if config.Env[DIRENV_DIFF] == "" {
return nil, nil
}
return LoadEnvDiff(config.Env[DIRENV_DIFF])
}