github.com/elves/elvish@v0.15.0/pkg/eval/mods/path/path.go (about)

     1  // Package path provides functions for manipulating filesystem path names.
     2  package path
     3  
     4  import (
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/elves/elvish/pkg/eval"
     9  )
    10  
    11  // Ns is the namespace for the re: module.
    12  var Ns = eval.NsBuilder{}.AddGoFns("path:", fns).Ns()
    13  
    14  var fns = map[string]interface{}{
    15  	"abs":           filepath.Abs,
    16  	"base":          filepath.Base,
    17  	"clean":         filepath.Clean,
    18  	"dir":           filepath.Dir,
    19  	"ext":           filepath.Ext,
    20  	"eval-symlinks": filepath.EvalSymlinks,
    21  	"is-abs":        filepath.IsAbs,
    22  	"is-dir":        isDir,
    23  	"is-regular":    isRegular,
    24  }
    25  
    26  //elvdoc:fn abs
    27  //
    28  // ```elvish
    29  // path:abs $path
    30  // ```
    31  //
    32  // Outputs `$path` converted to an absolute path.
    33  //
    34  // ```elvish-transcript
    35  // ~> cd ~
    36  // ~> path:abs bin
    37  // ▶ /home/user/bin
    38  // ```
    39  
    40  //elvdoc:fn base
    41  //
    42  // ```elvish
    43  // path:base $path
    44  // ```
    45  //
    46  // Outputs the last element of `$path`. This is analogous to the POSIX `basename` command. See the
    47  // [Go documentation](https://pkg.go.dev/path/filepath#Base) for more details.
    48  //
    49  // ```elvish-transcript
    50  // ~> path:base ~/bin
    51  // ▶ bin
    52  // ```
    53  
    54  //elvdoc:fn clean
    55  //
    56  // ```elvish
    57  // path:clean $path
    58  // ```
    59  //
    60  // Outputs the shortest version of `$path` equivalent to `$path` by purely lexical processing. This
    61  // is most useful for eliminating unnecessary relative path elements such as `.` and `..` without
    62  // asking the OS to evaluate the path name. See the [Go
    63  // documentation](https://pkg.go.dev/path/filepath#Clean) for more details.
    64  //
    65  // ```elvish-transcript
    66  // ~> path:clean ./../bin
    67  // ▶ ../bin
    68  // ```
    69  
    70  //elvdoc:fn dir
    71  //
    72  // ```elvish
    73  // path:dir $path
    74  // ```
    75  //
    76  // Outputs all but the last element of `$path`, typically the path's enclosing directory. See the
    77  // [Go documentation](https://pkg.go.dev/path/filepath#Dir) for more details. This is analogous to
    78  // the POSIX `dirname` command.
    79  //
    80  // ```elvish-transcript
    81  // ~> path:dir /a/b/c/something
    82  // ▶ /a/b/c
    83  // ```
    84  
    85  //elvdoc:fn ext
    86  //
    87  // ```elvish
    88  // ext $path
    89  // ```
    90  //
    91  // Outputs the file name extension used by `$path` (including the separating period). If there is no
    92  // extension the empty string is output. See the [Go
    93  // documentation](https://pkg.go.dev/path/filepath#Ext) for more details.
    94  //
    95  // ```elvish-transcript
    96  // ~> path:ext hello.elv
    97  // ▶ .elv
    98  // ```
    99  
   100  //elvdoc:fn is-abs
   101  //
   102  // ```elvish
   103  // is-abs $path
   104  // ```
   105  //
   106  // Outputs `$true` if the path is an abolute path. Note that platforms like Windows have different
   107  // rules than UNIX like platforms for what constitutes an absolute path. See the [Go
   108  // documentation](https://pkg.go.dev/path/filepath#IsAbs) for more details.
   109  //
   110  // ```elvish-transcript
   111  // ~> path:is-abs hello.elv
   112  // ▶ false
   113  // ~> path:is-abs /hello.elv
   114  // ▶ true
   115  // ```
   116  
   117  //elvdoc:fn eval-symlinks
   118  //
   119  // ```elvish-transcript
   120  // ~> mkdir bin
   121  // ~> ln -s bin sbin
   122  // ~> path:eval-symlinks ./sbin/a_command
   123  // ▶ bin/a_command
   124  // ```
   125  //
   126  // Outputs `$path` after resolving any symbolic links. If `$path` is relative the result will be
   127  // relative to the current directory, unless one of the components is an absolute symbolic link.
   128  // This function calls `path:clean` on the result before outputing it. This is analogous to the
   129  // external `realpath` or `readlink` command found on many systems. See the [Go
   130  // documentation](https://pkg.go.dev/path/filepath#EvalSymlinks) for more details.
   131  
   132  //elvdoc:fn is-dir
   133  //
   134  // ```elvish
   135  // is-dir $path
   136  // ```
   137  //
   138  // Outputs `$true` if the path resolves to a directory. If the final element of the path is a
   139  // symlink, even if it points to a directory, it still outputs `$false` since a symlink is not a
   140  // directory. Use [`eval-symlinks`](#patheval-symlinks) on the path first if you don't care if the
   141  // final element is a symlink.
   142  //
   143  // ```elvish-transcript
   144  // ~> touch not-a-dir
   145  // ~> path:is-dir not-a-dir
   146  // ▶ false
   147  // ~> path:is-dir /tmp
   148  // ▶ true
   149  // ```
   150  
   151  func isDir(path string) bool {
   152  	fi, err := os.Stat(path)
   153  	return err == nil && fi.Mode().IsDir()
   154  }
   155  
   156  //elvdoc:fn is-regular
   157  //
   158  // ```elvish
   159  // is-regular $path
   160  // ```
   161  //
   162  // Outputs `$true` if the path resolves to a regular file. If the final element of the path is a
   163  // symlink, even if it points to a regular file, it still outputs `$false` since a symlink is not a
   164  // regular file. Use [`eval-symlinks`](#patheval-symlinks) on the path first if you don't care if
   165  // the final element is a symlink.
   166  //
   167  // ```elvish-transcript
   168  // ~> touch not-a-dir
   169  // ~> path:is-regular not-a-dir
   170  // ▶ true
   171  // ~> path:is-dir /tmp
   172  // ▶ false
   173  // ```
   174  
   175  func isRegular(path string) bool {
   176  	fi, err := os.Stat(path)
   177  	return err == nil && fi.Mode().IsRegular()
   178  }