elvish/eval/pwd.go

31 lines
611 B
Go
Raw Normal View History

2016-02-21 19:58:20 +08:00
package eval
import (
"os"
"github.com/elves/elvish/eval/types"
"github.com/elves/elvish/eval/vartypes"
)
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 {
2017-12-23 06:44:32 +08:00
store AddDirer
}
2016-02-21 19:58:20 +08:00
var _ vartypes.Variable = PwdVariable{}
2016-02-21 19:58:20 +08:00
func (PwdVariable) Get() types.Value {
2016-02-21 19:58:20 +08:00
pwd, err := os.Getwd()
maybeThrow(err)
2018-01-02 09:34:09 +08:00
return types.String(pwd)
2016-02-21 19:58:20 +08:00
}
func (pwd PwdVariable) Set(v types.Value) error {
2018-01-02 09:34:09 +08:00
path, ok := v.(types.String)
2016-02-21 19:58:20 +08:00
if !ok {
return ErrPathMustBeString
2016-02-21 19:58:20 +08:00
}
return Chdir(string(path), pwd.store)
2016-02-21 19:58:20 +08:00
}