-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:45Drives/houston-common into main
- Loading branch information
Showing
24 changed files
with
1,177 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,51 @@ | ||
# houston-common | ||
Common library for Houston | ||
|
||
## API Documentation | ||
https://45drives.github.io/houston-common/ | ||
|
||
## Result/ResultAsync documentation from neverthrow | ||
https://github.com/supermacro/neverthrow | ||
|
||
## Example | ||
### Running command and processing output | ||
```ts | ||
import { server, Command, unwrap } from "@45drives/houston-common-lib"; | ||
|
||
function findFile(searchPath: string, filename: string): ResultAsync<string[], ProcessError> { | ||
return server | ||
.execute(new Command(["find", searchPath, "-name", filename, "-print0"])) | ||
.map((proc) => proc.getStdout().split("\0")); | ||
} | ||
const findResult = findFile("/home/user", "id_rsa.pub"); // findResult could contain either list of file paths or a ProcessError | ||
|
||
// 1. use findResult through map method | ||
findResult.map((filePaths: string[]) => { | ||
// this callback only runs if findResult was successful | ||
doSomethingWith(filePaths); | ||
}); | ||
|
||
// 2. or use findResult through match | ||
findResult.match( | ||
(filePaths: string[]) => { | ||
// this callback only runs if findResult was successful | ||
doSomethingWith(filePaths); | ||
}, | ||
(err: ProcessError) => { | ||
// this callback only runs if findResult failed | ||
handleError(err); | ||
} | ||
); | ||
|
||
// 3. or if whatever you are doing to the result also returns a Result, use .andThen instead of .map | ||
const rmResult = findResult.andThen((filePaths: string[]) => | ||
// this callback only runs if findResult was successful | ||
server.execute(new Command(["rm", ...filePaths])) | ||
); | ||
|
||
// 4. or if you'd rather not use Result and a) throw ProcessError if the command failed | ||
const foundPathsOrThrow: string[] = await unwrap(findResult); | ||
|
||
// 4. b) fallback to default value if failed, ignoring error: | ||
const foundPathsOrEmpty: string[] = await findResult.unwrapOr([]); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import smbconf from "./smb.conf?raw"; | ||
import zfsconf from "./zfs.json"; | ||
|
||
export { smbconf, zfsconf } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[global] | ||
|
||
|
||
[share] | ||
path = /tank |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * as hl4 from "./hl4" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { okAsync, Result, ResultAsync } from "neverthrow"; | ||
import { EasySetupConfig } from "./types"; | ||
import { ParsingError, ProcessError, SambaConfParser, SambaManagerNet } from "@/index"; | ||
import * as defaultConfigs from "@/defaultconfigs"; | ||
|
||
export interface IEasySetupConfigurator { | ||
|
||
applyConfig(config: EasySetupConfig): ResultAsync<void, ProcessError>; | ||
} | ||
|
||
export class EasySetupConfigurator implements IEasySetupConfigurator { | ||
sambaManager: SambaManagerNet; | ||
|
||
constructor() { | ||
this.sambaManager = new SambaManagerNet(); | ||
} | ||
|
||
applyConfig(config: EasySetupConfig): ResultAsync<void, ProcessError> { | ||
|
||
return this.applyZFSConfig(config) | ||
.andThen(() => this.applySambaConfig(config)); | ||
} | ||
|
||
private applyZFSConfig(config: EasySetupConfig) { | ||
return okAsync({}); | ||
} | ||
|
||
private applySambaConfig(config: EasySetupConfig) { | ||
const globalSambaUpdateResult = this.sambaManager.editGlobal(config.sambaConfig.global); | ||
const shareSamabaResults = config.sambaConfig.shares.map(share => this.sambaManager.addShare(share)); | ||
|
||
return ResultAsync.combine([ | ||
globalSambaUpdateResult, | ||
...shareSamabaResults | ||
]).map(() => { }); | ||
} | ||
|
||
static loadConfig(easyConfigName: keyof typeof defaultConfigs): Result<EasySetupConfig, ParsingError> { | ||
const dc = defaultConfigs[easyConfigName]; | ||
return SambaConfParser() | ||
.apply(dc.smbconf) | ||
.map((sambaConfig) => { | ||
return { | ||
sambaConfig, | ||
zfsConfig: dc.zfsconf | ||
} | ||
}) | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { SambaConfig } from "../samba/types"; | ||
|
||
export type EasySetupConfig = { | ||
sambaConfig: SambaConfig | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from "./samba/manager"; | ||
export * from "./samba/types"; | ||
export * from "./samba/parser"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { ParsingError, ProcessError } from "@/errors"; | ||
import { SambaGlobalConfig, SambaShareConfig } from "@/typedoc.index"; | ||
import { errAsync, ResultAsync } from "neverthrow"; | ||
import { SambaManagerBase } from "./manager"; | ||
import { suite, test, expect } from "vitest"; | ||
|
||
class SambaManagerTest extends SambaManagerBase { | ||
addShare(_: SambaShareConfig): ResultAsync<SambaShareConfig, ParsingError | ProcessError> { | ||
return errAsync(new Error()); | ||
} | ||
|
||
editGlobal( | ||
_: SambaGlobalConfig | ||
): ResultAsync<SambaGlobalConfig, ParsingError | ProcessError> { | ||
return errAsync(new Error()); | ||
} | ||
|
||
editShare(_: SambaShareConfig): ResultAsync<SambaShareConfig, ParsingError | ProcessError> { | ||
return errAsync(new Error()); | ||
} | ||
|
||
exportConfig(): ResultAsync<string, ProcessError> { | ||
return errAsync(new Error()); | ||
} | ||
|
||
getGlobalConfig(): ResultAsync<SambaGlobalConfig, ParsingError | ProcessError> { | ||
return errAsync(new Error()); | ||
} | ||
|
||
getShare(_: string): ResultAsync<SambaShareConfig, ParsingError | ProcessError> { | ||
return errAsync(new Error()); | ||
} | ||
|
||
getShares(): ResultAsync<SambaShareConfig[], ParsingError | ProcessError> { | ||
return errAsync(new Error()); | ||
} | ||
|
||
importConfig(_: string): ResultAsync<this, ProcessError> { | ||
return errAsync(new Error()); | ||
} | ||
|
||
listShareNames(): ResultAsync<string[], ParsingError | ProcessError> { | ||
return errAsync(new Error()); | ||
} | ||
|
||
removeShare(_: SambaShareConfig): ResultAsync<SambaShareConfig, ParsingError | ProcessError> { | ||
return errAsync(new Error()); | ||
} | ||
} | ||
|
||
suite("SambaManager", () => { | ||
const mgr = new SambaManagerTest(); | ||
test("recommended defaults from conf file", () => { | ||
expect(mgr.recommendedGlobalDefaults().isOk()).toBe(true); | ||
expect(mgr.recommendedShareDefaults().isOk()).toBe(true); | ||
}); | ||
}); |
Oops, something went wrong.