2016-02-21 19:58:20 +08:00
|
|
|
package eval
|
|
|
|
|
2017-06-25 00:08:47 +08:00
|
|
|
import (
|
|
|
|
"os"
|
2018-01-01 04:31:45 +08:00
|
|
|
|
|
|
|
"github.com/elves/elvish/eval/types"
|
2018-01-03 09:13:51 +08:00
|
|
|
"github.com/elves/elvish/eval/vartypes"
|
2017-06-25 00:08:47 +08:00
|
|
|
)
|
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.
|
2017-06-25 00:08:47 +08:00
|
|
|
type PwdVariable struct {
|
2017-12-23 06:44:32 +08:00
|
|
|
store AddDirer
|
2017-06-25 00:08:47 +08:00
|
|
|
}
|
2016-02-21 19:58:20 +08:00
|
|
|
|
2018-01-03 09:13:51 +08:00
|
|
|
var _ vartypes.Variable = PwdVariable{}
|
2016-02-21 19:58:20 +08:00
|
|
|
|
2018-01-01 04:31:45 +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
|
|
|
}
|
|
|
|
|
2018-01-03 10:04:14 +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 {
|
2018-01-03 10:04:14 +08:00
|
|
|
return ErrPathMustBeString
|
2016-02-21 19:58:20 +08:00
|
|
|
}
|
2018-01-03 10:04:14 +08:00
|
|
|
return Chdir(string(path), pwd.store)
|
2016-02-21 19:58:20 +08:00
|
|
|
}
|