backend/source/logic/cli.ts

219 lines
5 KiB
TypeScript
Raw Normal View History

2024-09-08 21:31:17 +02:00
/**
*/
2024-09-09 12:13:10 +02:00
async function main(
args_raw : Array<string>
) : Promise<void>
{
const arg_handler : lib_plankton.args.class_handler = new lib_plankton.args.class_handler({
"data_path": lib_plankton.args.class_argument.volatile({
2024-09-10 01:11:58 +02:00
"indicators_long": ["data-path"],
2024-09-09 12:13:10 +02:00
"indicators_short": ["d"],
"type": lib_plankton.args.enum_type.string,
"mode": lib_plankton.args.enum_mode.replace,
"default": "data.json",
// "info": null,
"name": "data-path",
}),
"timezone_shift": lib_plankton.args.class_argument.volatile({
2024-09-10 01:11:58 +02:00
"indicators_long": ["timezone-shift"],
2024-09-09 12:13:10 +02:00
"indicators_short": ["t"],
2024-09-10 01:11:58 +02:00
"type": lib_plankton.args.enum_type.integer,
2024-09-09 12:13:10 +02:00
"mode": lib_plankton.args.enum_mode.replace,
"default": 0,
// "info": null,
"name": "timezone-shift",
}),
2024-09-10 01:11:58 +02:00
"calendar_id": lib_plankton.args.class_argument.volatile({
"indicators_long": ["calendar"],
"indicators_short": ["c"],
"type": lib_plankton.args.enum_type.integer,
"mode": lib_plankton.args.enum_mode.replace,
"default": 1,
// "info": null,
"name": "calendar_id",
}),
"view_mode": lib_plankton.args.class_argument.volatile({
"indicators_long": ["view-moode"],
"indicators_short": ["m"],
"type": lib_plankton.args.enum_type.string,
"mode": lib_plankton.args.enum_mode.replace,
"default": "table",
// "info": null,
"name": "view_mode",
}),
2024-09-09 12:13:10 +02:00
"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": null,
"name": "help",
}),
});
const args : Record<string, any> = arg_handler.read(lib_plankton.args.enum_environment.cli, args_raw.join(" "));
2024-09-08 21:31:17 +02:00
2024-09-10 01:11:58 +02:00
// init
lib_plankton.log.conf_push(
[
2024-09-11 17:24:20 +02:00
// lib_plankton.log.channel_make({"kind": "file", "data": {"threshold": "debug", "path": "/tmp/kalender.log"}}),
lib_plankton.log.channel_make({"kind": "file", "data": {"threshold": "info", "path": "/dev/stderr"}}),
2024-09-10 01:11:58 +02:00
// lib_plankton.log.channel_make({"kind": "stdout", "data": {"threshold": "info"}}),
]
);
2024-09-09 12:13:10 +02:00
// exec
if (args["help"]) {
process.stdout.write(
arg_handler.generate_help(
2024-09-08 21:31:17 +02:00
{
2024-09-09 12:13:10 +02:00
"programname": "kalender",
"description": "Kalender",
"executable": "kalender",
2024-09-08 21:31:17 +02:00
}
2024-09-09 12:13:10 +02:00
)
2024-09-08 21:31:17 +02:00
+
2024-09-09 12:13:10 +02:00
"\n"
);
}
else {
const data : type_datamodel = lib_plankton.call.convey(
await lib_plankton.file.read(args["data_path"]),
[
lib_plankton.json.decode,
(data_raw : any) => (
({
"users": data_raw["users"],
"calendars": (
data_raw["calendars"]
2024-09-08 21:31:17 +02:00
.map(
2024-09-09 12:13:10 +02:00
(calendar_entry_raw : any) => ({
"id": calendar_entry_raw["id"],
"object": (
((calendar_object_raw) => {
switch (calendar_object_raw["kind"]) {
2024-09-10 01:11:58 +02:00
default: {
return calendar_object_raw;
break;
}
2024-09-09 12:13:10 +02:00
case "concrete": {
return {
"kind": "concrete",
"data": {
"name": calendar_object_raw["data"]["name"],
"users": calendar_object_raw["data"]["users"],
"events": (
calendar_object_raw["data"]["events"]
.map(
(event_raw : any) => ({
"name": event_raw["name"],
"begin": event_raw["begin"],
"end": (
(event_raw["end"] === null)
?
null
:
event_raw["end"]
),
"description": event_raw["description"],
})
)
),
},
};
break;
}
}
}) (calendar_entry_raw["object"])
),
})
2024-09-08 21:31:17 +02:00
)
2024-09-09 12:13:10 +02:00
),
}) as type_datamodel
),
]
);
2024-09-11 17:24:20 +02:00
let content : string;
2024-09-10 01:11:58 +02:00
switch (args["view_mode"]) {
default: {
2024-09-11 17:24:20 +02:00
content = "";
2024-09-10 01:11:58 +02:00
throw (new Error("invalid view mode"));
break;
2024-09-09 12:13:10 +02:00
}
2024-09-10 01:11:58 +02:00
case "table": {
2024-09-11 17:24:20 +02:00
content = await calendar_view_table_html(
2024-09-10 01:11:58 +02:00
data,
args.calendar_id,
{
"timezone_shift": args["timezone_shift"],
}
);
break;
}
case "list": {
2024-09-11 17:24:20 +02:00
content = await calendar_view_list_html(
2024-09-10 01:11:58 +02:00
data,
args.calendar_id,
{
"timezone_shift": args["timezone_shift"],
}
);
break;
}
}
2024-09-11 17:24:20 +02:00
const output : string = template_coin(
"main",
{
"content": content,
}
);
2024-09-09 12:13:10 +02:00
process.stdout.write(output);
}
return Promise.resolve<void>(undefined);
}
/*
process.stderr.write(
JSON.stringify(
{
"x1": datetime_from_date_object(
new Date(Date.now()),
{
"timezone_shift": 2,
2024-09-08 21:31:17 +02:00
}
2024-09-09 12:13:10 +02:00
),
"x2": datetime_from_date_object(
new Date(Date.now()),
{
"timezone_shift": 0,
2024-09-08 21:31:17 +02:00
}
2024-09-09 12:13:10 +02:00
),
"x3": pit_from_year_and_week(
2024,
37,
{
"timezone_shift": 0,
2024-09-08 21:31:17 +02:00
}
2024-09-09 12:13:10 +02:00
),
2024-09-08 21:31:17 +02:00
},
2024-09-09 12:13:10 +02:00
undefined,
"\t"
)
+
"\n"
);
*/
(
main(process.argv.slice(2))
.then(
() => {}
)
2024-09-10 01:11:58 +02:00
/*
2024-09-09 12:13:10 +02:00
.catch(
(error) => {process.stderr.write(String(error) + "\n");}
)
2024-09-10 01:11:58 +02:00
*/
2024-09-09 12:13:10 +02:00
);