Skip to content

Commit

Permalink
add filesystemnode type assertions and remove method
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaboud committed May 31, 2024
1 parent 638e070 commit 6ca2175
Showing 1 changed file with 72 additions and 7 deletions.
79 changes: 72 additions & 7 deletions houston-common-lib/lib/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ export class Ownership {
}
}

export type ExtendedAttributes = Record<string, string>;

export class Path {
public readonly path: string;

Expand Down Expand Up @@ -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 (
Expand All @@ -255,6 +260,7 @@ export class Path {
filesystem: {
source,
type: parseFileSystemType(realType),
realType,
},
mountpoint,
});
Expand Down Expand Up @@ -383,6 +389,26 @@ export class Path {
)
.map(() => this);
}
// TODO
// getExtendedAttributesOn(
// server: Server
// ): ResultAsync<ExtendedAttributes, ProcessError> {}

// setExtendedAttributesOn(
// server: Server,
// attributes: ExtendedAttributes
// ): ResultAsync<Path, ProcessError> {}

// getExtendedAttributeOn(
// server: Server,
// attributeName: string
// ): ResultAsync<string | null, ProcessError> {}

// setExtendedAttributeOn(
// server: Server,
// attributeName: string,
// attributeValue: string
// ): ResultAsync<Path, ProcessError> {}
}

export class FileSystemNode extends Path {
Expand Down Expand Up @@ -470,12 +496,6 @@ export class FileSystemNode extends Path {
);
}

remove(commandOptions?: CommandOptions): ResultAsync<null, ProcessError> {
return this.server
.execute(new Command(["rm", this.path], commandOptions), true)
.map(() => null);
}

getMode(
commandOptions?: CommandOptions
): ResultAsync<Mode, ProcessError | ParsingError> {
Expand All @@ -501,6 +521,39 @@ export class FileSystemNode extends Path {
): ResultAsync<this, ProcessError> {
return this.setOwnershipOn(this.server, ownership, commandOptions);
}

assertExists(
expected: boolean = true,
commandOptions?: CommandOptions | undefined
): ResultAsync<this, ProcessError> {
return this.exists(commandOptions).andThen((exists) =>
exists === expected
? ok(this)
: err(
new ProcessError(`assertExists(${expected}) failed: ${this.path}`)
)
);
}

assertIsFile(
commandOptions?: CommandOptions | undefined
): ResultAsync<File, ProcessError> {
return this.isFile(commandOptions).andThen((isFile) =>
isFile
? ok(new File(this))
: err(new ProcessError(`assertIsFile failed: ${this.path}`))
);
}

assertIsDirectory(
commandOptions?: CommandOptions | undefined
): ResultAsync<Directory, ProcessError> {
return this.isDirectory(commandOptions).andThen((isDirectory) =>
isDirectory
? ok(new Directory(this))
: err(new ProcessError(`assertIsDirectory failed: ${this.path}`))
);
}
}

export class File extends FileSystemNode {
Expand All @@ -511,6 +564,12 @@ export class File extends FileSystemNode {
return this.createOn(this.server, "file", parents, commandOptions);
}

remove(commandOptions?: CommandOptions): ResultAsync<null, ProcessError> {
return this.server
.execute(new Command(["rm", this.path], commandOptions), true)
.map(() => null);
}

read(
binary?: false,
commandOptions?: CommandOptions
Expand Down Expand Up @@ -560,6 +619,12 @@ export class Directory extends FileSystemNode {
return this.createOn(this.server, "directory", parents, commandOptions);
}

remove(commandOptions?: CommandOptions): ResultAsync<null, ProcessError> {
return this.server
.execute(new Command(["rmdir", this.path], commandOptions), true)
.map(() => null);
}

getChildren(
opts: {
/**
Expand Down

0 comments on commit 6ca2175

Please sign in to comment.