forked from direnv/direnv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
93 lines (78 loc) · 1.89 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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
)
type Config struct {
Env Env
WorkDir string // Current directory
ConfDir string
SelfPath string
BashPath string
RCDir string
}
func LoadConfig(env Env) (config *Config, err error) {
config = &Config{
Env: env,
}
config.ConfDir = env["DIRENV_CONFIG"]
if config.ConfDir == "" {
config.ConfDir = XdgConfigDir(env, "direnv")
}
if config.ConfDir == "" {
err = fmt.Errorf("Couldn't find a configuration directory for direnv")
return
}
var exePath string
if exePath, err = exec.LookPath(os.Args[0]); err != nil {
err = fmt.Errorf("LoadConfig() Lookpath failed: %q", err)
return
}
if exePath, err = filepath.EvalSymlinks(exePath); err != nil {
err = fmt.Errorf("LoadConfig() symlink resolution: %q", err)
return
}
config.SelfPath = exePath
config.BashPath = env["DIRENV_BASH"]
if config.BashPath == "" {
if config.BashPath, err = exec.LookPath("bash"); err != nil {
err = fmt.Errorf("Can't find bash: %q", err)
return
}
}
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:]
}
return
}
func (self *Config) AllowDir() string {
return filepath.Join(self.ConfDir, "allow")
}
func (self *Config) LoadedRC() *RC {
if self.RCDir == "" {
return nil
}
rcPath := filepath.Join(self.RCDir, ".envrc")
mtime, err := strconv.ParseInt(self.Env["DIRENV_MTIME"], 10, 64)
if err != nil {
return nil
}
return RCFromEnv(rcPath, mtime)
}
func (self *Config) FindRC() *RC {
return FindRC(self.WorkDir, self.AllowDir())
}
func (self *Config) EnvBackup() (Env, error) {
if self.Env["DIRENV_BACKUP"] == "" {
return nil, fmt.Errorf("DIRENV_BACKUP is empty")
}
return ParseEnv(self.Env["DIRENV_BACKUP"])
}