Add $pwd.

This commit is contained in:
Qi Xiao 2016-02-21 12:58:20 +01:00
parent a880b74a1b
commit 160e1456d3
2 changed files with 25 additions and 0 deletions

View File

@ -60,6 +60,7 @@ func NewEvaler(st *store.Store) *Evaler {
"true": NewRoVariable(Bool(true)),
"false": NewRoVariable(Bool(false)),
"paths": &EnvPathList{envName: "PATH"},
"pwd": PwdVariable{},
}
for _, b := range builtinFns {
ev.global[FnPrefix+b.Name] = NewRoVariable(b)

24
eval/pwd.go Normal file
View File

@ -0,0 +1,24 @@
package eval
import "os"
// PwdVariable is a variable whose value always reflects the current working
// directory. Setting it changes the current working directory.
type PwdVariable struct{}
var _ Variable = PwdVariable{}
func (PwdVariable) Get() Value {
pwd, err := os.Getwd()
maybeThrow(err)
return String(pwd)
}
func (PwdVariable) Set(v Value) {
path, ok := v.(String)
if !ok {
throw(ErrPathMustBeString)
}
err := os.Chdir(string(path))
maybeThrow(err)
}