forked from direnv/direnv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_prune.go
57 lines (47 loc) · 1001 Bytes
/
cmd_prune.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
package main
import (
"io/ioutil"
"os"
"path"
"strings"
)
// CmdPrune is `direnv prune`
var CmdPrune = &Cmd{
Name: "prune",
Desc: "removes old allowed files",
Action: actionWithConfig(cmdPruneAction),
}
func cmdPruneAction(env Env, args []string, config *Config) (err error) {
var dir *os.File
var fi os.FileInfo
var dirList []string
var envrc []byte
allowed := config.AllowDir()
if dir, err = os.Open(allowed); err != nil {
return err
}
defer dir.Close()
if dirList, err = dir.Readdirnames(0); err != nil {
return err
}
for _, hash := range dirList {
filename := path.Join(allowed, hash)
if fi, err = os.Stat(filename); err != nil {
return err
}
if !fi.IsDir() {
if envrc, err = ioutil.ReadFile(filename); err != nil {
return err
}
envrcStr := strings.TrimSpace(string(envrc))
// skip old files, w/o path inside
if envrcStr == "" {
continue
}
if !fileExists(envrcStr) {
_ = os.Remove(filename)
}
}
}
return nil
}