forked from u-root/u-root
/
builtin_fn_fs.go
72 lines (58 loc) · 1.33 KB
/
builtin_fn_fs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package eval
import (
"errors"
"os"
"path/filepath"
"github.com/u-root/u-root/cmds/elvish/eval/vals"
"github.com/u-root/u-root/cmds/elvish/util"
)
// Filesystem.
var ErrStoreNotConnected = errors.New("store not connected")
func init() {
addBuiltinFns(map[string]interface{}{
// Directory
"cd": cd,
"dir-history": dirs,
// Path
"path-abs": filepath.Abs,
"path-base": filepath.Base,
"path-clean": filepath.Clean,
"path-dir": filepath.Dir,
"path-ext": filepath.Ext,
"eval-symlinks": filepath.EvalSymlinks,
"tilde-abbr": tildeAbbr,
// File types
"-is-dir": isDir,
})
}
func cd(fm *Frame, args ...string) error {
var dir string
switch len(args) {
case 0:
dir = mustGetHome("")
case 1:
dir = args[0]
default:
return ErrArgs
}
return fm.Chdir(dir)
}
var dirDescriptor = vals.NewStructDescriptor("path", "score")
func newDirStruct(path string, score float64) *vals.Struct {
return vals.NewStruct(dirDescriptor,
[]interface{}{path, vals.FromGo(score)})
}
func dirs(fm *Frame) error {
// out := fm.ports[1].Chan
// for _, dir := range dirs {
// out <- newDirStruct(dir.Path, dir.Score)
// }
return nil
}
func tildeAbbr(path string) string {
return util.TildeAbbr(path)
}
func isDir(path string) bool {
fi, err := os.Stat(path)
return err == nil && fi.Mode().IsDir()
}