From 959b768c47cf0796061ac1314b73fb38430d4a04 Mon Sep 17 00:00:00 2001 From: Benjamin King Date: Sat, 3 Aug 2024 17:20:49 -0500 Subject: [PATCH] datasette package add `--config` CLI option (#2378) --- datasette/cli.py | 8 ++++++++ datasette/utils/__init__.py | 12 ++++++++++++ tests/test_utils.py | 3 +++ 3 files changed, 23 insertions(+) diff --git a/datasette/cli.py b/datasette/cli.py index 0c8a854102..1f449b4377 100644 --- a/datasette/cli.py +++ b/datasette/cli.py @@ -198,6 +198,12 @@ def plugins(all, requirements, plugins_dir): "--tag", help="Name for the resulting Docker container, can optionally use name:tag format", ) +@click.option( + "-c", + "--config", + type=click.File(mode="r"), + help="Path to JSON/YAML Datasette configuration file", +) @click.option( "-m", "--metadata", @@ -250,6 +256,7 @@ def plugins(all, requirements, plugins_dir): def package( files, tag, + config, metadata, extra_options, branch, @@ -276,6 +283,7 @@ def package( with temporary_docker_directory( files, "datasette", + config=config, metadata=metadata, extra_options=extra_options, branch=branch, diff --git a/datasette/utils/__init__.py b/datasette/utils/__init__.py index c87de5f003..2b5c1c093e 100644 --- a/datasette/utils/__init__.py +++ b/datasette/utils/__init__.py @@ -357,6 +357,7 @@ def escape_sqlite(s): def make_dockerfile( files, + config_file, metadata_file, extra_options, branch, @@ -378,6 +379,8 @@ def make_dockerfile( for filename in files: cmd.extend(["-i", filename]) cmd.extend(["--cors", "--inspect-file", "inspect-data.json"]) + if config_file: + cmd.extend(["--config", f"{config_file}"]) if metadata_file: cmd.extend(["--metadata", f"{metadata_file}"]) if template_dir: @@ -444,6 +447,7 @@ def make_dockerfile( def temporary_docker_directory( files, name, + config, metadata, extra_options, branch, @@ -467,6 +471,10 @@ def temporary_docker_directory( saved_cwd = os.getcwd() file_paths = [os.path.join(saved_cwd, file_path) for file_path in files] file_names = [os.path.split(f)[-1] for f in files] + if config: + config_content = parse_metadata(config.read()) + else: + config_content = {} if metadata: metadata_content = parse_metadata(metadata.read()) else: @@ -479,6 +487,7 @@ def temporary_docker_directory( try: dockerfile = make_dockerfile( file_names, + config_content and "config.json", metadata_content and "metadata.json", extra_options, branch, @@ -494,6 +503,9 @@ def temporary_docker_directory( apt_get_extras=apt_get_extras, ) os.chdir(datasette_dir) + if config_content: + with open("config.json", "w") as fp: + fp.write(json.dumps(config_content, indent=2)) if metadata_content: with open("metadata.json", "w") as fp: fp.write(json.dumps(metadata_content, indent=2)) diff --git a/tests/test_utils.py b/tests/test_utils.py index 88a4532ad1..de677c9683 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -266,6 +266,7 @@ def test_temporary_docker_directory_uses_hard_link(): with utils.temporary_docker_directory( files=["hello"], name="t", + config=None, metadata=None, extra_options=None, branch=None, @@ -296,6 +297,7 @@ def test_temporary_docker_directory_uses_copy_if_hard_link_fails(mock_link): with utils.temporary_docker_directory( files=["hello"], name="t", + config=None, metadata=None, extra_options=None, branch=None, @@ -322,6 +324,7 @@ def test_temporary_docker_directory_quotes_args(): with utils.temporary_docker_directory( files=["hello"], name="t", + config=None, metadata=None, extra_options="--$HOME", branch=None,