Skip to content

Commit

Permalink
url opener
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienPensart committed Nov 12, 2023
1 parent 3022f3c commit 6576e3b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
7 changes: 4 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,10 @@ musicbot database edgeql
Options:
MusicDB options:
--dsn TEXT DSN to MusicBot EdgeDB
--graphql TEXT DSN to MusicBot GrapQL
-h, --help Show this message and exit.
--dsn TEXT DSN to MusicBot EdgeDB
--graphql TEXT DSN to MusicBot GrapQL
--output [json|table|m3u] Output format [default: table]
-h, --help Show this message and exit.
musicbot database graphiql
**************************
Expand Down
19 changes: 12 additions & 7 deletions musicbot/commands/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from musicbot import MusicbotObject, MusicDb, syncify
from musicbot.cli.musicdb import musicdb_options
from musicbot.cli.options import open_option, yes_option
from musicbot.cli.options import open_option, output_option, yes_option


@click.group(help="DB management", cls=AdvancedGroup, aliases=["db", "edgedb"])
Expand All @@ -20,10 +20,17 @@ def cli() -> None:
@cli.command(help="EdgeDB raw query", aliases=["query", "fetch", "execute"])
@click.argument("query")
@musicdb_options
@output_option
@syncify
@beartype
async def edgeql(musicdb: MusicDb, query: str) -> None:
print(await musicdb.query_json(query))
async def edgeql(musicdb: MusicDb, output: str, query: str) -> None:
result = await musicdb.query_json(query)
if output == "json":
intermediate_json = MusicbotObject.loads_json(result)
if intermediate_json is not None:
MusicbotObject.print_json(intermediate_json)
else:
print(result)


@cli.command(help="GraphQL query")
Expand Down Expand Up @@ -63,10 +70,9 @@ def pgcli(ctx: click.Context, musicdb: MusicDb, pgcli_args: tuple[str, ...]) ->
@beartype
def graphiql(musicdb: MusicDb, _open: bool) -> None:
if musicdb.graphql:
url = f"{musicdb.graphql}/explore"
print(url)
print(musicdb.graphiql)
if _open and not MusicbotObject.dry:
_ = webbrowser.open(url)
_ = webbrowser.open(musicdb.graphiql)


@cli.command(help="Explore with EdgeDB UI", context_settings=dict(ignore_unknown_options=True, help_option_names=[]))
Expand All @@ -87,7 +93,6 @@ def ui(

args = ["edgedb", "ui", "--print-url", "--no-server-check", "--dsn", musicdb.dsn] + list(edgedb_args)
try:
# _ = subprocess.run(args, check=True)
url = subprocess.check_output(
" ".join(args),
stderr=subprocess.STDOUT,
Expand Down
8 changes: 6 additions & 2 deletions musicbot/musicdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Self
from typing import Self
from urllib.parse import urlparse

import edgedb
Expand Down Expand Up @@ -67,7 +67,11 @@ def from_dsn(
graphql = f"http://{parsed.hostname}:{parsed.port}/db/edgedb/graphql"
return cls(client=client, graphql=graphql)

async def query_json(self, query: str) -> Any:
@property
def graphiql(self) -> str:
return f"{self.graphql}/explore"

async def query_json(self, query: str) -> str:
return await self.client.query_json(query)

async def graphql_query(self, query: str) -> httpx.Response | None:
Expand Down
8 changes: 6 additions & 2 deletions tests/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_database_pgcli(cli_runner: CliRunner, edgedb: str) -> None:

@beartype
def test_database_edgeql(cli_runner: CliRunner, edgedb: str) -> None:
_ = run_cli(
output = run_cli(
cli_runner,
cli,
[
Expand All @@ -34,8 +34,11 @@ def test_database_edgeql(cli_runner: CliRunner, edgedb: str) -> None:
"select Music;",
"--dsn",
edgedb,
"--output",
"json",
],
)
assert MusicbotObject.loads_json(output) is not None


@beartype
Expand Down Expand Up @@ -74,7 +77,7 @@ def test_database_graphiql(cli_runner: CliRunner, edgedb: str) -> None:

@beartype
def test_database_ui(cli_runner: CliRunner, edgedb: str) -> None:
_ = run_cli(
url = run_cli(
cli_runner,
cli,
[
Expand All @@ -86,6 +89,7 @@ def test_database_ui(cli_runner: CliRunner, edgedb: str) -> None:
"--no-open",
],
)
_ = httpx.head(url, timeout=5)


@beartype
Expand Down

0 comments on commit 6576e3b

Please sign in to comment.