diff --git a/README.md b/README.md index 27199d1..92a629b 100644 --- a/README.md +++ b/README.md @@ -35,14 +35,18 @@ $ python setup.py install Usage: adr-viewer [OPTIONS] Options: - --adr-path TEXT Directory containing ADR files. [default: doc/adr/] - --output TEXT File to write output to. [default: index.html] - --serve Serve content at http://localhost:8000/ - --port INT Custom server port [default: 8000] - --help Show this message and exit. + --adr-path TEXT Directory containing ADR files; pass explicitly, read + .adr-dir if it exists or uses doc/adr/ + --output TEXT File to write output to. [default: index.html] + --serve Serve content at http://localhost:8000/ + --port INTEGER Change port for the server [default: 8000] + --template-dir TEXT Template directory. + --help Show this message and exit. ``` The default for `--adr-path` is `doc/adr/` because this is the default path generated by `adr-tools`. +If `.adr-dir` exists in the directory in which `adr-viewer` was executed, it will be read and used for the `--adr-path`, +following the `adr-tools` convention for overrides. ## Supported Record Types diff --git a/adr_viewer/__init__.py b/adr_viewer/__init__.py index 8093691..5b70d20 100644 --- a/adr_viewer/__init__.py +++ b/adr_viewer/__init__.py @@ -103,13 +103,39 @@ def generate_content(path, template_dir_override=None): return render_html(config, template_dir_override) +CONVENTIONAL_ADR_DIR = 'doc/adr/' +DEFAULT_ADR_DIR_FILE = '.adr-dir' + + +def resolve_adr_dir(maybe_dir=None, adr_dir_file=DEFAULT_ADR_DIR_FILE): + """ + If passed something, blindly use it. Otherwise, resolve based on + conventions in the ADR tooling ecosystem. + """ + def lookup(): + adr_dir = CONVENTIONAL_ADR_DIR + if os.path.exists(adr_dir_file): + with open(adr_dir_file, 'r') as file: + adr_dir = file.read().strip() + return adr_dir + + return maybe_dir if maybe_dir else lookup() + + @click.command() -@click.option('--adr-path', default='doc/adr/', help='Directory containing ADR files.', show_default=True) +@click.option('--adr-path', + default=None, + help=f""" + Directory containing ADR files; pass explicitly, + read {DEFAULT_ADR_DIR_FILE} if it exists or uses {CONVENTIONAL_ADR_DIR} + """, + show_default=True) @click.option('--output', default='index.html', help='File to write output to.', show_default=True) @click.option('--serve', default=False, help='Serve content at http://localhost:8000/', is_flag=True) @click.option('--port', default=8000, help='Change port for the server', show_default=True) @click.option('--template-dir', default=None, help='Template directory.', show_default=True) def main(adr_path, output, serve, port, template_dir): + adr_path = resolve_adr_dir(adr_path) content = generate_content(adr_path, template_dir) if serve: diff --git a/adr_viewer/test_adr_viewer.py b/adr_viewer/test_adr_viewer.py index 8274dbf..84f5a8e 100644 --- a/adr_viewer/test_adr_viewer.py +++ b/adr_viewer/test_adr_viewer.py @@ -1,4 +1,6 @@ -from adr_viewer import parse_adr_to_config, render_html +import tempfile + +from adr_viewer import parse_adr_to_config, render_html, resolve_adr_dir def test_should_extract_title_from_record(): @@ -85,4 +87,20 @@ def test_should_render_html_with_collapsible_index(): def test_should_ignore_invalid_files(): config = parse_adr_to_config('test/adr/0003-bad-formatting.md') - assert config is None \ No newline at end of file + assert config is None + + +def test_resolve_adr_path_default(): + assert resolve_adr_dir() == "doc/adr/" + + +def test_resolve_adr_path_explicit(): + assert resolve_adr_dir("docs/adrs") == "docs/adrs" + + +def test_resolve_adr_path_adrdir(): + test_adr_dir_content = "just a test " + with tempfile.NamedTemporaryFile('w') as tmpfile: + tmpfile.write(test_adr_dir_content) + tmpfile.flush() + assert resolve_adr_dir(None, adr_dir_file=tmpfile.name) == test_adr_dir_content.strip()