github.com/xyproto/u-root@v6.0.1-0.20200302025726-5528e0c77a3c+incompatible/cmds/core/elvish/eval/chdir.go (about) 1 package eval 2 3 import ( 4 "os" 5 ) 6 7 // AddDirer wraps the AddDir function. 8 type AddDirer interface { 9 // AddDir adds a directory with the given weight to some storage. 10 AddDir(dir string, weight float64) error 11 } 12 13 // Chdir changes the current directory. On success it also updates the PWD 14 // environment variable and records the new directory in the directory history. 15 // It runs the functions in beforeChdir immediately before changing the 16 // directory, and the functions in afterChdir immediately after (if chdir was 17 // successful). It returns nil as long as the directory changing part succeeds. 18 func (ev *Evaler) Chdir(path string) error { 19 for _, hook := range ev.beforeChdir { 20 hook(path) 21 } 22 23 err := os.Chdir(path) 24 if err != nil { 25 return err 26 } 27 28 for _, hook := range ev.afterChdir { 29 hook(path) 30 } 31 32 pwd, err := os.Getwd() 33 if err != nil { 34 logger.Println("getwd after cd:", err) 35 return nil 36 } 37 os.Setenv("PWD", pwd) 38 39 return nil 40 }