From 6ca2175bf7e86cdd107c051c9fefb3c8ad13b231 Mon Sep 17 00:00:00 2001 From: joshuaboud Date: Fri, 31 May 2024 11:15:11 -0300 Subject: [PATCH] add filesystemnode type assertions and remove method --- houston-common-lib/lib/path.ts | 79 +++++++++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 7 deletions(-) diff --git a/houston-common-lib/lib/path.ts b/houston-common-lib/lib/path.ts index 71de86f..bd47fea 100644 --- a/houston-common-lib/lib/path.ts +++ b/houston-common-lib/lib/path.ts @@ -77,6 +77,8 @@ export class Ownership { } } +export type ExtendedAttributes = Record; + export class Path { public readonly path: string; @@ -239,7 +241,10 @@ export class Path { commandOptions ) ) - .map((proc) => proc.getStdout().trim().split(RegexSnippets.newlineSplitter)[1]) + .map( + (proc) => + proc.getStdout().trim().split(RegexSnippets.newlineSplitter)[1] + ) .andThen((tokens) => { const [source, mountpoint, realType] = tokens?.split(/\s+/g) ?? []; if ( @@ -255,6 +260,7 @@ export class Path { filesystem: { source, type: parseFileSystemType(realType), + realType, }, mountpoint, }); @@ -383,6 +389,26 @@ export class Path { ) .map(() => this); } + // TODO + // getExtendedAttributesOn( + // server: Server + // ): ResultAsync {} + + // setExtendedAttributesOn( + // server: Server, + // attributes: ExtendedAttributes + // ): ResultAsync {} + + // getExtendedAttributeOn( + // server: Server, + // attributeName: string + // ): ResultAsync {} + + // setExtendedAttributeOn( + // server: Server, + // attributeName: string, + // attributeValue: string + // ): ResultAsync {} } export class FileSystemNode extends Path { @@ -470,12 +496,6 @@ export class FileSystemNode extends Path { ); } - remove(commandOptions?: CommandOptions): ResultAsync { - return this.server - .execute(new Command(["rm", this.path], commandOptions), true) - .map(() => null); - } - getMode( commandOptions?: CommandOptions ): ResultAsync { @@ -501,6 +521,39 @@ export class FileSystemNode extends Path { ): ResultAsync { return this.setOwnershipOn(this.server, ownership, commandOptions); } + + assertExists( + expected: boolean = true, + commandOptions?: CommandOptions | undefined + ): ResultAsync { + return this.exists(commandOptions).andThen((exists) => + exists === expected + ? ok(this) + : err( + new ProcessError(`assertExists(${expected}) failed: ${this.path}`) + ) + ); + } + + assertIsFile( + commandOptions?: CommandOptions | undefined + ): ResultAsync { + return this.isFile(commandOptions).andThen((isFile) => + isFile + ? ok(new File(this)) + : err(new ProcessError(`assertIsFile failed: ${this.path}`)) + ); + } + + assertIsDirectory( + commandOptions?: CommandOptions | undefined + ): ResultAsync { + return this.isDirectory(commandOptions).andThen((isDirectory) => + isDirectory + ? ok(new Directory(this)) + : err(new ProcessError(`assertIsDirectory failed: ${this.path}`)) + ); + } } export class File extends FileSystemNode { @@ -511,6 +564,12 @@ export class File extends FileSystemNode { return this.createOn(this.server, "file", parents, commandOptions); } + remove(commandOptions?: CommandOptions): ResultAsync { + return this.server + .execute(new Command(["rm", this.path], commandOptions), true) + .map(() => null); + } + read( binary?: false, commandOptions?: CommandOptions @@ -560,6 +619,12 @@ export class Directory extends FileSystemNode { return this.createOn(this.server, "directory", parents, commandOptions); } + remove(commandOptions?: CommandOptions): ResultAsync { + return this.server + .execute(new Command(["rmdir", this.path], commandOptions), true) + .map(() => null); + } + getChildren( opts: { /**