From 160e1456d36951b1dabdda0a5d5e7b7255f142d9 Mon Sep 17 00:00:00 2001 From: Qi Xiao Date: Sun, 21 Feb 2016 12:58:20 +0100 Subject: [PATCH] Add $pwd. --- eval/eval.go | 1 + eval/pwd.go | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 eval/pwd.go diff --git a/eval/eval.go b/eval/eval.go index 4e8c0153..03603b83 100644 --- a/eval/eval.go +++ b/eval/eval.go @@ -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) diff --git a/eval/pwd.go b/eval/pwd.go new file mode 100644 index 00000000..5ff03e6a --- /dev/null +++ b/eval/pwd.go @@ -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) +}