Skip to content

Commit

Permalink
feat: Add ls input option to list files and folders
Browse files Browse the repository at this point in the history
  • Loading branch information
nmaguiar committed Jun 29, 2024
1 parent 332421b commit bfdb298
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/docs/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ List of data input types that can be auto-detected (through the file extension o
| lines | A given string/text to be processed line by line |
| llm | A large language model input (uses 'llmenv' or 'llmoptions') |
| llmmodels | Lists the large language models available (using 'llmenv' or 'llmoptions') |
| ls | Returns a list of files and folders for a given directory path or zip or tar or tgz file |
| md | A Markdown format |
| mdtable | A Markdown table format |
| ndjson | A NDJSON format |
Expand Down Expand Up @@ -235,6 +236,18 @@ List of options to use when _in=lines_:

---

### 🧾 LS input options

List of options to use when _in=ls_:

| Option | Type | Description |
|--------|------|-------------|
| lsext | String | Forces the file format parsing of the provided path or file (between zip, tar, tgz) |
| lsrecursive | Boolean | Will list all files and folders recursively (for folders) |
| lsposix | Boolean | Tries to add extra posix data if available (for ZIP files) |

---

### 🧾 ndJSON input options

List of options to use when _in=ndjson_:
Expand Down
38 changes: 38 additions & 0 deletions src/include/inputFns.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,44 @@ var _inputFns = new Map([
if (isUnDef(res.getModels)) _exit(-1, "OpenAF support for llm model listing API not found.")
_$o(res.getModels(), options)
}],
["ls", (_res, options) => {
_showTmpMsg()
if (isString(_res)) {
var _r
var isPosix = toBoolean(params.lsposix)

if (isDef(params.file)) _res = params.file

var _i = io.fileExists(_res), _f
if (_i) _f = io.fileInfo(_res)
if (_i && _f.isFile) {
var ext = isDef(params.lsext) ? params.lsext :_f.filename.replace(/^.*\./, '').toLowerCase()
switch(ext) {
case "tgz":
case "gz":
_r = io.listFilesTAR(_res, true)
break
case "tar":
_r = io.listFilesTAR(_res)
break
case "jar":
case "zip":
default :
plugin("ZIP")
_r = $m4a((new ZIP()).list(_res))
}
} else {
if (toBoolean(params.lsrecursive)) {
_r = listFilesRecursive(_res, isPosix)
} else {
_r = io.listFiles(_res, isPosix).files
}
}
_$o(_r, options)
} else {
_exit(-1, "ls is only supported with a string.")
}
}],
["toml", (_res, options) => {
_showTmpMsg()
if (isUnDef(af.fromTOML)) _exit(-1, "TOML support not found.")
Expand Down

0 comments on commit bfdb298

Please sign in to comment.