Files
bare-operating-system/packages/bare-os-seeder/kernel/bin/crontab
T
2026-04-03 03:07:51 -04:00

80 lines
1.9 KiB
Plaintext

/** Shared helpers for drive-resident /bin scripts (prepended before each command). */
function bareStdin(ctx) {
return typeof ctx.shellStdin === 'string' ? ctx.shellStdin : ''
}
async function run(ctx, argv) {
const vfs = ctx.vfs
const home = vfs.env?.HOME || '/home/guest'
const h = String(home).replace(/\/$/, '')
const tabPath = `${h}/.crontab`
const rest = argv.slice(1)
if (rest.length === 0) {
ctx.console.error('crontab: usage: crontab -l | crontab -r | crontab <file>')
ctx.exitCode = 1
return
}
if (rest[0] === '-l') {
try {
const b = await vfs.readFile(tabPath)
if (!b || !b.length) {
ctx.console.log('no crontab for user')
return
}
const s = ctx.b4a.toString(b)
ctx.console.log(s.endsWith('\n') ? s : s + '\n')
} catch {
ctx.console.log('no crontab for user')
}
return
}
if (rest[0] === '-r') {
if (ctx.identity?.state !== 'unlocked') {
ctx.console.error('crontab: log in to modify crontab')
ctx.exitCode = 1
return
}
try {
await vfs.unlink(tabPath)
} catch (e) {
const msg = String(e?.message || e)
if (/not found|ENOENT|no entry|missing/i.test(msg)) {
ctx.console.error('crontab: no crontab for user')
} else {
ctx.console.error('crontab: ' + msg)
}
ctx.exitCode = 1
}
return
}
if (rest[0].startsWith('-')) {
ctx.console.error('crontab: unknown option ' + rest[0])
ctx.exitCode = 1
return
}
if (ctx.identity?.state !== 'unlocked') {
ctx.console.error('crontab: log in to install crontab')
ctx.exitCode = 1
return
}
const file = rest[0]
try {
const b = await vfs.readFile(file)
if (b == null) {
ctx.console.error('crontab: ' + file + ': cannot read')
ctx.exitCode = 1
return
}
await vfs.writeFile(tabPath, b)
} catch (e) {
ctx.console.error('crontab: ' + (e.message || e))
ctx.exitCode = 1
}
}