Skip to content

Commit

Permalink
Use .adr-dir for --adr-path if it exists
Browse files Browse the repository at this point in the history
Use whatever the user passed if they passed anything, then check for
.adr-dir before falling back to `doc/adr`.

Fixes mrwilson#25
  • Loading branch information
colindean committed Mar 16, 2023
1 parent f3d5fdc commit fda3112
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 27 additions & 1 deletion adr_viewer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 20 additions & 2 deletions adr_viewer/test_adr_viewer.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down Expand Up @@ -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
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()

0 comments on commit fda3112

Please sign in to comment.