From 9c6f4fa9ec6c1e82e9935c9b07c7c460bd7ed9f1 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 5 Jun 2024 13:48:32 -0400 Subject: [PATCH] add --host flag to docs serve (#10228) (#10231) --- .changes/unreleased/Features-20240527-124405.yaml | 6 ++++++ core/dbt/cli/main.py | 1 + core/dbt/cli/params.py | 8 ++++++++ core/dbt/task/docs/serve.py | 3 ++- tests/unit/task/docs/test_serve.py | 11 +++++++++++ 5 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 .changes/unreleased/Features-20240527-124405.yaml diff --git a/.changes/unreleased/Features-20240527-124405.yaml b/.changes/unreleased/Features-20240527-124405.yaml new file mode 100644 index 00000000000..5dd2850609d --- /dev/null +++ b/.changes/unreleased/Features-20240527-124405.yaml @@ -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" diff --git a/core/dbt/cli/main.py b/core/dbt/cli/main.py index 35a01700acd..9b6479a0d02 100644 --- a/core/dbt/cli/main.py +++ b/core/dbt/cli/main.py @@ -280,6 +280,7 @@ def docs_generate(ctx, **kwargs): @click.pass_context @global_flags @p.browser +@p.host @p.port @p.profiles_dir @p.project_dir diff --git a/core/dbt/cli/params.py b/core/dbt/cli/params.py index b2716728ce6..b5ebf89b3a9 100644 --- a/core/dbt/cli/params.py +++ b/core/dbt/cli/params.py @@ -140,6 +140,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", diff --git a/core/dbt/task/docs/serve.py b/core/dbt/task/docs/serve.py index 99f97700758..1b8e4c01aff 100644 --- a/core/dbt/task/docs/serve.py +++ b/core/dbt/task/docs/serve.py @@ -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") diff --git a/tests/unit/task/docs/test_serve.py b/tests/unit/task/docs/test_serve.py index cedb234a205..5a832341201 100644 --- a/tests/unit/task/docs/test_serve.py +++ b/tests/unit/task/docs/test_serve.py @@ -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 @@ -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)