From 446d8a883cbb17c0f526a1e0cb0093a529d75d9d Mon Sep 17 00:00:00 2001 From: Luke Browning Date: Thu, 25 Jul 2024 17:08:44 -0700 Subject: [PATCH] Add database option to cli + fix db.setup() timing issue --- src/commands/download.ts | 2 +- src/commands/index.ts | 4 ++-- src/commands/manage.ts | 5 ++++- src/commands/register.ts | 4 +++- src/commands/update.ts | 3 ++- src/main.ts | 29 +++++++++++++++++++++++++---- 6 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/commands/download.ts b/src/commands/download.ts index 8c96020..71ce07c 100644 --- a/src/commands/download.ts +++ b/src/commands/download.ts @@ -7,7 +7,7 @@ import { CHAPTER_DELAY_TIME } from "@core/constants"; import { Command as Commander } from 'commander'; -export function initDownloadCommand(program: Commander, db: Database) { +export function initDownloadCommand(program: Commander) { const downloadFunction = async (plugin: string, query: string, options: any) => { let parsedPlugin = await parsePlugin(plugin); let download = await createDownload(parsedPlugin, query); diff --git a/src/commands/index.ts b/src/commands/index.ts index 60b90e1..0243a40 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -15,9 +15,9 @@ const Commands = { 'manage': handleManageDialog, 'update': handleUpdateDialog } -export function initCommands(program: Commander, db: Database) { +export function initCommands(program: Commander) { for (const a of initFunctions) { - a(program, db) + a(program) } } diff --git a/src/commands/manage.ts b/src/commands/manage.ts index 1b1bf5b..7e25a24 100644 --- a/src/commands/manage.ts +++ b/src/commands/manage.ts @@ -8,8 +8,10 @@ import { MangaPlugin } from "plugin"; import { Command as Commander } from "commander"; import Table from "cli-table3"; -export function initManageCommand(program: Commander, db: Database) { +export function initManageCommand(program: Commander) { const deleteFunction = async (plugin: string | undefined, id: string | undefined, options: any) => { + let db = program.opts().database as Database; + let target: { plugin: MangaPlugin, id: string } | undefined; if (id && plugin) { @@ -25,6 +27,7 @@ export function initManageCommand(program: Commander, db: Database) { } const listFunction = async (options: any) => { + let db = program.opts().database as Database; await printListFromDatabase(db, options.showChapters); await db.close(); } diff --git a/src/commands/register.ts b/src/commands/register.ts index e8ecd53..3a995b4 100644 --- a/src/commands/register.ts +++ b/src/commands/register.ts @@ -6,8 +6,10 @@ import { getUserConfirmation } from "@helpers/cli"; import { Command as Commander } from "commander"; -export function initRegisterCommand(program: Commander, db: Database) { +export function initRegisterCommand(program: Commander) { const registerFunction = async (plugin: string, query: string, options: any) => { + let db = program.opts().database as Database; + let parsedPlugin = await parsePlugin(plugin); let manga = await getManga(parsedPlugin, query); diff --git a/src/commands/update.ts b/src/commands/update.ts index 0ac0fa0..6e4bf77 100644 --- a/src/commands/update.ts +++ b/src/commands/update.ts @@ -6,8 +6,9 @@ import { UPDATE_CHAPTER_DELAY_TIME } from "@core/constants"; import { Command as Commander } from "commander"; -export function initUpdateCommand(program: Commander, db: Database) { +export function initUpdateCommand(program: Commander) { let updateFunction = async () => { + let db = program.opts().database as Database; await runUpdate(db); await db.close(); } diff --git a/src/main.ts b/src/main.ts index 2654d70..99971a4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,19 +1,26 @@ -import SQLite from "@databases/sqlite"; import Config from "@core/config"; import { handleDialog, initCommands } from "./commands"; import { Command as Commander } from 'commander'; import { ALL_PLUGIN_NAMES } from "./plugins"; +import { Database } from "database"; +import SQLite from "@databases/sqlite"; +import Postgres from "@databases/postgres"; + const program = new Commander(); -let db = new SQLite(); +let db: Database; export async function run() { program .description("A CLI utility to download manga chapters from various online platforms.") .addHelpText('after', `\nExample:\n $ mangathr --help`) - .hook('preAction', async () => await db.setup() ); + .hook('preAction', async (thisCommand, _) => { + // Pass option up to file-wide variable + db = (thisCommand.opts().database as Database); + await db.setup(); + } ); program .option('--db-path ', "specify path to database", @@ -23,6 +30,20 @@ export async function run() { (v: string) => Config.CONFIG.PSQL_CONNECTION_STRING = v) .option('--dest ', "specify path to save files", (v: string) => Config.CONFIG.DOWNLOAD_DIR = v) + .option('--database ', "database driver to use (default sqlite)", + (v: string) => { + switch (v.toLowerCase().trim()) { + case "postgres": + case "psql": + return new Postgres(); + + default: + console.warn(`\x1b[33mUnknown driver '${v}', defaulting to sqlite...\x1b[0m`); + case "sqlite": + case "sqlite3": + return new SQLite(); + } + }, new SQLite()); program .option('--list-plugins', "prints all available plugin names") @@ -37,7 +58,7 @@ export async function run() { await handleDialog(db); }); - initCommands(program, db); + initCommands(program); await program.parseAsync(process.argv); }