mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-05 03:17:50 +08:00
0f6b4bb737
Related #1062 Resolves #1120
38 lines
795 B
Go
38 lines
795 B
Go
package eval
|
|
|
|
import (
|
|
"os"
|
|
|
|
"github.com/elves/elvish/pkg/eval/vars"
|
|
)
|
|
|
|
// PwdVariable is a variable whose value always reflects the current working
|
|
// directory. Setting it changes the current working directory.
|
|
type PwdVariable struct {
|
|
ev *Evaler
|
|
}
|
|
|
|
var _ vars.Var = PwdVariable{}
|
|
|
|
// Getwd allows for unit test error injection.
|
|
var getwd func() (string, error) = os.Getwd
|
|
|
|
// Get returns the current working directory. It returns "/unknown/pwd" when
|
|
// it cannot be determined.
|
|
func (PwdVariable) Get() interface{} {
|
|
pwd, err := getwd()
|
|
if err != nil {
|
|
return "/unknown/pwd"
|
|
}
|
|
return pwd
|
|
}
|
|
|
|
// Set changes the current working directory.
|
|
func (pwd PwdVariable) Set(v interface{}) error {
|
|
path, ok := v.(string)
|
|
if !ok {
|
|
return ErrPathMustBeString
|
|
}
|
|
return pwd.ev.Chdir(path)
|
|
}
|