Skip to content

Commit

Permalink
add --host flag to docs serve (#10228)
Browse files Browse the repository at this point in the history
(cherry picked from commit c2d4643)
  • Loading branch information
MichelleArk authored and github-actions[bot] committed May 28, 2024
1 parent 2b61767 commit cc81406
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20240527-124405.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: Add --host flag to dbt docs serve, defaulting to '127.0.0.1'
time: 2024-05-27T12:44:05.040843-04:00
custom:
Author: michelleark
Issue: "10229"
1 change: 1 addition & 0 deletions core/dbt/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ def docs_generate(ctx, **kwargs):
@click.pass_context
@global_flags
@p.browser
@p.host
@p.port
@p.profile
@p.profiles_dir
Expand Down
8 changes: 8 additions & 0 deletions core/dbt/cli/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,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/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

Check warning on line 19 in core/dbt/task/serve.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/task/serve.py#L19

Added line #L19 was not covered by tests

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:

Check warning on line 24 in core/dbt/task/serve.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/task/serve.py#L24

Added line #L24 was not covered by tests
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 cc81406

Please sign in to comment.