github.com/xyproto/u-root@v6.0.1-0.20200302025726-5528e0c77a3c+incompatible/cmds/core/elvish/eval/pwd.go (about)

     1  package eval
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/u-root/u-root/cmds/core/elvish/eval/vars"
     7  )
     8  
     9  // PwdVariable is a variable whose value always reflects the current working
    10  // directory. Setting it changes the current working directory.
    11  type PwdVariable struct {
    12  	ev *Evaler
    13  }
    14  
    15  var _ vars.Var = PwdVariable{}
    16  
    17  func (PwdVariable) Get() interface{} {
    18  	pwd, err := os.Getwd()
    19  	maybeThrow(err)
    20  	return pwd
    21  }
    22  
    23  func (pwd PwdVariable) Set(v interface{}) error {
    24  	path, ok := v.(string)
    25  	if !ok {
    26  		return ErrPathMustBeString
    27  	}
    28  	return pwd.ev.Chdir(path)
    29  }