- cat added
- touch beginnings - path resolver now returns a -1 on error - added a command template file for easier copy-pasting
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import type { CommandDescriptor, CommandOutput } from "@core/command";
|
||||
import type { CommandIO } from "@core/command_io";
|
||||
import { createSuccessResult, type ResultSuccess } from "@core/result";
|
||||
import type { ShellState } from "@core/shell_state";
|
||||
import type { ShellError } from "@core/util/errors";
|
||||
|
||||
export const COMMAND_NAME = "";
|
||||
|
||||
//Need to implement escape sequences
|
||||
async function main(
|
||||
io: CommandIO,
|
||||
state: ShellState,
|
||||
flags: string[],
|
||||
args: string[],
|
||||
): Promise<CommandOutput<ShellError>> {
|
||||
const result: ResultSuccess = createSuccessResult(COMMAND_NAME);
|
||||
|
||||
return {
|
||||
state: state,
|
||||
result,
|
||||
};
|
||||
}
|
||||
|
||||
export const descriptor: CommandDescriptor = {
|
||||
fn: main,
|
||||
name: COMMAND_NAME,
|
||||
};
|
||||
@@ -0,0 +1,72 @@
|
||||
import type { CommandDescriptor, CommandOutput } from "@core/command";
|
||||
import type { CommandIO } from "@core/command_io";
|
||||
import { TreeNodeType, type FileNode, type TreeNode } from "@core/filesystem/treenode";
|
||||
import { createErrorResult, createSuccessResult, type ResultSuccess } from "@core/result";
|
||||
import { getCurrentSession } from "@core/session_manager";
|
||||
import type { ShellState } from "@core/shell_state";
|
||||
import type { PathResolutionError, PermissionDeniedError, ShellError } from "@core/util/errors";
|
||||
import { resolvePath } from "@core/util/path_resolver";
|
||||
|
||||
export const COMMAND_NAME = "cat";
|
||||
|
||||
//Need to implement escape sequences
|
||||
async function main(
|
||||
io: CommandIO,
|
||||
state: ShellState,
|
||||
flags: string[],
|
||||
args: string[],
|
||||
): Promise<CommandOutput<ShellError>> {
|
||||
for (const arg of args) {
|
||||
const inode = resolvePath(state, arg);
|
||||
if (inode === -1) {
|
||||
return {
|
||||
state,
|
||||
result: createErrorResult(
|
||||
"PathResolutionError",
|
||||
"No such file or directory",
|
||||
{ path: arg },
|
||||
COMMAND_NAME,
|
||||
),
|
||||
} satisfies CommandOutput<PathResolutionError>;
|
||||
}
|
||||
|
||||
// Casting to TreeNode because the resolvePath already ensures the inode is correct;
|
||||
const node = state.filesystem.getNode(inode) as TreeNode;
|
||||
|
||||
if (node.type != TreeNodeType.File) {
|
||||
return {
|
||||
state,
|
||||
result: createErrorResult(
|
||||
"PathResolutionError",
|
||||
`${node.name} is a directory`,
|
||||
{ path: arg },
|
||||
COMMAND_NAME,
|
||||
),
|
||||
} satisfies CommandOutput<PathResolutionError>;
|
||||
}
|
||||
|
||||
const session = getCurrentSession(state);
|
||||
|
||||
if (!state.filesystem.canRead(inode, session.user)) {
|
||||
return {
|
||||
state,
|
||||
result: createErrorResult("PermissionDenied", "Permission denied", { resource: arg }, COMMAND_NAME),
|
||||
} satisfies CommandOutput<PermissionDeniedError>;
|
||||
}
|
||||
|
||||
node as FileNode;
|
||||
io.writeStdout(node.content.toArray());
|
||||
}
|
||||
|
||||
const result: ResultSuccess = createSuccessResult(COMMAND_NAME);
|
||||
|
||||
return {
|
||||
state: state,
|
||||
result,
|
||||
};
|
||||
}
|
||||
|
||||
export const descriptor: CommandDescriptor = {
|
||||
fn: main,
|
||||
name: COMMAND_NAME,
|
||||
};
|
||||
+2
-11
@@ -39,8 +39,8 @@ async function main(
|
||||
} else if (args[0] === "-") {
|
||||
inode = session.pwd;
|
||||
} else {
|
||||
const result = safeResolvePath(state, args[0]);
|
||||
if (result.type === "error") {
|
||||
inode = resolvePath(state, args[0]);
|
||||
if (inode === -1) {
|
||||
return {
|
||||
state,
|
||||
result: createErrorResult(
|
||||
@@ -51,7 +51,6 @@ async function main(
|
||||
),
|
||||
} satisfies CommandOutput<PathResolutionError>;
|
||||
}
|
||||
inode = result.inode;
|
||||
}
|
||||
|
||||
if (state.filesystem.isFile(inode)) {
|
||||
@@ -74,14 +73,6 @@ async function main(
|
||||
state: returnState,
|
||||
result: createSuccessResult(COMMAND_NAME),
|
||||
};
|
||||
|
||||
function safeResolvePath(state: ShellState, path: string): { type: "success"; inode: number } | { type: "error" } {
|
||||
try {
|
||||
return { type: "success", inode: resolvePath(state, path) };
|
||||
} catch {
|
||||
return { type: "error" };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const descriptor: CommandDescriptor = {
|
||||
|
||||
@@ -48,10 +48,8 @@ async function main(
|
||||
entries.push({ inode: session.cwd, path: "." });
|
||||
} else {
|
||||
for (const arg of args) {
|
||||
let inode;
|
||||
try {
|
||||
inode = resolvePath(state, arg);
|
||||
} catch {
|
||||
const inode = resolvePath(state, arg);
|
||||
if (inode === -1) {
|
||||
return {
|
||||
state,
|
||||
result: createErrorResult(
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
import type { CommandDescriptor, CommandOutput } from "@core/command";
|
||||
import type { CommandIO } from "@core/command_io";
|
||||
import { TreeNodeType } from "@core/filesystem/treenode";
|
||||
import { createSuccessResult, type ResultSuccess } from "@core/result";
|
||||
import { getCurrentSession } from "@core/session_manager";
|
||||
import type { ShellState } from "@core/shell_state";
|
||||
import type { ShellError } from "@core/util/errors";
|
||||
import { resolvePath } from "@core/util/path_resolver";
|
||||
|
||||
export const COMMAND_NAME = "touch";
|
||||
|
||||
//Need to implement escape sequences
|
||||
async function main(
|
||||
io: CommandIO,
|
||||
state: ShellState,
|
||||
flags: string[],
|
||||
args: string[],
|
||||
): Promise<CommandOutput<ShellError>> {
|
||||
for (const arg of args) {
|
||||
const inode = resolvePath(state, arg);
|
||||
const session = getCurrentSession(state);
|
||||
|
||||
let newState;
|
||||
|
||||
if (inode === -1) {
|
||||
const index = arg.lastIndexOf("/");
|
||||
let parentPath;
|
||||
if (index === -1) {
|
||||
parentPath = "./";
|
||||
} else {
|
||||
parentPath = arg.slice(0, index);
|
||||
}
|
||||
const fileName = arg.slice(index + 1);
|
||||
const parent = resolvePath(state, parentPath);
|
||||
|
||||
newState = state.filesystem.createNode(
|
||||
parent,
|
||||
fileName,
|
||||
TreeNodeType.File,
|
||||
0o644,
|
||||
session.user.uid,
|
||||
session.user.gid,
|
||||
);
|
||||
} else {
|
||||
}
|
||||
}
|
||||
|
||||
const result: ResultSuccess = createSuccessResult(COMMAND_NAME);
|
||||
|
||||
return {
|
||||
state: state,
|
||||
result,
|
||||
};
|
||||
}
|
||||
|
||||
export const descriptor: CommandDescriptor = {
|
||||
fn: main,
|
||||
name: COMMAND_NAME,
|
||||
};
|
||||
Reference in New Issue
Block a user