github.com/mem/u-root@v2.0.1-0.20181004165302-9b18b4636a33+incompatible/cmds/elvish/eval/builtin_fn_fs.go (about)

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