chore: fix path syntax in core programs
This commit is contained in:
@@ -3,21 +3,26 @@ import type { SimpleStream } from '../utils/SimpleStream'
|
||||
import { Program } from './Program'
|
||||
|
||||
export class Cat extends Program {
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, __: Item, args: string[]): Promise<number> {
|
||||
let item: Item = await Item.open(args[1])
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, workdir: Item, args: string[]): Promise<number> {
|
||||
if (args.length < 2) {
|
||||
stdout.emit("cat: error: missing path argument\n")
|
||||
return 1
|
||||
}
|
||||
|
||||
const item = await Item.open(Item.NormalizePath(args[1].startsWith('/') ? args[1] : `${workdir.GetPath()}/${args[1]}`))
|
||||
|
||||
if (!(await item.Exists())) {
|
||||
stdout.emit(`cat: error: item ${item.GetPath()} doesn't exist.\n`)
|
||||
return 1
|
||||
return 2
|
||||
}
|
||||
|
||||
if (item.IsDirectory()) {
|
||||
stdout.emit(`cat: error: can't read data from a directory; item ${item.GetPath()} is a directory.\n`)
|
||||
return 2
|
||||
return 3
|
||||
}
|
||||
|
||||
stdout.emit(`${item.ReadData()}`)
|
||||
stdout.emit('[ EOF ]\n')
|
||||
stdout.emit('<- EOF\n')
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -12,10 +12,14 @@ export class Cd extends Program {
|
||||
}
|
||||
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, workdir: Item, args: string[]): Promise<number> {
|
||||
// open the target directory, default to workdir
|
||||
|
||||
const item = args[1]
|
||||
? await Item.openDir(args[1].startsWith('/')
|
||||
? args[1]
|
||||
: `${workdir.GetPath()}${workdir.GetPath().endsWith('/') ? '' : '/'}${args[1]}`)
|
||||
? await Item.openDir(Item.NormalizePath(
|
||||
args[1].startsWith('/')
|
||||
? args[1]
|
||||
: `${workdir.GetPath()}/${args[1]}`
|
||||
))
|
||||
: workdir
|
||||
|
||||
if (args[1] && !item.IsDirectory()) {
|
||||
|
||||
@@ -8,27 +8,6 @@ export class Echo extends Program {
|
||||
}
|
||||
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, __: 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(' '))
|
||||
|
||||
stdout.emit(args.slice(1).join(' ') + '\n')
|
||||
|
||||
return 0
|
||||
|
||||
@@ -8,10 +8,14 @@ export class Ls extends Program {
|
||||
}
|
||||
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, workdir: Item, args: string[]): Promise<number> {
|
||||
// open the target directory, default to workdir
|
||||
|
||||
const item = args[1]
|
||||
? await Item.openDir(args[1].startsWith('/')
|
||||
? args[1]
|
||||
: `${workdir.GetPath()}${workdir.GetPath().endsWith('/') ? '' : '/'}${args[1]}`)
|
||||
? await Item.openDir(Item.NormalizePath(
|
||||
args[1].startsWith('/')
|
||||
? args[1]
|
||||
: `${workdir.GetPath()}/${args[1]}`
|
||||
))
|
||||
: workdir
|
||||
|
||||
if (args[1] && !item.IsDirectory()) {
|
||||
@@ -25,10 +29,10 @@ export class Ls extends Program {
|
||||
}
|
||||
|
||||
stdout.emit(`-> Listing contents of item: '${item.GetPath()}'\n`)
|
||||
stdout.emit(` [ drwx name ]\n`)
|
||||
stdout.emit(` [ drwx name ]\n`)
|
||||
const items = await item.List()
|
||||
items.forEach(entry => {
|
||||
stdout.emit(` | ${`${(entry.IsDirectory() ? 'd' : '-')}${(entry.IsReadable() ? 'r' : '-')}${(entry.IsWritable() ? 'w' : '-')}${(entry.IsExecutable() ? 'x' : '-')}`.padEnd(8, ' ')} '${entry.GetName()}'\n`)
|
||||
stdout.emit(` | ${`${(entry.IsDirectory() ? 'd' : '-')}${(entry.IsReadable() ? 'r' : '-')}${(entry.IsWritable() ? 'w' : '-')}${(entry.IsExecutable() ? 'x' : '-')}`.padEnd(8, ' ')} ${entry.GetName()}\n`)
|
||||
})
|
||||
|
||||
return 0
|
||||
|
||||
@@ -13,11 +13,8 @@ export class Mkdir extends Program {
|
||||
return 1
|
||||
}
|
||||
|
||||
const item = await Item.openDir(args[1].startsWith('/')
|
||||
? args[1]
|
||||
: `${workdir.GetPath()}${workdir.GetPath().endsWith('/') ? '' : '/'}${args[1]}`)
|
||||
const item = await Item.openDir(Item.NormalizePath(args[1].startsWith('/') ? args[1] : `${workdir.GetPath()}/${args[1]}`))
|
||||
|
||||
stdout.emit(`does ${args[1]} exist? ${await item.Exists()}\n`)
|
||||
|
||||
if (await item.Exists()) {
|
||||
stdout.emit(`mkdir: directory ${item.GetPath()} already exists.\n`)
|
||||
|
||||
@@ -30,7 +30,7 @@ export class Rm extends Program {
|
||||
return 1
|
||||
}
|
||||
|
||||
stdout.emit(`removing: '${item.GetPath()}'\n`)
|
||||
stdout.emit(`-> removing: '${item.GetPath()}'\n`)
|
||||
|
||||
await item.Delete()
|
||||
|
||||
|
||||
@@ -8,11 +8,12 @@ export class Touch extends Program {
|
||||
}
|
||||
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, workdir: Item, args: string[]): Promise<number> {
|
||||
stdout.emit(`touching children, please wait...\n`)
|
||||
if (args.length < 2) {
|
||||
stdout.emit("touch: error: missing path argument\n")
|
||||
return 1
|
||||
}
|
||||
|
||||
const path = args.slice(1).join()
|
||||
const item = await Item.open(path.includes('/') ? path : `${workdir.GetPath()}${workdir.GetPath().endsWith('/') ? '' : '/'}${path}`)
|
||||
stdout.emit(`does ${item.GetPath()} exist? ${await item.Exists()}\n`)
|
||||
const item = await Item.open(Item.NormalizePath(args[1].startsWith('/') ? args[1] : `${workdir.GetPath()}/${args[1]}`))
|
||||
|
||||
if (await item.Exists()) {
|
||||
stdout.emit("touch: the file already exists.\n")
|
||||
|
||||
Reference in New Issue
Block a user