elvish/eval/pwd.go

31 lines
553 B
Go
Raw Normal View History

2016-02-21 19:58:20 +08:00
package eval
import (
"os"
"github.com/elves/elvish/daemon/api"
)
2016-02-21 19:58:20 +08:00
// PwdVariable is a variable whose value always reflects the current working
// directory. Setting it changes the current working directory.
type PwdVariable struct {
daemon *api.Client
}
2016-02-21 19:58:20 +08:00
var _ Variable = PwdVariable{}
func (PwdVariable) Get() Value {
pwd, err := os.Getwd()
maybeThrow(err)
return String(pwd)
}
func (pwd PwdVariable) Set(v Value) {
2016-02-21 19:58:20 +08:00
path, ok := v.(String)
if !ok {
throw(ErrPathMustBeString)
}
err := Chdir(string(path), pwd.daemon)
2016-02-21 19:58:20 +08:00
maybeThrow(err)
}