43 lines
1.1 KiB
Plaintext
43 lines
1.1 KiB
Plaintext
/** Shared helpers for drive-resident /bin scripts (prepended before each command). */
|
|
function bareStdin(ctx) {
|
|
return typeof ctx.shellStdin === 'string' ? ctx.shellStdin : ''
|
|
}
|
|
|
|
function joinDirFile(dir, name) {
|
|
if (!dir || dir === '.') return name
|
|
const d = dir.endsWith('/') ? dir.slice(0, -1) : dir
|
|
return d + '/' + name
|
|
}
|
|
|
|
async function run(ctx, argv) {
|
|
const names = argv.slice(1).filter((a) => !a.startsWith('-'))
|
|
if (!names.length) {
|
|
ctx.console.error('which: missing argument')
|
|
ctx.exitCode = 1
|
|
return
|
|
}
|
|
const pathEnv = ctx.vfs.env.PATH || '/bin'
|
|
const dirs = pathEnv.split(':').filter(Boolean)
|
|
let miss = 0
|
|
for (const cmd of names) {
|
|
if (cmd.includes('/')) {
|
|
const buf = await ctx.vfs.readFile(cmd)
|
|
if (buf != null) ctx.console.log(ctx.vfs.resolveLogical(cmd))
|
|
else miss++
|
|
continue
|
|
}
|
|
let found = false
|
|
for (const dir of dirs) {
|
|
const p = joinDirFile(dir, cmd)
|
|
const buf = await ctx.drive.get(p, { follow: true })
|
|
if (buf) {
|
|
ctx.console.log(p)
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if (!found) miss++
|
|
}
|
|
if (miss) ctx.exitCode = 1
|
|
}
|