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 a03e6afdcb0..285357a0975 100644 --- a/core/dbt/cli/main.py +++ b/core/dbt/cli/main.py @@ -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 diff --git a/core/dbt/cli/params.py b/core/dbt/cli/params.py index b29cd0fefd1..b874adeafff 100644 --- a/core/dbt/cli/params.py +++ b/core/dbt/cli/params.py @@ -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", 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..fb85fb9d09e 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.serve.socketserver.TCPServer") as patched_TCPServer: + patched_TCPServer.return_value = MagicMock() + serve_task.run() + patched_TCPServer.assert_called_once_with(("", 8000), SimpleHTTPRequestHandler)