35 lines
1.0 KiB
TypeScript
35 lines
1.0 KiB
TypeScript
import { Item } from '../fs/Item'
|
|
import type { SimpleStream } from '../utils/SimpleStream'
|
|
import { Program } from './Program'
|
|
|
|
export class Echo extends Program {
|
|
constructor() {
|
|
super()
|
|
}
|
|
|
|
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, workdir: Item, args: string[]): Promise<number> {
|
|
if (args.length < 2) {
|
|
stdout.emit("echo: error: missing path argument\n")
|
|
return 1
|
|
}
|
|
|
|
const item = await Item.open(args[1].startsWith('/')
|
|
? args[1]
|
|
: `${workdir.GetPath()}${workdir.GetPath().endsWith('/') ? '' : '/'}${args[1]}`)
|
|
|
|
if (!(await item.Exists())) {
|
|
stdout.emit(`echo: error: item ${item.GetPath()} doesn't exist.\n`)
|
|
return 2
|
|
}
|
|
|
|
if (item.IsDirectory()) {
|
|
stdout.emit(`echo: error: can't write data to a directory; item ${item.GetPath()} is a directory.\n`)
|
|
return 3
|
|
}
|
|
|
|
await item.Write(args.slice(2).join(' '))
|
|
|
|
return 0
|
|
}
|
|
}
|