chore: add some documentation and somet testing

This commit is contained in:
BluJ
2022-07-18 13:27:51 -06:00
parent cb39f5f3ba
commit 17d4b8048f
7 changed files with 296 additions and 249 deletions

View File

@@ -1,4 +1,3 @@
import { YAML } from "../dependencies.ts";
import { matches } from "../dependencies.ts";
import { ExpectedExports } from "../types.ts";
@@ -8,22 +7,31 @@ const { any, string, dictionary } = matches;
const matchConfig = dictionary([string, any]);
export const getConfig = (spec: ConfigSpec): ExpectedExports.getConfig => async (effects) => {
/**
* Call with the configuration to get a standard getConfig for the expected exports
* Assumption: start9/config.yaml is where the config will be stored
* Throws: Error if there is no file
* Throws: Error if the config.yaml isn't yaml nor config shape
* @param spec
* @returns
*/
export const getConfig = (spec: ConfigSpec): ExpectedExports.getConfig =>
async (effects) => {
const config = await effects
.readFile({
path: "start9/config.yaml",
volumeId: "main",
})
.then((x) => YAML.parse(x))
.then((x) => matchConfig.unsafeCast(x))
.catch((e) => {
effects.info(`Got error ${e} while trying to read the config`);
return undefined;
});
.readFile({
path: "start9/config.yaml",
volumeId: "main",
})
.then((x) => YAML.parse(x))
.then((x) => matchConfig.unsafeCast(x))
.catch((e) => {
effects.info(`Got error ${e} while trying to read the config`);
return undefined;
});
return {
result: {
config,
spec,
},
result: {
config,
spec,
},
};
};
};

View File

@@ -1,38 +1,42 @@
import { YAML } from "../dependencies.ts";
import { exists } from "../exists.ts";
import { ResultType, Properties, ExpectedExports, Effects } from "../types.ts";
import { Effects, ExpectedExports, Properties, ResultType } from "../types.ts";
// deno-lint-ignore no-explicit-any
const asResult = (result: any) => ({ result: result as Properties })
const asResult = (result: any) => ({ result: result as Properties });
const noPropertiesFound: ResultType<Properties> = {
result: {
version: 2,
data: {
"Not Ready": {
type: "string",
value: "Could not find properties. The service might still be starting",
qr: false,
copyable: false,
masked: false,
description: "Fallback Message When Properties could not be found"
}
}
}
} as const
result: {
version: 2,
data: {
"Not Ready": {
type: "string",
value: "Could not find properties. The service might still be starting",
qr: false,
copyable: false,
masked: false,
description: "Fallback Message When Properties could not be found",
},
},
},
} as const;
/**
* Default will pull from a file (start9/stats.yaml) expected to be made on the main volume
* @param effects
* @returns
* Assumption: start9/stats.yaml is created by some process
* Throws: stats.yaml isn't yaml
* @param effects
* @returns
*/
export const properties: ExpectedExports.properties = async (
effects: Effects,
effects: Effects,
) => {
if (await exists(effects, { path: "start9/stats.yaml", volumeId: "main" }) === false) {
return noPropertiesFound;
}
return await effects.readFile({
path: "start9/stats.yaml",
volumeId: "main",
}).then(YAML.parse).then(asResult)
if (
await exists(effects, { path: "start9/stats.yaml", volumeId: "main" }) ===
false
) {
return noPropertiesFound;
}
return await effects.readFile({
path: "start9/stats.yaml",
volumeId: "main",
}).then(YAML.parse).then(asResult);
};

View File

@@ -1,33 +1,40 @@
import { YAML } from "../dependencies.ts";
import { ExpectedExports, Effects, Config, SetResult, DependsOn } from "../types.ts";
import {
Config,
DependsOn,
Effects,
ExpectedExports,
SetResult,
} from "../types.ts";
/**
* Will set the config to the default start9/config.yaml
* @param effects
* Assumption: start9/config.yaml is the location of the configuration
* @param effects
* @param newConfig Config to be written to start9/config.yaml
* @param depends_on This would be the depends on for condition depends_on
* @returns
* @returns
*/
export const setConfig = async (
effects: Effects,
newConfig: Config,
dependsOn: DependsOn = {}
effects: Effects,
newConfig: Config,
dependsOn: DependsOn = {},
) => {
await effects.createDir({
path: "start9",
volumeId: "main",
});
await effects.writeFile({
path: "start9/config.yaml",
toWrite: YAML.stringify(newConfig),
volumeId: "main",
});
await effects.createDir({
path: "start9",
volumeId: "main",
});
await effects.writeFile({
path: "start9/config.yaml",
toWrite: YAML.stringify(newConfig),
volumeId: "main",
});
const result: SetResult = {
signal: "SIGTERM",
"depends-on": dependsOn,
};
return { result, };
const result: SetResult = {
signal: "SIGTERM",
"depends-on": dependsOn,
};
return { result };
};
const _typeConversionCheck: ExpectedExports.setConfig = setConfig
const _typeConversionCheck: ExpectedExports.setConfig = setConfig;