From 8c22dfbb707f99534c9f3fb48c41043289d462eb Mon Sep 17 00:00:00 2001 From: Michelle Ark Date: Tue, 28 May 2024 11:11:56 -0400 Subject: [PATCH] add --host flag to docs serve (#10228) (cherry picked from commit c2d4643f9d5432a268937ad21da83be9df2fb971) --- .changes/unreleased/Features-20240527-124405.yaml | 6 ++++++ core/dbt/cli/main.py | 1 + core/dbt/cli/params.py | 8 ++++++++ core/dbt/task/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 ab501e015f4..6df286930d9 100644 --- a/core/dbt/cli/main.py +++ b/core/dbt/cli/main.py @@ -298,6 +298,7 @@ def docs_generate(ctx, **kwargs): @click.pass_context @global_flags @p.browser +@p.host @p.port @p.profile @p.profiles_dir diff --git a/core/dbt/cli/params.py b/core/dbt/cli/params.py index b23516391bb..ae15e73036a 100644 --- a/core/dbt/cli/params.py +++ b/core/dbt/cli/params.py @@ -119,6 +119,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/serve.py b/core/dbt/task/serve.py index ef378342f98..3ba95580ee0 100644 --- a/core/dbt/task/serve.py +++ b/core/dbt/task/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 c7937c01a16..5d3193b6f46 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)