Skip to content

Commit

Permalink
add --host flag to docs serve
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelleArk committed May 27, 2024
1 parent 84456f5 commit 2ff8fd9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
8 changes: 8 additions & 0 deletions core/dbt/cli/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@
is_flag=True,
)

host = click.option(
"--host",
envvar="DBT_HOST",
help="host to serve dbt docs on",
type=click.STRING,
default="127.0.0.1",
)

indirect_selection = click.option(
"--indirect-selection",
envvar="DBT_INDIRECT_SELECTION",
Expand Down
3 changes: 2 additions & 1 deletion core/dbt/task/docs/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ def run(self):
shutil.copyfile(DOCS_INDEX_FILE_PATH, "index.html")

port = self.args.port
host = self.args.host

if self.args.browser:
webbrowser.open_new_tab(f"http://localhost:{port}")

with socketserver.TCPServer(("127.0.0.1", port), SimpleHTTPRequestHandler) as httpd:
with socketserver.TCPServer((host, port), SimpleHTTPRequestHandler) as httpd:
click.echo(f"Serving docs at {port}")
click.echo(f"To access from your browser, navigate to: http://localhost:{port}")
click.echo("\n\n")
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/task/docs/test_serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def serve_task():
task = ServeTask(config=MagicMock(), args=MagicMock())
task.config.project_target_path = "."
task.args.port = 8000
task.args.host = "127.0.0.1"
return task


Expand All @@ -21,3 +22,13 @@ def test_serve_bind_to_127(serve_task):
patched_TCPServer.return_value = MagicMock()
serve_task.run()
patched_TCPServer.assert_called_once_with(("127.0.0.1", 8000), SimpleHTTPRequestHandler)


def test_serve_bind_to_all(serve_task):
serve_task.args.browser = False
serve_task.args.host = ""

with patch("dbt.task.docs.serve.socketserver.TCPServer") as patched_TCPServer:
patched_TCPServer.return_value = MagicMock()
serve_task.run()
patched_TCPServer.assert_called_once_with(("", 8000), SimpleHTTPRequestHandler)

0 comments on commit 2ff8fd9

Please sign in to comment.