Skip to content

Commit

Permalink
Merge pull request #196 from uclaacm/jason/catRedirect
Browse files Browse the repository at this point in the history
feat: cat redirection
  • Loading branch information
bliutech authored Jan 6, 2023
2 parents 04c766b + 51a0b72 commit 7bc62c4
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/components/shared/TerminalCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,14 @@ function executeCat(
currentWorkingDirectory: Directory,
path: string
): TerminalCommandResult {
let resultPath: string | null = null;

if (path.includes('>')) {
const files = path.split('>');
path = files[0].trim();
resultPath = files[1].trim();
}

const result: TerminalCommandResult = {
modifiedFS: null,
modifiedCWD: null,
Expand All @@ -293,6 +301,48 @@ function executeCat(
const fileContent = ((file as File).content ||= '\n');
result.out = [fileContent];
}

if (!resultPath) {
return result;
}
const resultFile = resultPath.startsWith('/')
? fileSystem.getFileSystemObjectFromPath(resultPath)
: currentWorkingDirectory.getFileSystemObjectFromPath(resultPath);

if (resultFile && resultFile.isDirectory) {
result.err = [`bash: '${resultPath}': Is a directory`];
result.out = [];
return result;
}

if (resultFile && !resultFile.isDirectory) {
resultFile.content = result.out[0];
console.log('resultFile.content: ', resultFile.content);
result.modifiedFS = fileSystem;
result.modifiedCWD = currentWorkingDirectory;
result.out = [];
console.log(resultFile);
return result;
}

// Create the file since it does not exist
const touchResult = executeTouch(
fileSystem,
currentWorkingDirectory,
resultPath
);

result.modifiedFS = touchResult.modifiedFS;
result.modifiedCWD = touchResult.modifiedCWD;
const newlyCreatedFile = resultPath.startsWith('/')
? (result.modifiedFS as Directory).getFileSystemObjectFromPath(resultPath)
: (result.modifiedCWD as Directory).getFileSystemObjectFromPath(resultPath);

if (newlyCreatedFile && !newlyCreatedFile.isDirectory) {
newlyCreatedFile.content = result.out[0];
}

result.out = [];
return result;
}

Expand Down

0 comments on commit 7bc62c4

Please sign in to comment.