core/source/app/cli.ts

327 lines
9 KiB
TypeScript
Raw Normal View History

2024-07-02 15:02:35 +02:00
/*
Copyright 2016-2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
<info@greenscale.de>
»heimdall« is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
»heimdall« 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with »heimdall«. If not, see <http://www.gnu.org/licenses/>.
*/
2023-08-03 08:34:33 +02:00
/**
*/
async function main(
) : Promise<void>
{
await _heimdall.init();
// consts
const version : string = "0.9";
// args
const arg_handler : lib_plankton.args.class_handler = new lib_plankton.args.class_handler(
{
"order_path": new lib_plankton.args.class_argument(
{
"name": "order_path",
"type": lib_plankton.args.enum_type.string,
"kind": lib_plankton.args.enum_kind.positional,
"mode": lib_plankton.args.enum_mode.replace,
"default": "monitoring.hmdl.json",
"parameters": {
"index": 0,
},
"info": lib_plankton.translate.get("help.args.order_path"),
}
),
"show_help": new lib_plankton.args.class_argument(
{
"name": "help",
"type": lib_plankton.args.enum_type.boolean,
"kind": lib_plankton.args.enum_kind.volatile,
"mode": lib_plankton.args.enum_mode.replace,
"default": false,
"parameters": {
"indicators_long": ["help"],
"indicators_short": ["h"],
},
"info": lib_plankton.translate.get("help.args.show_help"),
}
),
"show_version": new lib_plankton.args.class_argument(
{
"name": "version",
"type": lib_plankton.args.enum_type.boolean,
"kind": lib_plankton.args.enum_kind.volatile,
"mode": lib_plankton.args.enum_mode.replace,
"default": false,
"parameters": {
"indicators_long": ["version"],
"indicators_short": ["r"],
},
"info": lib_plankton.translate.get("help.args.show_version"),
}
),
"show_schema": new lib_plankton.args.class_argument(
{
"name": "schema",
"type": lib_plankton.args.enum_type.boolean,
"kind": lib_plankton.args.enum_kind.volatile,
"mode": lib_plankton.args.enum_mode.replace,
"default": false,
"parameters": {
"indicators_long": ["schema"],
"indicators_short": ["s"],
},
"info": lib_plankton.translate.get("help.args.show_schema"),
}
),
"expose_full_order": new lib_plankton.args.class_argument(
{
"name": "expose_full_order",
"type": lib_plankton.args.enum_type.boolean,
"kind": lib_plankton.args.enum_kind.volatile,
"mode": lib_plankton.args.enum_mode.replace,
"default": false,
"parameters": {
"indicators_long": ["expose-full-order"],
"indicators_short": ["e"],
},
"info": lib_plankton.translate.get("help.args.expose_full_order"),
}
),
"erase_state": new lib_plankton.args.class_argument(
{
"name": "erase_state",
"type": lib_plankton.args.enum_type.boolean,
"kind": lib_plankton.args.enum_kind.volatile,
"mode": lib_plankton.args.enum_mode.replace,
"default": false,
"parameters": {
"indicators_long": ["erase-state"],
"indicators_short": ["x"],
},
"info": lib_plankton.translate.get("help.args.erase_state"),
}
),
"database_path": new lib_plankton.args.class_argument(
{
"name": "database_path",
"type": lib_plankton.args.enum_type.string,
"kind": lib_plankton.args.enum_kind.volatile,
"mode": lib_plankton.args.enum_mode.replace,
"default": null,
"parameters": {
"indicators_long": ["database-path"],
"indicators_short": ["d"],
},
"info": lib_plankton.translate.get("help.args.database_path"),
}
),
"mutex_path": new lib_plankton.args.class_argument(
{
"name": "mutex_path",
"type": lib_plankton.args.enum_type.string,
"kind": lib_plankton.args.enum_kind.volatile,
"mode": lib_plankton.args.enum_mode.replace,
"default": "/tmp/heimdall.lock",
"parameters": {
"indicators_long": ["mutex-path"],
"indicators_short": ["m"],
},
"info": lib_plankton.translate.get("help.args.mutex_path"),
}
),
"send_ok_notifications": new lib_plankton.args.class_argument(
{
"name": "send_ok_notifications",
"type": lib_plankton.args.enum_type.boolean,
"kind": lib_plankton.args.enum_kind.volatile,
"mode": lib_plankton.args.enum_mode.replace,
"default": false,
"parameters": {
"indicators_long": ["send-ok-notifications"],
"indicators_short": ["y"],
},
"info": lib_plankton.translate.get(
"help.args.send_ok_notifications",
{
"condition_name": lib_plankton.translate.get("conditions.ok"),
}
),
}
),
"language": new lib_plankton.args.class_argument(
{
"name": "language",
"type": lib_plankton.args.enum_type.string,
"kind": lib_plankton.args.enum_kind.volatile,
"mode": lib_plankton.args.enum_mode.replace,
"default": null,
"parameters": {
"indicators_long": ["language"],
"indicators_short": ["l"],
},
"info": lib_plankton.translate.get("help.args.language"),
}
),
"time_to_live": new lib_plankton.args.class_argument(
{
"name": "time_to_live",
"type": lib_plankton.args.enum_type.float,
"kind": lib_plankton.args.enum_kind.volatile,
"mode": lib_plankton.args.enum_mode.replace,
"default": (60 * 60 * 24 * 7),
"parameters": {
"indicators_long": ["time-to-live"],
"indicators_short": ["t"],
},
"info": lib_plankton.translate.get("help.args.time_to_live"),
}
),
2023-08-31 16:00:36 +02:00
"working_directory": new lib_plankton.args.class_argument(
{
"name": "working_directory",
"type": lib_plankton.args.enum_type.string,
"kind": lib_plankton.args.enum_kind.volatile,
"mode": lib_plankton.args.enum_mode.replace,
"default": ".",
"parameters": {
"indicators_long": ["working-directory"],
"indicators_short": ["w"],
},
"info": lib_plankton.translate.get("help.args.working_directory"),
}
),
2023-08-03 08:34:33 +02:00
"verbosity": new lib_plankton.args.class_argument(
{
"name": "verbosity",
"type": lib_plankton.args.enum_type.integer,
"kind": lib_plankton.args.enum_kind.volatile,
"mode": lib_plankton.args.enum_mode.replace,
"default": 1,
"parameters": {
"indicators_long": ["verbosity"],
"indicators_short": ["v"],
},
"info": lib_plankton.translate.get("help.args.verbosity"),
}
),
}
);
const args : Record<string, any> = arg_handler.read(
lib_plankton.args.enum_environment.cli,
process.argv.slice(2).join(" ")
);
// setup logging
lib_plankton.log.conf_push(
[
new lib_plankton.log.class_channel_minlevel(
new lib_plankton.log.class_channel_stdout(),
[
lib_plankton.log.enum_level.debug,
lib_plankton.log.enum_level.info,
lib_plankton.log.enum_level.notice,
lib_plankton.log.enum_level.warning,
lib_plankton.log.enum_level.error,
][Math.min(4, args["verbosity"])]
),
]
);
// exec
if (args.show_help) {
process.stdout.write(
arg_handler.generate_help(
{
"programname": "heimdall",
"description": lib_plankton.translate.get("help.title"),
"executable": "heimdall",
}
)
);
}
else {
if (args["show_version"]) {
process.stdout.write(version + "\n");
}
else {
if (args["show_schema"]) {
process.stdout.write(
lib_plankton.json.encode(
_heimdall.order.schema_root(
),
true
)
+
"\n"
)
}
else {
2023-08-31 16:00:36 +02:00
const order_path : string = (
args["order_path"].startsWith("/")
? args["order_path"]
: (args["working_directory"] + "/" + args["order_path"])
);
2023-08-03 08:34:33 +02:00
const nm_path = require("path");
const nm_fs = require("fs");
2023-08-31 16:00:36 +02:00
if (! nm_fs.existsSync(order_path)) {
2023-08-03 08:34:33 +02:00
lib_plankton.log.error(
lib_plankton.translate.get("misc.order_file_not_found"),
{
"path": args["order_path"],
}
);
}
else {
const order : _heimdall.type_order = await _heimdall.order.load(
2023-08-31 16:00:36 +02:00
nm_path.normalize(order_path)
2023-08-03 08:34:33 +02:00
);
if (args["expose_full_order"]) {
process.stdout.write(lib_plankton.json.encode(order, true) + "\n");
}
else {
await _heimdall.master.wrap_mutex(
args["mutex_path"],
() => _heimdall.master.wrap_database(
args["database_path"],
args["order_path"],
async () => {
await _heimdall.master.clean(
{
"time_to_live": args["time_to_live"],
"erase_state": args["erase_state"],
}
);
await _heimdall.master.run(
order,
{
"send_ok_notifications": args["send_ok_notifications"],
}
);
}
)
)
.catch(
() => {process.exit(2);}
);
}
}
}
}
}
}
main();