opencode-fs-plugin 1.6.2 → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -2
- package/SPEC.md +7 -3
- package/package.json +1 -1
- package/skills/filesystem/SKILL.md +34 -4
- package/src/tools/find.ts +64 -6
- package/src/tools/head.ts +15 -6
package/README.md
CHANGED
|
@@ -25,9 +25,9 @@ unit, end-to-end and coverage tests.
|
|
|
25
25
|
| `sha256sum`/`md5sum` | `fs.hash` | `algorithm`, `tag`; hashes files or stdin |
|
|
26
26
|
| `du` | `fs.du` | `summarize`, `all`, `human`, `total`, `maxDepth`, `follow`, `oneFileSystem` |
|
|
27
27
|
| `df` | `fs.df` | `human`, `inodes`, `printType`, `all` |
|
|
28
|
-
| `find` | `fs.find` | `name`, `iname`, `pathPattern`, `lname`, `ilname`, `regex`, `iregex`, `type`, `size`, `mtime`, `mmin`, `maxDepth`, `minDepth`, `empty`, `perm`, `uid`, `gid`, `user`, `group`, `links`, `inum`, `samefile`, `fstype`, `newer`, `newerXY`, `printf`, `follow`, `print0`, `sort` |
|
|
28
|
+
| `find` | `fs.find` | `name`, `iname`, `pathPattern`, `lname`, `ilname`, `regex`, `iregex`, `type`, `size`, `mtime`, `mmin`, `maxDepth`, `minDepth`, `empty`, `perm`, `uid`, `gid`, `user`, `group`, `links`, `inum`, `samefile`, `fstype`, `newer`, `newerXY`, `printf`, `follow`, `print0`, `sort`, `action` (`print`/`delete`/`trash`), `recursive` |
|
|
29
29
|
| `cat` | `fs.cat` | `number`, `numberNonBlank`, `squeezeBlank`, `showEnds`, `showTabs`, `showNonPrinting` |
|
|
30
|
-
| `head` | `fs.head` | `lines`, `bytes`, `quiet`, `verbose` |
|
|
30
|
+
| `head` | `fs.head` | `lines`, `skip`, `bytes`, `quiet`, `verbose` |
|
|
31
31
|
| `tail` | `fs.tail` | `lines`, `bytes`, `fromStart`, `quiet`, `verbose`, `follow`, `timeoutMs`, `intervalMs`, `maxBytes` |
|
|
32
32
|
| `tee` | `fs.tee` | `append`, `content` |
|
|
33
33
|
| `wc` | `fs.wc` | `lines`, `words`, `bytes`, `chars`, `maxLineLength` |
|
|
@@ -74,6 +74,10 @@ unit, end-to-end and coverage tests.
|
|
|
74
74
|
| HTTP(S) | `fs.http_request` | `url`, `method`, `headers`, `body`, `json`, `output`, `checksum`, `maxBytes`, `timeoutMs`, `failOnError`, `followRedirects`, `maxRedirects` |
|
|
75
75
|
| shell pipe | `fs.pipe` | `stages`, `input`, `output`, `append` |
|
|
76
76
|
|
|
77
|
+
`fs.head.skip` selects line ranges (`sed -n '20,40p'`). `fs.find` can
|
|
78
|
+
`action: "delete"` or `"trash"` its matches; the starting directory itself is
|
|
79
|
+
never removed, and the `fs_rm`/`fs_trash` permissions are asserted.
|
|
80
|
+
|
|
77
81
|
Canonical field names are listed above. Aliases are accepted only where noted:
|
|
78
82
|
`path` for one `paths` element (`stat`, `ls`, `cat`, `find`, `tee`, `grep`,
|
|
79
83
|
`sed`), `source` for `sources` (`cp`, `mv`, `ln`), `expression` for
|
package/SPEC.md
CHANGED
|
@@ -83,8 +83,9 @@ Aliases are accepted only where noted: `path` for one `paths` element (`stat`,
|
|
|
83
83
|
These are not implemented:
|
|
84
84
|
|
|
85
85
|
- `xz`, `zstd`, `7z`, `rar`
|
|
86
|
-
- `awk`, full `sed` command language, `find -exec`
|
|
87
|
-
|
|
86
|
+
- `awk`, full `sed` command language, arbitrary `find -exec`; `find action` covers
|
|
87
|
+
delete/trash only
|
|
88
|
+
- `chgrp`, `chattr`, `rsync`
|
|
88
89
|
- ACL and xattr preservation in `cp -a`
|
|
89
90
|
- remaining GNU edge-flag parity for `ls`, `stat`, `sort`, `find`
|
|
90
91
|
- symlink-race-proof sandboxing
|
|
@@ -101,7 +102,7 @@ These are not implemented:
|
|
|
101
102
|
- ICMP `ping` (raw sockets) and `ip`/netlink configuration
|
|
102
103
|
- `rsync`, `scp`, and other transfer protocols; use the SSH MCP for remote work
|
|
103
104
|
- ACLs, xattrs, and `chattr`/`lsattr`
|
|
104
|
-
- `
|
|
105
|
+
- `jq`/`yq`
|
|
105
106
|
- `7z`, `rar`, `xz`, `zstd`, and `bzip2`; batch 2 may add pure-JS or native deps
|
|
106
107
|
- `fallocate`, `mkfifo`, `mknod`, `flock`, and mount operations
|
|
107
108
|
|
|
@@ -114,6 +115,9 @@ These are not implemented:
|
|
|
114
115
|
- `find -size` accepts `c` (bytes), `w` (2-byte words), `b` (512-byte blocks),
|
|
115
116
|
and binary `K`/`M`/`G`/... suffixes; `-mtime`/`-mmin` accept `+N` (older),
|
|
116
117
|
`-N` (newer), and exact `N`. `newerXY` uses `XY:path`.
|
|
118
|
+
- `head skip` selects a line range: `skip: 19, lines: 21` is `sed -n '20,40p'`.
|
|
119
|
+
- `find action: "delete"|"trash"` reuses the `fs_rm`/`fs_trash` gates, removes
|
|
120
|
+
deepest matches first, and never removes the starting directory itself.
|
|
117
121
|
- `sort -k` accepts field/character ranges with modifiers `bdfghiMnrV`.
|
|
118
122
|
- `tar`/`zip` extraction rejects path traversal and escaping links.
|
|
119
123
|
- `pipe` passes raw bytes and only accepts registered pipeable stages; `tar` and
|
package/package.json
CHANGED
|
@@ -46,7 +46,7 @@ what `fs.stat`, `fs.ls`, `fs.hash`, `fs.diff`, or `fs.cat` can confirm.
|
|
|
46
46
|
| Times and sizes | `fs.touch`, `fs.truncate` |
|
|
47
47
|
| Links and modes | `fs.ln`, `fs.readlink`, `fs.chmod` |
|
|
48
48
|
| Hash, usage | `fs.hash`, `fs.du`, `fs.df` |
|
|
49
|
-
| Predicate search | `fs.find` (`name`, `type`, `size`, `mtime`, `maxDepth`) |
|
|
49
|
+
| Predicate search | `fs.find` (`name`, `type`, `size`, `mtime`, `maxDepth`, `action`) |
|
|
50
50
|
| Archives | `fs.tar` (`create`/`extract`/`list`), `fs.zip`, `fs.unzip` (`extract`/`list`), `fs.cpio`, `fs.ar`, `fs.gzip`, `fs.gunzip` |
|
|
51
51
|
| Compare | `fs.diff` |
|
|
52
52
|
| Temp and trash | `fs.mktemp`, `fs.trash` |
|
|
@@ -67,7 +67,7 @@ Recent parity additions:
|
|
|
67
67
|
|---|---|
|
|
68
68
|
| `fs.sort` | `generalNumeric`, `humanNumeric`, `version`, `dictionary`, `ignoreNonPrinting`, `stable`, `check`, `checkQuiet`, `zeroTerminated`, GNU `keys` ranges |
|
|
69
69
|
| `fs.ls` | `size`, `numericIds`, `noGroup`, `noOwner`, `slash`, `quote`, `versionSort`, `extensionSort`, `fullTime`, `timeStyle`, `hide`, `ignore`, `groupDirectoriesFirst` |
|
|
70
|
-
| `fs.find` | `lname`/`ilname`, `regex`/`iregex`, `uid`/`gid`/`user`/`group`, `links`, `inum`, `samefile`, `fstype`, `newerXY`, `printf
|
|
70
|
+
| `fs.find` | `lname`/`ilname`, `regex`/`iregex`, `uid`/`gid`/`user`/`group`, `links`, `inum`, `samefile`, `fstype`, `newerXY`, `printf`, `action` (`print`/`delete`/`trash`); `c`/`w`/`b` size suffixes |
|
|
71
71
|
| `fs.stat` | `terse`, extended tokens `%f`, `%m`, `%r`, `%w`, `%W`, `%C` |
|
|
72
72
|
| `fs.tail` | bounded `follow`, `timeoutMs`, `intervalMs`, `maxBytes` |
|
|
73
73
|
| `fs.tar`/`fs.zip` | `input`/`member` stream input and pipe sink support |
|
|
@@ -99,20 +99,49 @@ host drops unknown fields, so a name typo runs with defaults.
|
|
|
99
99
|
| `url` + optional `output` | `fs.http_request` |
|
|
100
100
|
| `stages` + optional `input`/`output` | `fs.pipe` |
|
|
101
101
|
|
|
102
|
+
`fs.head` takes `skip` for line ranges: `sed -n '20,40p'` is
|
|
103
|
+
`fs.head({ paths, skip: 19, lines: 21 })`. `fs.find` takes
|
|
104
|
+
`action: "print" | "delete" | "trash"`; the starting directory itself is never
|
|
105
|
+
removed, and deletion asserts `fs_rm`/`fs_trash`.
|
|
106
|
+
|
|
102
107
|
### Common mistakes
|
|
103
108
|
|
|
104
109
|
- `fs.hash` canonical is `files`; `paths` and `path` are accepted aliases.
|
|
105
110
|
- `fs.mkdir` canonical is `path`; `paths` is accepted but cannot be combined.
|
|
106
111
|
- `fs.sed` canonical is `script`/`expressions`; `expression` is accepted. Only
|
|
107
|
-
`s/.../.../flags` is supported; line ranges
|
|
112
|
+
`s/.../.../flags` is supported; line ranges use `fs.head({ skip, lines })` or
|
|
113
|
+
`fs.tail`.
|
|
108
114
|
- `fs.grep` canonical is `maxCount`; `limit` is accepted. Add `recursive: true`
|
|
109
115
|
for a directory.
|
|
116
|
+
- `fs.find` `action: "delete"`/`"trash"` never removes the starting directory
|
|
117
|
+
itself; use `fs.rm` if you mean the root. Deletion asserts `fs_rm`/`fs_trash`,
|
|
118
|
+
and directory matches need `recursive: true` for delete.
|
|
110
119
|
- `fs.cat` and text filters read `paths`; Code Mode has no stdin, so `fs.cat({})`
|
|
111
120
|
fails. Use `fs.pipe` for stdin.
|
|
112
121
|
- There is no `fs.write`; use the built-in `write` or `fs.tee({ paths, content })`.
|
|
113
122
|
- `fs.mktemp` needs a template ending in at least three `X`; use `suffix` for an
|
|
114
123
|
extension.
|
|
115
124
|
|
|
125
|
+
### Shell patterns and their fs equivalents
|
|
126
|
+
|
|
127
|
+
| Shell pattern | Use |
|
|
128
|
+
|---|---|
|
|
129
|
+
| `sed -n '1,80p' f` | `fs.head({ paths: [f], lines: 80 })` |
|
|
130
|
+
| `sed -n '20,40p' f` | `fs.head({ paths: [f], skip: 19, lines: 21 })` |
|
|
131
|
+
| `sed 's/a/b/'` / `sed -i` | `fs.sed({ script, paths, inPlace })` |
|
|
132
|
+
| `head -n N f` / `tail -n N f` | `fs.head` / `fs.tail` |
|
|
133
|
+
| `wc -l f` | `fs.wc({ paths: [f], lines: true })` |
|
|
134
|
+
| `sha256sum f` | `fs.hash({ files: [f] })` |
|
|
135
|
+
| `find ... -exec rm -rf {} +` | `fs.find({ ..., action: "delete", recursive: true })` or `action: "trash"` |
|
|
136
|
+
| `cat > f <<EOF` | built-in `write` or `fs.tee({ paths: [f], content })` |
|
|
137
|
+
| `rg pattern path` | `fs.grep({ pattern, paths })` or the built-in `grep` |
|
|
138
|
+
| `ls -l f` | `fs.ls({ paths: [f], long: true })` |
|
|
139
|
+
|
|
140
|
+
`sed -n` applied to another command's stdout has no fs equivalent; keep that in
|
|
141
|
+
the command pipeline. `fs.find` has no arbitrary `-exec` by design; use
|
|
142
|
+
`action: "delete"` or `"trash"` for the common cleanup case, or list the paths and
|
|
143
|
+
remove them explicitly.
|
|
144
|
+
|
|
116
145
|
## Batch independent calls
|
|
117
146
|
|
|
118
147
|
`execute` is a JS runtime. Do not `await` independent fs calls one at a time.
|
|
@@ -344,7 +373,8 @@ await tools.fs.http_request({
|
|
|
344
373
|
- Linux only; there is no Windows branch.
|
|
345
374
|
- `fs.sed` supports `s<delim>pattern<delim>replacement<delim>flags` with `g`,
|
|
346
375
|
`i` and `p`. It is not a full sed interpreter.
|
|
347
|
-
- `fs.find` has no `-exec`; use `
|
|
376
|
+
- `fs.find` has no arbitrary `-exec`; use `action: "delete"`/`"trash"` for the
|
|
377
|
+
common cleanup case, or `fs.rm`/`fs.trash` separately.
|
|
348
378
|
- No `xz`, `zstd`, `7z`, `rar`, `chattr`, ACLs, xattrs, or `rsync`.
|
|
349
379
|
`chown` and `dd` are available; the SSH MCP covers transfers.
|
|
350
380
|
- `fs.tail` has bounded `follow` only; unbounded `tail -f` is not supported.
|
package/src/tools/find.ts
CHANGED
|
@@ -11,6 +11,8 @@ import { pathsFrom } from "../core/aliases.js";
|
|
|
11
11
|
import { FsError } from "../core/errors.js";
|
|
12
12
|
import { lookupGroup, lookupUser, modeString, num, typeName, walkAll } from "../core/fsutil.js";
|
|
13
13
|
import { textResult, type ToolSpec } from "../core/types.js";
|
|
14
|
+
import { rmTool } from "./rm.js";
|
|
15
|
+
import { trashTool } from "./trash.js";
|
|
14
16
|
|
|
15
17
|
const Schema = Type.Object(
|
|
16
18
|
{
|
|
@@ -49,6 +51,15 @@ const Schema = Type.Object(
|
|
|
49
51
|
follow: Type.Optional(Type.Boolean({ description: "Follow symlinks (`-L`)." })),
|
|
50
52
|
print0: Type.Optional(Type.Boolean({ description: "NUL-separated output (`-print0`)." })),
|
|
51
53
|
sort: Type.Optional(Type.Boolean({ description: "Sort results lexically. Default: true." })),
|
|
54
|
+
action: Type.Optional(
|
|
55
|
+
Type.Union([Type.Literal("print"), Type.Literal("delete"), Type.Literal("trash")], {
|
|
56
|
+
description:
|
|
57
|
+
"What to do with matches. Default `print`. `delete`/`trash` are destructive and assert `fs_rm`/`fs_trash`; the starting directory itself is never removed.",
|
|
58
|
+
}),
|
|
59
|
+
),
|
|
60
|
+
recursive: Type.Optional(
|
|
61
|
+
Type.Boolean({ description: "Delete matched directories recursively (`action: delete` only)." }),
|
|
62
|
+
),
|
|
52
63
|
},
|
|
53
64
|
{ additionalProperties: false },
|
|
54
65
|
);
|
|
@@ -78,6 +89,15 @@ interface Patterns {
|
|
|
78
89
|
fstypeCache: Map<string, string>;
|
|
79
90
|
}
|
|
80
91
|
|
|
92
|
+
interface Match {
|
|
93
|
+
requested: string;
|
|
94
|
+
full: string;
|
|
95
|
+
stats: Awaited<ReturnType<typeof fs.lstat>>;
|
|
96
|
+
root: string;
|
|
97
|
+
depth: number;
|
|
98
|
+
isRoot: boolean;
|
|
99
|
+
}
|
|
100
|
+
|
|
81
101
|
const FS_TYPES = new Map<number, string>([
|
|
82
102
|
[0xef53, "ext4"],
|
|
83
103
|
[0x01021994, "tmpfs"],
|
|
@@ -108,13 +128,21 @@ export const findTool: ToolSpec<Input> = {
|
|
|
108
128
|
"Find entries with Linux-like predicates: `-name`, `-iname`, `-path`, `-lname`, `-ilname`, " +
|
|
109
129
|
"`-regex`, `-iregex`, `-type`, `-size`, `-mtime`, `-mmin`, `-maxdepth`, `-mindepth`, `-empty`, `-perm`, " +
|
|
110
130
|
"`-uid`, `-gid`, `-user`, `-group`, `-links`, `-inum`, `-samefile`, `-fstype`, `-newer`, " +
|
|
111
|
-
"`-newerXY`, `-printf`, `-L`, `-print0`."
|
|
131
|
+
"`-newerXY`, `-printf`, `-L`, `-print0`. `action` can print, delete, or trash matches; " +
|
|
132
|
+
"the starting directory itself is never removed.",
|
|
112
133
|
schema: Schema,
|
|
113
134
|
pipeable: false,
|
|
114
|
-
mutates:
|
|
135
|
+
mutates: true,
|
|
115
136
|
async run(ctx, input) {
|
|
116
137
|
/* v8 ignore next */
|
|
117
138
|
const paths = pathsFrom(input) ?? ["."];
|
|
139
|
+
const action = input.action ?? "print";
|
|
140
|
+
if (action !== "print" && (input.printf !== undefined || input.print0 === true)) {
|
|
141
|
+
throw new FsError("find action delete/trash cannot be combined with printf or print0.", "invalid");
|
|
142
|
+
}
|
|
143
|
+
if (input.recursive === true && action !== "delete") {
|
|
144
|
+
throw new FsError("find recursive requires action: delete.", "invalid");
|
|
145
|
+
}
|
|
118
146
|
const patterns: Patterns = {
|
|
119
147
|
nameMatch: input.name === undefined ? undefined : picomatch(input.name, { dot: true }),
|
|
120
148
|
inameMatch: input.iname === undefined ? undefined : picomatch(input.iname, { dot: true, nocase: true }),
|
|
@@ -141,7 +169,7 @@ export const findTool: ToolSpec<Input> = {
|
|
|
141
169
|
patterns.uid = input.uid ?? await resolveId(input.user, "user");
|
|
142
170
|
patterns.gid = input.gid ?? await resolveId(input.group, "group");
|
|
143
171
|
|
|
144
|
-
const
|
|
172
|
+
const found: Match[] = [];
|
|
145
173
|
for (const requested of paths) {
|
|
146
174
|
const target = await ctx.sandbox.resolveExisting(requested, { allowRoot: true });
|
|
147
175
|
await ctx.permission.assert([{ target, boundary: target.full }]);
|
|
@@ -152,14 +180,14 @@ export const findTool: ToolSpec<Input> = {
|
|
|
152
180
|
/* v8 ignore next */
|
|
153
181
|
if (!rootStats.isDirectory()) {
|
|
154
182
|
if (await matches(target.full, rootStats, input, patterns, 0)) {
|
|
155
|
-
|
|
183
|
+
found.push({ requested, full: target.full, stats: rootStats, root: target.full, depth: 0, isRoot: true });
|
|
156
184
|
}
|
|
157
185
|
continue;
|
|
158
186
|
}
|
|
159
187
|
/* v8 ignore next */
|
|
160
188
|
if (input.maxDepth === 0) {
|
|
161
189
|
if (await matches(target.full, rootStats, input, patterns, 0)) {
|
|
162
|
-
|
|
190
|
+
found.push({ requested, full: target.full, stats: rootStats, root: target.full, depth: 0, isRoot: true });
|
|
163
191
|
}
|
|
164
192
|
continue;
|
|
165
193
|
}
|
|
@@ -175,10 +203,40 @@ export const findTool: ToolSpec<Input> = {
|
|
|
175
203
|
const depth = relative.split(path.sep).length;
|
|
176
204
|
if (input.minDepth !== undefined && depth < input.minDepth) continue;
|
|
177
205
|
if (await matches(entry.path, entry.stats, input, patterns, depth)) {
|
|
178
|
-
|
|
206
|
+
found.push({ requested: entry.path, full: entry.path, stats: entry.stats, root: target.full, depth, isRoot: false });
|
|
179
207
|
}
|
|
180
208
|
}
|
|
181
209
|
}
|
|
210
|
+
|
|
211
|
+
if (action !== "print") {
|
|
212
|
+
const targets = found
|
|
213
|
+
.filter((match) => !(match.isRoot && match.stats.isDirectory()))
|
|
214
|
+
.sort((a, b) => b.depth - a.depth || a.full.localeCompare(b.full));
|
|
215
|
+
const unique = [...new Map(targets.map((match) => [match.full, match])).values()];
|
|
216
|
+
if (unique.length === 0) {
|
|
217
|
+
return textResult("", { found: found.length, action, ...(action === "delete" ? { removed: [] } : { trashed: [] }) });
|
|
218
|
+
}
|
|
219
|
+
const pathsToChange = unique.map((match) => match.full);
|
|
220
|
+
if (action === "delete") {
|
|
221
|
+
const result = await rmTool.run(
|
|
222
|
+
{ ...ctx, permission: ctx.permissionFor("fs_rm") },
|
|
223
|
+
{ paths: pathsToChange, recursive: input.recursive === true, force: true },
|
|
224
|
+
{},
|
|
225
|
+
);
|
|
226
|
+
return textResult("", { found: found.length, action, ...result.metadata });
|
|
227
|
+
}
|
|
228
|
+
const result = await trashTool.run(
|
|
229
|
+
{ ...ctx, permission: ctx.permissionFor("fs_trash") },
|
|
230
|
+
{ paths: pathsToChange },
|
|
231
|
+
{},
|
|
232
|
+
);
|
|
233
|
+
return textResult("", { found: found.length, action, ...result.metadata });
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const results: string[] = [];
|
|
237
|
+
for (const match of found) {
|
|
238
|
+
results.push(await render(match.requested, match.full, match.stats, input, patterns, match.root, match.depth));
|
|
239
|
+
}
|
|
182
240
|
if (input.sort !== false) results.sort((a, b) => a.localeCompare(b));
|
|
183
241
|
if (input.printf !== undefined) {
|
|
184
242
|
return textResult(results.join(""), { found: results.length });
|
package/src/tools/head.ts
CHANGED
|
@@ -12,6 +12,9 @@ const Schema = Type.Object(
|
|
|
12
12
|
{
|
|
13
13
|
paths: Type.Optional(Type.Array(Type.String({ minLength: 1 }), { description: "Files to read. Omit to read stdin." })),
|
|
14
14
|
lines: Type.Optional(Type.Integer({ minimum: 1, description: "Number of lines. Default: 10 (`-n`)." })),
|
|
15
|
+
skip: Type.Optional(
|
|
16
|
+
Type.Integer({ minimum: 0, description: "Skip this many leading lines before output (`sed -n 'a,bp'` ranges)." }),
|
|
17
|
+
),
|
|
15
18
|
bytes: Type.Optional(Type.Integer({ minimum: 1, description: "Number of bytes (`-c`)." })),
|
|
16
19
|
quiet: Type.Optional(Type.Boolean({ description: "Never print file headers (`-q`)." })),
|
|
17
20
|
verbose: Type.Optional(Type.Boolean({ description: "Always print file headers (`-v`)." })),
|
|
@@ -23,7 +26,9 @@ type Input = Static<typeof Schema>;
|
|
|
23
26
|
|
|
24
27
|
export const headTool: ToolSpec<Input> = {
|
|
25
28
|
name: "head",
|
|
26
|
-
description:
|
|
29
|
+
description:
|
|
30
|
+
"Output the first part of files or stdin with Linux `head` semantics (`-n`, `-c`, `-q`, `-v`). " +
|
|
31
|
+
"`skip` selects a line range such as `sed -n '20,40p'`.",
|
|
27
32
|
schema: Schema,
|
|
28
33
|
pipeable: "filter",
|
|
29
34
|
mutates: false,
|
|
@@ -31,7 +36,11 @@ export const headTool: ToolSpec<Input> = {
|
|
|
31
36
|
if (input.lines !== undefined && input.bytes !== undefined) {
|
|
32
37
|
throw new FsError("head accepts either lines or bytes, not both.", "invalid");
|
|
33
38
|
}
|
|
39
|
+
if (input.skip !== undefined && input.bytes !== undefined) {
|
|
40
|
+
throw new FsError("head skip applies to lines, not bytes.", "invalid");
|
|
41
|
+
}
|
|
34
42
|
const count = input.bytes ?? input.lines ?? 10;
|
|
43
|
+
const skip = input.skip ?? 0;
|
|
35
44
|
const paths = input.paths ?? [];
|
|
36
45
|
const targets = [];
|
|
37
46
|
for (const requested of paths) {
|
|
@@ -46,7 +55,7 @@ export const headTool: ToolSpec<Input> = {
|
|
|
46
55
|
if (targets.length === 0) {
|
|
47
56
|
const data = input.bytes !== undefined
|
|
48
57
|
? await collect(await inputStream([], io.stdin), input.bytes)
|
|
49
|
-
: await takeLines(inputStream([], io.stdin), count);
|
|
58
|
+
: await takeLines(inputStream([], io.stdin), count, skip);
|
|
50
59
|
output.push(data);
|
|
51
60
|
} else {
|
|
52
61
|
for (const target of targets) {
|
|
@@ -56,7 +65,7 @@ export const headTool: ToolSpec<Input> = {
|
|
|
56
65
|
output.push(
|
|
57
66
|
input.bytes !== undefined
|
|
58
67
|
? await collect(readFiles([target.full]), input.bytes)
|
|
59
|
-
: await takeLines(readFiles([target.full]), count),
|
|
68
|
+
: await takeLines(readFiles([target.full]), count, skip),
|
|
60
69
|
);
|
|
61
70
|
}
|
|
62
71
|
}
|
|
@@ -64,14 +73,14 @@ export const headTool: ToolSpec<Input> = {
|
|
|
64
73
|
},
|
|
65
74
|
};
|
|
66
75
|
|
|
67
|
-
async function takeLines(stream: AsyncIterable<Buffer>, count: number): Promise<Buffer> {
|
|
76
|
+
async function takeLines(stream: AsyncIterable<Buffer>, count: number, skip = 0): Promise<Buffer> {
|
|
68
77
|
const output: string[] = [];
|
|
78
|
+
let seen = 0;
|
|
69
79
|
let taken = 0;
|
|
70
80
|
for await (const line of lines(stream)) {
|
|
71
|
-
|
|
81
|
+
if (seen++ < skip) continue;
|
|
72
82
|
output.push(line.text + (line.newline ? "\n" : ""));
|
|
73
83
|
taken++;
|
|
74
|
-
/* v8 ignore next */
|
|
75
84
|
if (taken >= count) break;
|
|
76
85
|
}
|
|
77
86
|
return Buffer.from(output.join(""), "utf8");
|