Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion deploy/database.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Command, ValidationError } from "@cliffy/command";
import { createTrpcClient } from "../auth.ts";
import { error, renderTemporalTimestamp, tablePrinter } from "../util.ts";
import {
error,
renderTemporalTimestamp,
tablePrinter,
writeJsonResult,
} from "../util.ts";
import { green } from "@std/fmt/colors";
import type { GlobalContext } from "../main.ts";
import { parse as parseConnectionString } from "pg-connection-string";
Expand Down Expand Up @@ -244,6 +249,20 @@ const databasesQueryCommand = new Command<DatabaseContext>()
array: false,
});

if (options.json) {
if (res.kind === "ok") {
writeJsonResult({ rows: res.rows ?? [] });
return;
}
if (res.kind === "postgres_error") {
error(options, res.error, { errorCode: "POSTGRES_ERROR" });
}
if (res.error) {
error(options, res.message, { errorCode: "QUERY_ERROR" });
}
return;
}

if (res.kind === "ok") {
if (
Array.isArray(res.rows) && res.rows.length > 0 &&
Expand Down Expand Up @@ -318,6 +337,22 @@ const databasesListCommand = new Command<DatabaseContext>()
} & ConnectionInfo
>;

if (options.json) {
writeJsonResult(list.map((database) => ({
name: database.slug,
engine: database.engine,
createdAt: database.created_at,
assignments: database.assignments.map((a) => a.app_slug),
connection: database.safeConnectionConfig,
databases: database.databases.map((db) => ({
name: db.name,
status: db.status,
createdAt: db.created_at,
})),
})));
return;
}

tablePrinter(
["NAME", "ENGINE", "ASSIGNMENTS", "CONNECTION DETAILS"],
list,
Expand Down
22 changes: 21 additions & 1 deletion deploy/env.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Command } from "@cliffy/command";
import { parse as dotEnvParse } from "@std/dotenv";
import { error, isNonInteractive, tablePrinter } from "../util.ts";
import {
error,
isNonInteractive,
tablePrinter,
writeJsonResult,
} from "../util.ts";
import { green } from "@std/fmt/colors";
import { createTrpcClient } from "../auth.ts";
import type { GlobalContext } from "../main.ts";
Expand Down Expand Up @@ -42,6 +47,21 @@ const envListCommand = new Command<EnvCommandContext>()
{ org },
) as Context[];

if (options.json) {
writeJsonResult(envVars.map((envVar) => ({
id: envVar.id,
key: envVar.key,
value: envVar.is_secret ? null : envVar.value,
isSecret: envVar.is_secret,
contexts: envVar.context_ids
? envVar.context_ids.map((id) =>
contexts.find((c) => c.id === id)?.name ?? id
)
: null,
})));
return;
}

if (envVars.length === 0) {
console.log(
"There are no environment variables set on this application.",
Expand Down
22 changes: 21 additions & 1 deletion deploy/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ const logsCommand = new Command<GlobalContext>()
const seenIds = new Set();
let onceConnected = false;

const encoder = new TextEncoder();
const sub = trpcClient.subscription(
"apps.logs",
{
Expand All @@ -161,7 +162,7 @@ const logsCommand = new Command<GlobalContext>()
onData: (data: unknown) => {
const typedData = data as "streaming" | null | LogEntry[];
if (typedData === "streaming") {
if (!onceConnected && !options.quiet) {
if (!onceConnected && !options.quiet && !options.json) {
console.log("connected, streaming logs...");
}
onceConnected = true;
Expand All @@ -175,6 +176,25 @@ const logsCommand = new Command<GlobalContext>()
seenIds.add(id);
}

if (options.json) {
// NDJSON: one record per line on stdout, severity preserved as
// a numeric field so agents can filter without re-parsing.
Deno.stdout.writeSync(encoder.encode(
JSON.stringify({
timestamp: log.Timestamp,
traceId: log.TraceId || null,
spanId: log.SpanId || null,
severity: log.SeverityText,
severityNumber: log.SeverityNumber,
body: log.Body,
scope: log.ScopeName,
revision: log.Revision,
attributes: log.LogAttributes,
}) + "\n",
));
continue;
}

const prefix = `[${renderTemporalTimestamp(log.Timestamp)}${
log.TraceId ? ` (${log.TraceId})` : ""
}]`;
Expand Down
Loading