munin/source/main.ts
Christian Fraß 3fa5d0820b [task-153]
2025-07-01 17:19:47 +00:00

252 lines
6.6 KiB
TypeScript

/*
This file is part of »munin«.
Copyright 2025 'Fenris Wolf' <fenris@folksprak.org>
»munin« is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
»munin« is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with »munin«. If not, see <http://www.gnu.org/licenses/>.
*/
namespace _munin
{
/**
*/
export async function main(
args_raw : Array<string>
): Promise<void>
{
// init
const language_codes : Array<string> = [
"deu",
"eng",
];
const packages = await Promise.all<lib_plankton.translate.type_package>(
language_codes
.map<Promise<lib_plankton.translate.type_package>>(
code => (
Promise.resolve<string>(code)
.then<string>(code => Promise.resolve(lib_plankton.string.coin("data/localization/{{code}}.json", {"code": code})))
.then<string>(path => lib_plankton.file.read(path))
.then<lib_plankton.translate.type_package>(content => Promise.resolve(lib_plankton.json.decode(content)))
)
)
);
await lib_plankton.translate.initialize(
{
"verbosity": 1,
"packages": packages,
"order": language_codes,
"autopromote": false,
}
);
// args
const arg_handler : lib_plankton.args.class_handler = new lib_plankton.args.class_handler(
{
"action": lib_plankton.args.class_argument.positional({
"index": 0,
"type": lib_plankton.args.enum_type.string,
"mode": lib_plankton.args.enum_mode.replace,
"default": "run",
"info": lib_plankton.string.coin(
"{{core}} : test | run",
{
"core": lib_plankton.translate.get("args.action.description"),
}
),
"name": "action",
}),
"conf_path": lib_plankton.args.class_argument.volatile({
"indicators_long": ["conf-path"],
"indicators_short": ["c"],
"type": lib_plankton.args.enum_type.string,
"mode": lib_plankton.args.enum_mode.replace,
"default": "munin.json",
"info": lib_plankton.translate.get("args.conf_path.description"),
"name": "conf-path",
}),
"conf_schema": lib_plankton.args.class_argument.volatile({
"indicators_long": ["conf-schema"],
"indicators_short": ["s"],
"type": lib_plankton.args.enum_type.boolean,
"mode": lib_plankton.args.enum_mode.replace,
"default": false,
"info": lib_plankton.translate.get("args.conf_schema.description"),
"name": "conf-schema",
}),
"conf_expose": lib_plankton.args.class_argument.volatile({
"indicators_long": ["conf-expose"],
"indicators_short": ["e"],
"type": lib_plankton.args.enum_type.boolean,
"mode": lib_plankton.args.enum_mode.replace,
"default": false,
"info": lib_plankton.translate.get("args.conf_expose.description"),
"name": "conf-expose",
}),
"single_run": lib_plankton.args.class_argument.volatile({
"indicators_long": ["single-run"],
"indicators_short": ["x"],
"type": lib_plankton.args.enum_type.boolean,
"mode": lib_plankton.args.enum_mode.replace,
"default": false,
"info": lib_plankton.translate.get("args.single_run.description"),
"name": "single-run",
}),
"verbosity": lib_plankton.args.class_argument.volatile({
"indicators_long": ["verbosity"],
"indicators_short": ["v"],
"type": lib_plankton.args.enum_type.string,
"mode": lib_plankton.args.enum_mode.replace,
"default": "notice",
"info": "error | warning | notice | info | debug",
"name": "verbosity",
}),
"dry_run": lib_plankton.args.class_argument.volatile({
"indicators_long": ["dry-run"],
"indicators_short": ["q"],
"type": lib_plankton.args.enum_type.boolean,
"mode": lib_plankton.args.enum_mode.replace,
"default": false,
"info": lib_plankton.translate.get("args.dry_run.description"),
"name": "dry-run",
}),
"help": lib_plankton.args.class_argument.volatile({
"indicators_long": ["help"],
"indicators_short": ["h"],
"type": lib_plankton.args.enum_type.boolean,
"mode": lib_plankton.args.enum_mode.replace,
"default": false,
"info": lib_plankton.translate.get("args.help.description"),
"name": "help",
}),
}
);
const args : Record<string, any> = arg_handler.read(
lib_plankton.args.enum_environment.cli,
args_raw.join(" ")
);
if (args.help) {
process.stdout.write(
arg_handler.generate_help(
{
"programname": "munin",
"description": lib_plankton.translate.get("help.description"),
"executable": "node build/munin",
}
)
);
}
else {
if (args.conf_schema) {
process.stdout.write(
lib_plankton.json.encode(
_munin.conf.schema_extended(),
{
"formatted": true,
}
)
+
"\n"
);
}
else {
switch (args.action) {
default:
{
throw (new Error("unhandled action: " + args.action));
break;
}
case "test":
{
_munin.test.all();
break;
}
case "run":
{
// init
const conf : _munin.conf.type_conf = await _munin.conf.load(args.conf_path);
lib_plankton.log.set_main_logger(
[
{
"kind": "filtered",
"data": {
"core": {
"kind": "std",
"data": {
"target": "stdout",
"format": {
"kind": "jsonl",
"data": {
"structured": true
}
}
}
},
"predicate": [
[
{
"item": {
"kind": "level",
"data": {
"threshold": args.verbosity,
}
},
},
]
],
}
},
]
);
// exec
if (args.conf_expose) {
process.stdout.write(
lib_plankton.json.encode(
conf,
{
"formatted": true,
}
)
+
"\n"
);
}
_munin.logic.run(
conf,
{
"single_run": args.single_run,
"dry_run": args.dry_run,
}
);
break;
}
}
}
}
return Promise.resolve<void>(undefined);
}
}
_munin.main(process.argv.slice(2))
.then(() => {})
.catch((reason) => {process.stderr.write(String(reason) + "\n");})
;