...

Text file src/github.com/go-task/slim-sprig/v3/docs/paths.md

Documentation: github.com/go-task/slim-sprig/v3/docs

     1# Path and Filepath Functions
     2
     3While Sprig does not grant access to the filesystem, it does provide functions
     4for working with strings that follow file path conventions.
     5
     6## Paths
     7
     8Paths separated by the slash character (`/`), processed by the `path` package.
     9
    10Examples:
    11
    12- The [Linux](https://en.wikipedia.org/wiki/Linux) and
    13  [MacOS](https://en.wikipedia.org/wiki/MacOS)
    14  [filesystems](https://en.wikipedia.org/wiki/File_system):
    15  `/home/user/file`, `/etc/config`;
    16- The path component of
    17  [URIs](https://en.wikipedia.org/wiki/Uniform_Resource_Identifier):
    18  `https://example.com/some/content/`, `ftp://example.com/file/`.
    19
    20### base
    21
    22Return the last element of a path.
    23
    24```
    25base "foo/bar/baz"
    26```
    27
    28The above prints "baz".
    29
    30### dir
    31
    32Return the directory, stripping the last part of the path. So `dir "foo/bar/baz"`
    33returns `foo/bar`.
    34
    35### clean
    36
    37Clean up a path.
    38
    39```
    40clean "foo/bar/../baz"
    41```
    42
    43The above resolves the `..` and returns `foo/baz`.
    44
    45### ext
    46
    47Return the file extension.
    48
    49```
    50ext "foo.bar"
    51```
    52
    53The above returns `.bar`.
    54
    55### isAbs
    56
    57To check whether a path is absolute, use `isAbs`.
    58
    59## Filepaths
    60
    61Paths separated by the `os.PathSeparator` variable, processed by the `path/filepath` package.
    62
    63These are the recommended functions to use when parsing paths of local filesystems, usually when dealing with local files, directories, etc.
    64
    65Examples:
    66
    67- Running on Linux or MacOS the filesystem path is separated by the slash character (`/`):
    68  `/home/user/file`, `/etc/config`;
    69- Running on [Windows](https://en.wikipedia.org/wiki/Microsoft_Windows)
    70  the filesystem path is separated by the backslash character (`\`):
    71  `C:\Users\Username\`, `C:\Program Files\Application\`;
    72
    73### osBase
    74
    75Return the last element of a filepath.
    76
    77```
    78osBase "/foo/bar/baz"
    79osBase "C:\\foo\\bar\\baz"
    80```
    81
    82The above prints "baz" on Linux and Windows, respectively.
    83
    84### osDir
    85
    86Return the directory, stripping the last part of the path. So `osDir "/foo/bar/baz"`
    87returns `/foo/bar` on Linux, and `osDir "C:\\foo\\bar\\baz"`
    88returns `C:\\foo\\bar` on Windows.
    89
    90### osClean
    91
    92Clean up a path.
    93
    94```
    95osClean "/foo/bar/../baz"
    96osClean "C:\\foo\\bar\\..\\baz"
    97```
    98
    99The above resolves the `..` and returns `foo/baz` on Linux and `C:\\foo\\baz` on Windows.
   100
   101### osExt
   102
   103Return the file extension.
   104
   105```
   106osExt "/foo.bar"
   107osExt "C:\\foo.bar"
   108```
   109
   110The above returns `.bar` on Linux and Windows, respectively.
   111
   112### osIsAbs
   113
   114To check whether a file path is absolute, use `osIsAbs`.

View as plain text