Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cleans up d1 list output for json #2622

Merged
merged 5 commits into from
Jan 25, 2023
Merged
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
47 changes: 47 additions & 0 deletions .changeset/rare-laws-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
"wrangler": patch
---

fix: implement `d1 list --json` with clean output for piping into other commands

Before:

```bash
rozenmd@cflaptop test % npx wrangler d1 list
--------------------
🚧 D1 is currently in open alpha and is not recommended for production data and traffic
🚧 Please report any bugs to https://github.com/cloudflare/wrangler2/issues/new/choose
🚧 To request features, visit https://community.cloudflare.com/c/developers/d1
🚧 To give feedback, visit https://discord.gg/cloudflaredev
--------------------

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ uuid β”‚ name β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ xxxxxx-xxxx-xxxx-xxxx-xxxxxx β”‚ test β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ xxxxxx-xxxx-xxxx-xxxx-xxxxxx β”‚ test2 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ xxxxxx-xxxx-xxxx-xxxx-xxxxxx β”‚ test3 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

After:

```bash
rozenmd@cflaptop test % npx wrangler d1 list --json
[
{
"uuid": "xxxxxx-xxxx-xxxx-xxxx-xxxxxx",
"name": "test"
},
{
"uuid": "xxxxxx-xxxx-xxxx-xxxx-xxxxxx",
"name": "test2"
},
{
"uuid": "xxxxxx-xxxx-xxxx-xxxx-xxxxxx",
"name": "test3"
},
]
```
44 changes: 0 additions & 44 deletions packages/wrangler/src/__tests__/d1/d1.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { cwd } from "process";
import { mockConsoleMethods } from "../helpers/mock-console";
import { useMockIsTTY } from "../helpers/mock-istty";
import { runInTempDir } from "../helpers/run-in-tmp";
import { runWrangler } from "../helpers/run-wrangler";
import writeWranglerToml from "../helpers/write-wrangler-toml";

function endEventLoop() {
return new Promise((resolve) => setImmediate(resolve));
Expand All @@ -12,7 +9,6 @@ function endEventLoop() {
describe("d1", () => {
const std = mockConsoleMethods();
runInTempDir();
const { setIsTTY } = useMockIsTTY();

it("should show help when no argument is passed", async () => {
await runWrangler("d1");
Expand Down Expand Up @@ -86,44 +82,4 @@ describe("d1", () => {
--------------------"
`);
});

describe("migrate", () => {
describe("apply", () => {
it("should not attempt to login in local mode", async () => {
setIsTTY(false);
writeWranglerToml({
d1_databases: [
{ binding: "DATABASE", database_name: "db", database_id: "xxxx" },
],
});
// If we get to the point where we are checking for migrations then we have not been asked to log in.
await expect(
runWrangler("d1 migrations apply --local DATABASE")
).rejects.toThrowError(
`No migrations present at ${cwd().replaceAll("\\", "/")}/migrations.`
);
});

it("should try to read D1 config from wrangler.toml", async () => {
setIsTTY(false);
writeWranglerToml();
await expect(
runWrangler("d1 migrations apply DATABASE")
).rejects.toThrowError(
"Can't find a DB with name/binding 'DATABASE' in local config. Check info in wrangler.toml..."
);
});

it("should not try to read wrangler.toml in local mode", async () => {
setIsTTY(false);
writeWranglerToml();
// If we get to the point where we are checking for migrations then we have not checked wrangler.toml.
await expect(
runWrangler("d1 migrations apply --local DATABASE")
).rejects.toThrowError(
`No migrations present at ${cwd().replaceAll("\\", "/")}/migrations.`
);
});
});
});
});
48 changes: 48 additions & 0 deletions packages/wrangler/src/__tests__/d1/migrate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { cwd } from "process";
import { useMockIsTTY } from "../helpers/mock-istty";
import { runInTempDir } from "../helpers/run-in-tmp";
import { runWrangler } from "../helpers/run-wrangler";
import writeWranglerToml from "../helpers/write-wrangler-toml";

describe("migrate", () => {
runInTempDir();
const { setIsTTY } = useMockIsTTY();

describe("apply", () => {
it("should not attempt to login in local mode", async () => {
setIsTTY(false);
writeWranglerToml({
d1_databases: [
{ binding: "DATABASE", database_name: "db", database_id: "xxxx" },
],
});
// If we get to the point where we are checking for migrations then we have not been asked to log in.
await expect(
runWrangler("d1 migrations apply --local DATABASE")
).rejects.toThrowError(
`No migrations present at ${cwd().replaceAll("\\", "/")}/migrations.`
);
});

it("should try to read D1 config from wrangler.toml", async () => {
setIsTTY(false);
writeWranglerToml();
await expect(
runWrangler("d1 migrations apply DATABASE")
).rejects.toThrowError(
"Can't find a DB with name/binding 'DATABASE' in local config. Check info in wrangler.toml..."
);
});

it("should not try to read wrangler.toml in local mode", async () => {
setIsTTY(false);
writeWranglerToml();
// If we get to the point where we are checking for migrations then we have not checked wrangler.toml.
await expect(
runWrangler("d1 migrations apply --local DATABASE")
).rejects.toThrowError(
`No migrations present at ${cwd().replaceAll("\\", "/")}/migrations.`
);
});
});
});
23 changes: 16 additions & 7 deletions packages/wrangler/src/d1/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,27 @@ import type {
import type { Database } from "./types";

export function Options(d1ListYargs: CommonYargsArgv) {
return d1ListYargs.epilogue(d1BetaWarning);
return d1ListYargs
.option("json", {
describe: "return output as clean JSON",
type: "boolean",
rozenmd marked this conversation as resolved.
Show resolved Hide resolved
default: false,
})
.epilogue(d1BetaWarning);
}

export async function Handler(
_: StrictYargsOptionsToInterface<typeof Options>
): Promise<void> {
export async function Handler({
json,
}: StrictYargsOptionsToInterface<typeof Options>): Promise<void> {
const accountId = await requireAuth({});
logger.log(d1BetaWarning);

const dbs: Array<Database> = await listDatabases(accountId);

render(<Table data={dbs}></Table>);
if (json) {
logger.log(JSON.stringify(dbs, null, 2));
} else {
logger.log(d1BetaWarning);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this beta warning be moved above the if statement?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point of the pull is to have a json parseable output - by having the warning, you lose that ability

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My idea was to not print the warning since it might pollute stdout

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I see the use case you may be referring to in regards of maybe piping the stdout to say a .json file or something and this messaging would break that.

render(<Table data={dbs}></Table>);
}
}

export const listDatabases = async (
Expand Down