elvish/pkg/eval/builtin_fn_fs.go

41 lines
620 B
Go
Raw Normal View History

2017-12-17 13:20:03 +08:00
package eval
import (
"src.elv.sh/pkg/eval/errs"
"src.elv.sh/pkg/fsutil"
2017-12-17 13:20:03 +08:00
)
// Filesystem commands.
2017-12-17 13:20:03 +08:00
func init() {
addBuiltinFns(map[string]any{
2017-12-17 13:20:03 +08:00
// Directory
"cd": cd,
2017-12-17 13:20:03 +08:00
// Path
"tilde-abbr": tildeAbbr,
2017-12-17 13:20:03 +08:00
})
}
func cd(fm *Frame, args ...string) error {
2017-12-17 13:20:03 +08:00
var dir string
switch len(args) {
case 0:
var err error
dir, err = getHome("")
if err != nil {
return err
}
case 1:
dir = args[0]
default:
return errs.ArityMismatch{What: "arguments", ValidLow: 0, ValidHigh: 1, Actual: len(args)}
2017-12-17 13:20:03 +08:00
}
return fm.Evaler.Chdir(dir)
2017-12-17 13:20:03 +08:00
}
func tildeAbbr(path string) string {
return fsutil.TildeAbbr(path)
2017-12-17 13:20:03 +08:00
}