Merge pull request #17 from Start9Labs/chore/include-options

chore: including options
This commit is contained in:
J M
2023-01-18 09:17:42 -07:00
committed by GitHub
2 changed files with 212 additions and 119 deletions

View File

@@ -1,21 +1,48 @@
import { ok } from "./util.ts";
import * as T from "./types.ts";
/**
export const DEFAULT_OPTIONS: T.BackupOptions = {
delete: true,
force: true,
ignoreExisting: false,
exclude: [],
};
type BackupSet = {
srcPath: string;
srcVolume: string;
dstPath: string;
dstVolume: string;
options?: Partial<T.BackupOptions>;
};
/**
* This utility simplifies the volume backup process.
* ```ts
* export const { createBackup, restoreBackup } = Backups.volumes("main").build();
* ```
*/
*
* Changing the options of the rsync, (ie exludes) use either
* ```ts
* Backups.volumes("main").set_options({exclude: ['bigdata/']}).volumes('excludedVolume').build()
* // or
* Backups.with_options({exclude: ['bigdata/']}).volumes('excludedVolume').build()
* ```
*
* Using the more fine control, using the addSets for more control
* ```ts
* Backups.addSets({
* srcVolume: 'main', srcPath:'smallData/', dstPath: 'main/smallData/', dstVolume: : Backups.BACKUP
* }, {
* srcVolume: 'main', srcPath:'bigData/', dstPath: 'main/bigData/', dstVolume: : Backups.BACKUP, options: {exclude:['bigData/excludeThis']}}
* ).build()
* ```
*/
export class Backups {
static BACKUP = "BACKUP" as const;
public backupSet = [] as {
srcPath: string;
srcVolume: string;
dstPath: string;
dstVolume: string;
}[];
constructor() {
constructor(
private options = DEFAULT_OPTIONS,
private backupSet = [] as BackupSet[],
) {
}
static volumes(...volumeNames: string[]) {
return new Backups().addSets(...volumeNames.map((srcVolume) => ({
@@ -26,24 +53,34 @@ export class Backups {
})));
}
static addSets(
...options: {
srcPath: string;
srcVolume: string;
dstPath: string;
dstVolume: string;
}[]
...options: BackupSet[]
) {
return new Backups().addSets(...options);
}
static with_options(options?: Partial<T.BackupOptions>) {
return new Backups({ ...DEFAULT_OPTIONS, ...options });
}
set_options(options?: Partial<T.BackupOptions>) {
this.options = {
...this.options,
...options,
};
return this;
}
volumes(...volumeNames: string[]) {
return this.addSets(...volumeNames.map((srcVolume) => ({
srcVolume,
srcPath: "./",
dstPath: `./${srcVolume}/`,
dstVolume: Backups.BACKUP,
})));
}
addSets(
...options: {
srcPath: string;
srcVolume: string;
dstPath: string;
dstVolume: string;
}[]
...options: BackupSet[]
) {
options.forEach((x) => this.backupSet.push(x));
options.forEach((x) =>
this.backupSet.push({ ...x, options: { ...this.options, ...x.options } })
);
return this;
}
build() {
@@ -58,10 +95,8 @@ export class Backups {
await effects.runRsync({
...item,
options: {
delete: true,
force: true,
ignoreExisting: false,
exclude: [],
...this.options,
...item.options,
},
}).wait();
}
@@ -77,10 +112,8 @@ export class Backups {
}
await effects.runRsync({
options: {
delete: true,
force: true,
ignoreExisting: false,
exclude: [],
...this.options,
...item.options,
},
srcVolume: item.dstVolume,
dstVolume: item.srcVolume,

238
types.ts
View File

@@ -1,7 +1,10 @@
// deno-lint-ignore no-namespace
export namespace ExpectedExports {
/** Set configuration is called after we have modified and saved the configuration in the embassy ui. Use this to make a file for the docker to read from for configuration. */
export type setConfig = (effects: Effects, input: Config) => Promise<ResultType<SetResult>>;
export type setConfig = (
effects: Effects,
input: Config,
) => Promise<ResultType<SetResult>>;
/** Get configuration returns a shape that describes the format that the embassy ui will generate, and later send to the set config */
export type getConfig = (effects: Effects) => Promise<ResultType<ConfigRes>>;
/** These are how we make sure the our dependency configurations are valid and if not how to fix them. */
@@ -9,17 +12,31 @@ export namespace ExpectedExports {
/** For backing up service data though the embassyOS UI */
export type createBackup = (effects: Effects) => Promise<ResultType<unknown>>;
/** For restoring service data that was previously backed up using the embassyOS UI create backup flow. Backup restores are also triggered via the embassyOS UI, or doing a system restore flow during setup. */
export type restoreBackup = (effects: Effects) => Promise<ResultType<unknown>>;
export type restoreBackup = (
effects: Effects,
) => Promise<ResultType<unknown>>;
/** Properties are used to get values from the docker, like a username + password, what ports we are hosting from */
export type properties = (effects: Effects) => Promise<ResultType<Properties>>;
export type properties = (
effects: Effects,
) => Promise<ResultType<Properties>>;
export type health = {
/** Should be the health check id */
[id: string]: (effects: Effects, dateMs: number) => Promise<ResultType<unknown>>;
[id: string]: (
effects: Effects,
dateMs: number,
) => Promise<ResultType<unknown>>;
};
export type migration = (effects: Effects, version: string, ...args: unknown[]) => Promise<ResultType<MigrationRes>>;
export type migration = (
effects: Effects,
version: string,
...args: unknown[]
) => Promise<ResultType<MigrationRes>>;
export type action = {
[id: string]: (effects: Effects, config?: Config) => Promise<ResultType<ActionResult>>;
[id: string]: (
effects: Effects,
config?: Config,
) => Promise<ResultType<ActionResult>>;
};
/**
@@ -32,7 +49,9 @@ export namespace ExpectedExports {
/** Used to reach out from the pure js runtime */
export type Effects = {
/** Usable when not sandboxed */
writeFile(input: { path: string; volumeId: string; toWrite: string }): Promise<void>;
writeFile(
input: { path: string; volumeId: string; toWrite: string },
): Promise<void>;
readFile(input: { volumeId: string; path: string }): Promise<string>;
metadata(input: { volumeId: string; path: string }): Promise<Metadata>;
/** Create a directory. Usable when not sandboxed */
@@ -42,12 +61,18 @@ export type Effects = {
removeFile(input: { volumeId: string; path: string }): Promise<void>;
/** Write a json file into an object. Usable when not sandboxed */
writeJsonFile(input: { volumeId: string; path: string; toWrite: Record<string, unknown> }): Promise<void>;
writeJsonFile(
input: { volumeId: string; path: string; toWrite: Record<string, unknown> },
): Promise<void>;
/** Read a json file into an object */
readJsonFile(input: { volumeId: string; path: string }): Promise<Record<string, unknown>>;
readJsonFile(
input: { volumeId: string; path: string },
): Promise<Record<string, unknown>>;
runCommand(input: { command: string; args?: string[]; timeoutMillis?: number }): Promise<ResultType<string>>;
runCommand(
input: { command: string; args?: string[]; timeoutMillis?: number },
): Promise<ResultType<string>>;
runDaemon(input: { command: string; args?: string[] }): {
wait(): Promise<ResultType<string>>;
term(): Promise<void>;
@@ -77,7 +102,7 @@ export type Effects = {
method?: "GET" | "POST" | "PUT" | "DELETE" | "HEAD" | "PATCH";
headers?: Record<string, string>;
body?: string;
}
},
): Promise<{
method: string;
ok: boolean;
@@ -91,18 +116,25 @@ export type Effects = {
}>;
runRsync(options: {
srcVolume: string,
dstVolume: string,
srcPath: string,
dstPath: string,
srcVolume: string;
dstVolume: string;
srcPath: string;
dstPath: string;
// rsync options: https://linux.die.net/man/1/rsync
options: {
delete: boolean,
force: boolean,
ignoreExisting: boolean,
exclude: string[]
}
}): {id: () => Promise<string>, wait: () => Promise<null>, progress: () => Promise<number>}
options: BackupOptions;
}): {
id: () => Promise<string>;
wait: () => Promise<null>;
progress: () => Promise<number>;
};
};
// rsync options: https://linux.die.net/man/1/rsync
export type BackupOptions = {
delete: boolean;
force: boolean;
ignoreExisting: boolean;
exclude: string[];
};
export type Metadata = {
fileType: string;
@@ -177,8 +209,8 @@ export type Target<T extends string, V> = V & {
export type UniqueBy =
| {
any: UniqueBy[];
}
any: UniqueBy[];
}
| string
| null;
@@ -188,19 +220,19 @@ export type WithNullable<T> = T & {
export type DefaultString =
| string
| {
/** The chars available for the randome generation */
charset?: string;
/** Length that we generate to */
len: number;
};
/** The chars available for the randome generation */
charset?: string;
/** Length that we generate to */
len: number;
};
export type ValueSpecString = // deno-lint-ignore ban-types
(
| {}
| {
pattern: string;
"pattern-description": string;
}
pattern: string;
"pattern-description": string;
}
) & {
copyable?: boolean;
masked?: boolean;
@@ -217,63 +249,71 @@ export type ValueSpecNumber = {
export type ValueSpecBoolean = Record<string, unknown>;
export type ValueSpecAny =
| Tag<"boolean", WithDescription<WithDefault<ValueSpecBoolean, boolean>>>
| Tag<"string", WithDescription<WithNullableDefault<WithNullable<ValueSpecString>, DefaultString>>>
| Tag<"number", WithDescription<WithNullableDefault<WithNullable<ValueSpecNumber>, number>>>
| Tag<
"enum",
WithDescription<
WithDefault<
{
values: readonly string[] | string[];
"value-names": {
[key: string]: string;
};
},
string
>
"string",
WithDescription<
WithNullableDefault<WithNullable<ValueSpecString>, DefaultString>
>
>
| Tag<
"number",
WithDescription<WithNullableDefault<WithNullable<ValueSpecNumber>, number>>
>
| Tag<
"enum",
WithDescription<
WithDefault<
{
values: readonly string[] | string[];
"value-names": {
[key: string]: string;
};
},
string
>
>
>
| Tag<"list", ValueSpecList>
| Tag<"object", WithDescription<WithNullableDefault<ValueSpecObject, Config>>>
| Tag<"union", WithDescription<WithDefault<ValueSpecUnion, string>>>
| Tag<
"pointer",
WithDescription<
| Subtype<
"package",
| Target<
"tor-key",
{
"package-id": string;
interface: string;
}
>
| Target<
"tor-address",
{
"package-id": string;
interface: string;
}
>
| Target<
"lan-address",
{
"package-id": string;
interface: string;
}
>
| Target<
"config",
{
"package-id": string;
selector: string;
multi: boolean;
}
>
>
| Subtype<"system", Record<string, unknown>>
"pointer",
WithDescription<
| Subtype<
"package",
| Target<
"tor-key",
{
"package-id": string;
interface: string;
}
>
| Target<
"tor-address",
{
"package-id": string;
interface: string;
}
>
| Target<
"lan-address",
{
"package-id": string;
interface: string;
}
>
| Target<
"config",
{
"package-id": string;
selector: string;
multi: boolean;
}
>
>
>;
| Subtype<"system", Record<string, unknown>>
>
>;
export type ValueSpecUnion = {
/** What tag for the specification, for tag unions */
tag: {
@@ -297,12 +337,32 @@ export type ValueSpecObject = {
"unique-by"?: UniqueBy;
};
export type ValueSpecList =
| Subtype<"boolean", WithDescription<WithDefault<ListSpec<ValueSpecBoolean>, boolean[]>>>
| Subtype<"string", WithDescription<WithDefault<ListSpec<ValueSpecString>, string[]>>>
| Subtype<"number", WithDescription<WithDefault<ListSpec<ValueSpecNumber>, number[]>>>
| Subtype<"enum", WithDescription<WithDefault<ListSpec<ValueSpecEnum>, string[]>>>
| Subtype<"object", WithDescription<WithNullableDefault<ListSpec<ValueSpecObject>, Record<string, unknown>[]>>>
| Subtype<"union", WithDescription<WithDefault<ListSpec<ValueSpecUnion>, string[]>>>;
| Subtype<
"boolean",
WithDescription<WithDefault<ListSpec<ValueSpecBoolean>, boolean[]>>
>
| Subtype<
"string",
WithDescription<WithDefault<ListSpec<ValueSpecString>, string[]>>
>
| Subtype<
"number",
WithDescription<WithDefault<ListSpec<ValueSpecNumber>, number[]>>
>
| Subtype<
"enum",
WithDescription<WithDefault<ListSpec<ValueSpecEnum>, string[]>>
>
| Subtype<
"object",
WithDescription<
WithNullableDefault<ListSpec<ValueSpecObject>, Record<string, unknown>[]>
>
>
| Subtype<
"union",
WithDescription<WithDefault<ListSpec<ValueSpecUnion>, string[]>>
>;
export type ValueSpecEnum = {
values: string[];
"value-names": { [key: string]: string };
@@ -354,8 +414,8 @@ export type DependsOn = {
export type KnownError =
| { error: string }
| {
"error-code": [number, string] | readonly [number, string];
};
"error-code": [number, string] | readonly [number, string];
};
export type ResultType<T> = KnownError | { result: T };
export type PackagePropertiesV2 = {