2017-06-24 23:52:53 +08:00
|
|
|
package eval
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2017-12-23 06:44:32 +08:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2017-06-24 23:52:53 +08:00
|
|
|
// Chdir changes the current directory. On success it also updates the PWD
|
|
|
|
// environment variable and records the new directory in the directory history.
|
2018-03-22 04:51:08 +08:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2017-06-24 23:52:53 +08:00
|
|
|
err := os.Chdir(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-22 04:51:08 +08:00
|
|
|
|
|
|
|
for _, hook := range ev.afterChdir {
|
|
|
|
hook(path)
|
|
|
|
}
|
|
|
|
|
2017-06-24 23:52:53 +08:00
|
|
|
pwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
logger.Println("getwd after cd:", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
os.Setenv("PWD", pwd)
|
2018-03-22 04:51:08 +08:00
|
|
|
|
2017-06-24 23:52:53 +08:00
|
|
|
return nil
|
|
|
|
}
|