forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chdir.go
40 lines (33 loc) · 943 Bytes
/
chdir.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
package eval
import (
"os"
)
// AddDirer wraps the AddDir function.
type AddDirer interface {
// AddDir adds a directory with the given weight to some storage.
AddDir(dir string, weight float64) error
}
// Chdir changes the current directory. On success it also updates the PWD
// environment variable and records the new directory in the directory history.
// It runs the functions in beforeChdir immediately before changing the
// directory, and the functions in afterChdir immediately after (if chdir was
// successful). It returns nil as long as the directory changing part succeeds.
func (ev *Evaler) Chdir(path string) error {
for _, hook := range ev.beforeChdir {
hook(path)
}
err := os.Chdir(path)
if err != nil {
return err
}
for _, hook := range ev.afterChdir {
hook(path)
}
pwd, err := os.Getwd()
if err != nil {
logger.Println("getwd after cd:", err)
return nil
}
os.Setenv("PWD", pwd)
return nil
}