| 1 |
# Path and Filepath Functions |
| 2 |
|
| 3 |
While Sprig does not grant access to the filesystem, it does provide functions |
| 4 |
for working with strings that follow file path conventions. |
| 5 |
|
| 6 |
## Paths |
| 7 |
|
| 8 |
Paths separated by the slash character (`/`), processed by the `path` package. |
| 9 |
|
| 10 |
Examples: |
| 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 |
|
| 22 |
Return the last element of a path. |
| 23 |
|
| 24 |
``` |
| 25 |
base "foo/bar/baz" |
| 26 |
``` |
| 27 |
|
| 28 |
The above prints "baz". |
| 29 |
|
| 30 |
### dir |
| 31 |
|
| 32 |
Return the directory, stripping the last part of the path. So `dir "foo/bar/baz"` |
| 33 |
returns `foo/bar`. |
| 34 |
|
| 35 |
### clean |
| 36 |
|
| 37 |
Clean up a path. |
| 38 |
|
| 39 |
``` |
| 40 |
clean "foo/bar/../baz" |
| 41 |
``` |
| 42 |
|
| 43 |
The above resolves the `..` and returns `foo/baz`. |
| 44 |
|
| 45 |
### ext |
| 46 |
|
| 47 |
Return the file extension. |
| 48 |
|
| 49 |
``` |
| 50 |
ext "foo.bar" |
| 51 |
``` |
| 52 |
|
| 53 |
The above returns `.bar`. |
| 54 |
|
| 55 |
### isAbs |
| 56 |
|
| 57 |
To check whether a path is absolute, use `isAbs`. |
| 58 |
|
| 59 |
## Filepaths |
| 60 |
|
| 61 |
Paths separated by the `os.PathSeparator` variable, processed by the `path/filepath` package. |
| 62 |
|
| 63 |
These are the recommended functions to use when parsing paths of local filesystems, usually when dealing with local files, directories, etc. |
| 64 |
|
| 65 |
Examples: |
| 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 |
|
| 75 |
Return the last element of a filepath. |
| 76 |
|
| 77 |
``` |
| 78 |
osBase "/foo/bar/baz" |
| 79 |
osBase "C:\\foo\\bar\\baz" |
| 80 |
``` |
| 81 |
|
| 82 |
The above prints "baz" on Linux and Windows, respectively. |
| 83 |
|
| 84 |
### osDir |
| 85 |
|
| 86 |
Return the directory, stripping the last part of the path. So `osDir "/foo/bar/baz"` |
| 87 |
returns `/foo/bar` on Linux, and `osDir "C:\\foo\\bar\\baz"` |
| 88 |
returns `C:\\foo\\bar` on Windows. |
| 89 |
|
| 90 |
### osClean |
| 91 |
|
| 92 |
Clean up a path. |
| 93 |
|
| 94 |
``` |
| 95 |
osClean "/foo/bar/../baz" |
| 96 |
osClean "C:\\foo\\bar\\..\\baz" |
| 97 |
``` |
| 98 |
|
| 99 |
The above resolves the `..` and returns `foo/baz` on Linux and `C:\\foo\\baz` on Windows. |
| 100 |
|
| 101 |
### osExt |
| 102 |
|
| 103 |
Return the file extension. |
| 104 |
|
| 105 |
``` |
| 106 |
osExt "/foo.bar" |
| 107 |
osExt "C:\\foo.bar" |
| 108 |
``` |
| 109 |
|
| 110 |
The above returns `.bar` on Linux and Windows, respectively. |
| 111 |
|
| 112 |
### osIsAbs |
| 113 |
|
| 114 |
To check whether a file path is absolute, use `osIsAbs`. |