cli: try to fallback to env if present

This commit is contained in:
dswbx
2025-02-12 09:52:18 +01:00
parent 2c6a4d2bed
commit d84731d89e
5 changed files with 24 additions and 6 deletions

View File

@@ -76,3 +76,9 @@ export async function getConfigPath(filePath?: string) {
return;
}
export function getConnectionCredentialsFromEnv() {
const dbUrl = process.env.DB_URL;
const dbToken = process.env.DB_TOKEN;
return dbUrl ? { url: dbUrl, authToken: dbToken } : undefined;
}

View File

@@ -4,15 +4,18 @@ import { StorageLocalAdapter } from "adapter/node";
import type { CliBkndConfig, CliCommand } from "cli/types";
import { Option } from "commander";
import { config } from "core";
import dotenv from "dotenv";
import { registries } from "modules/registries";
import {
PLATFORMS,
type Platform,
attachServeStatic,
getConfigPath,
getConnectionCredentialsFromEnv,
startServer
} from "./platform";
dotenv.config();
const isBun = typeof Bun !== "undefined";
export const run: CliCommand = (program) => {
@@ -101,16 +104,26 @@ async function action(options: {
}) {
const configFilePath = await getConfigPath(options.config);
let app: App;
if (options.dbUrl || !configFilePath) {
let app: App | undefined = undefined;
if (options.dbUrl) {
const connection = options.dbUrl
? { url: options.dbUrl, authToken: options.dbToken }
: undefined;
app = await makeApp({ connection, server: { platform: options.server } });
} else {
console.log("Using config from:", configFilePath);
} else if (configFilePath) {
console.log("[INFO] Using config from:", configFilePath);
const config = (await import(configFilePath).then((m) => m.default)) as CliBkndConfig;
app = await makeConfigApp(config, options.server);
} else {
const credentials = getConnectionCredentialsFromEnv();
if (credentials) {
console.log("[INFO] Using connection from environment");
app = await makeConfigApp({ app: { connection: credentials } }, options.server);
}
}
if (!app) {
app = await makeApp({ server: { platform: options.server } });
}
await startServer(options.server, app, { port: options.port });

View File

@@ -107,7 +107,6 @@ export class SqliteIntrospector implements DatabaseIntrospector, ConnectionIntro
}
const tables = await query.execute();
console.log("tables", tables);
return Promise.all(tables.map(({ name }) => this.#getTableMetadata(name)));
}
@@ -119,7 +118,6 @@ export class SqliteIntrospector implements DatabaseIntrospector, ConnectionIntro
async #getTableMetadata(table: string): Promise<TableMetadata> {
const db = this.#db;
console.log("get table metadata", table);
// Get the SQL that was used to create the table.
const tableDefinition = await db