|
| 1 | +# Design |
| 2 | + |
| 3 | +## Context |
| 4 | + |
| 5 | +See proposal.md - Why for the symptoms and specs/toc-skip-no-markers/spec.md for the |
| 6 | +requirements. |
| 7 | + |
| 8 | +`gh_toc_app` parses the options once each, in a fixed order: `--indent`, `--depth`, |
| 9 | +`--numbered`, `--bullet`, `-`, `--insert`, `--no-backup`, `--hide-footer`, `--stdout`, |
| 10 | +`--skip-header`, then the inputs. `--no-backup` and `--hide-footer` set |
| 11 | +`need_replace="yes"`. With `--stdout`, `gh_toc_app` calls `gh_toc_stdout` and returns; |
| 12 | +otherwise it calls `gh_toc` once per input, with `$#` as the number of inputs. |
| 13 | + |
| 14 | +`gh_toc` takes 10 positional parameters, the last one is `bullet`. For a local file it |
| 15 | +works in this order: |
| 16 | + |
| 17 | +``` |
| 18 | +gh_toc <input> ... |
| 19 | + | |
| 20 | + +--> one input: print "Table of Contents", "=================", "" |
| 21 | + | |
| 22 | + +--> remote URL: gh_toc_load | gh_toc_grab -> stdout |
| 23 | + | with --insert: "!! '<url>' is not a local file" |
| 24 | + | |
| 25 | + +--> local file: gh_toc_md2html -> GitHub API request |
| 26 | + gh_toc_grab, echo "$toc" -> stdout |
| 27 | + with --insert: marker check -> "You don't have ..." + exit 1 |
| 28 | + sed -i, backup and TOC files, "!! TOC was added" |
| 29 | +``` |
| 30 | + |
| 31 | +The `exit 1` of the marker check ends the whole program, so the loop in `gh_toc_app` does |
| 32 | +not reach the next inputs. |
| 33 | + |
| 34 | +The marker lines are checked with |
| 35 | +`grep -Eq '^<!--ts-->[[:space:]]*$'` and `grep -Eq '^<!--te-->[[:space:]]*$'` in three |
| 36 | +places: `gh_toc_md2html` (`--skip-header`), `gh_toc` (`--insert`) and `gh_toc_stdout` |
| 37 | +(`--stdout`), see `toc-markers`. |
| 38 | + |
| 39 | +Constraints: BSD and GNU tools, the OS/390 branch, `make lint` (shellcheck). |
| 40 | + |
| 41 | +## Goals / Non-Goals |
| 42 | + |
| 43 | +**Goals:** |
| 44 | + |
| 45 | +- Decide about a skipped file before anything is printed or sent for it. |
| 46 | +- Keep the code paths without `--skip-no-markers` as they are. |
| 47 | + |
| 48 | +**Non-Goals:** |
| 49 | + |
| 50 | +- A shared function for the four marker checks: the three existing checks stay as they |
| 51 | + are. |
| 52 | + |
| 53 | +## Decisions |
| 54 | + |
| 55 | +### The check at the top of `gh_toc` |
| 56 | + |
| 57 | +With `--skip-no-markers`, `gh_toc` checks the markers right after the check for an empty |
| 58 | +input and before the "Table of Contents" header, for a local input only |
| 59 | +(`gh_is_url` is not `yes`) that is an existing file (`[ -f "$gh_src" ]`). When a marker |
| 60 | +line is missing, it prints the warning to stderr and returns. The rest of `gh_toc` does |
| 61 | +not change. |
| 62 | + |
| 63 | +``` |
| 64 | +gh_toc <input> ... |
| 65 | + | |
| 66 | + +--> --skip-no-markers, local, existing file, no marker line: |
| 67 | + | "<input>: Warning: no <!--ts--> or <!--te--> in the file, skipped" >&2 |
| 68 | + | return 0 |
| 69 | + | |
| 70 | + +--> as before |
| 71 | +``` |
| 72 | + |
| 73 | +So a skipped file gets no header, no GitHub API request, no backup and TOC files, and |
| 74 | +the loop goes on to the next input. `return` gives 0, and the exit code of the program |
| 75 | +stays the one of its last command. |
| 76 | + |
| 77 | +Alternatives considered: |
| 78 | + |
| 79 | +- The check in the local branch, before `gh_toc_md2html`: the header is already printed, |
| 80 | + so one skipped file would leave "Table of Contents" with no entries on stdout. |
| 81 | +- Filter the inputs in `gh_toc_app` before the loop: `$#` would drop, and with it the |
| 82 | + "Table of Contents" header and the link prefix of the other inputs, e.g. a remote URL |
| 83 | + given with one skipped file would lose its prefix. The spec keeps the other inputs as |
| 84 | + before. |
| 85 | +- Replace the `exit 1` of the existing check with a `return`: the TOC is already printed |
| 86 | + and the API request is already sent by then. |
| 87 | + |
| 88 | +### The same regexes, inline |
| 89 | + |
| 90 | +The check uses the same two `grep -Eq` calls as `gh_toc_stdout`, so a file is skipped by |
| 91 | +exactly the lines `--insert` looks for (`toc-markers`). It is written inline, like the |
| 92 | +three existing checks. |
| 93 | + |
| 94 | +Alternative: a function such as `gh_toc_has_markers` for all four checks. It would touch |
| 95 | +the three existing checks, which this change does not need. |
| 96 | + |
| 97 | +### Only an existing file is skipped |
| 98 | + |
| 99 | +The `[ -f "$gh_src" ]` part keeps a missing path, a directory and `-` out of the skip: |
| 100 | +they go the same way as before, and a missing path still exits with code 1. Without it, |
| 101 | +`grep` would fail on a missing path, the file would be skipped with exit code 0, and a |
| 102 | +typo in a CI command would pass unnoticed. |
| 103 | + |
| 104 | +### Parsing and passing the option |
| 105 | + |
| 106 | +`gh_toc_app` gets `local skip_no_markers=""` next to `local to_stdout=""`, and a check |
| 107 | +right after `--hide-footer`: |
| 108 | + |
| 109 | +```bash |
| 110 | +if [ "$1" = '--skip-no-markers' ]; then |
| 111 | + need_replace="yes" |
| 112 | + skip_no_markers="yes" |
| 113 | + shift |
| 114 | +fi |
| 115 | +``` |
| 116 | + |
| 117 | +`need_replace="yes"` is what `--no-backup` and `--hide-footer` do to turn on `--insert`. |
| 118 | +The value goes to `gh_toc` as a new last (11th) parameter, read as |
| 119 | +`local skip_no_markers=${11}`. |
| 120 | + |
| 121 | +### The error with `--stdout` |
| 122 | + |
| 123 | +The option is parsed before `--stdout`, so the error is checked in the `--stdout` branch |
| 124 | +of `gh_toc_app`, before the existing check of the inputs: the message |
| 125 | +`--skip-no-markers cannot be used with --stdout.` goes to stderr and the program exits |
| 126 | +with 1. Stderr follows `toc-stdout` (Errors on stderr): with `--stdout`, every error goes |
| 127 | +to stderr. The check comes before `gh_toc_stdout`, so there is no network call. |
| 128 | + |
| 129 | +### The warning |
| 130 | + |
| 131 | +`<input>: Warning: no <!--ts--> or <!--te--> in the file, skipped` follows the form of the |
| 132 | +skipped level warning (#75), `<input>: Warning: ...`, and goes to stderr like it. Unlike |
| 133 | +that warning, it has the input also with one input: the point of the warning is which |
| 134 | +file was skipped. Stdout keeps only the TOC and the `!!` lines. |
| 135 | + |
| 136 | +### Help, README |
| 137 | + |
| 138 | +`show_help` gets |
| 139 | +` --skip-no-markers Skip files without <!--ts--> or <!--te-->. Set --insert as well. Default: false.` |
| 140 | +right after the `--hide-footer` line. The description starts at the same column as the |
| 141 | +other options, and "Set --insert as well" is the wording of `--no-backup` and |
| 142 | +`--hide-footer`. |
| 143 | + |
| 144 | +README, section "Auto insert and update TOC": after the paragraph "Next time when your |
| 145 | +file will be changed just repeat the command ...", a sentence about |
| 146 | +`--skip-no-markers` and the example |
| 147 | +`find . -name '*.md' -exec ./gh-md-toc --insert --no-backup --hide-footer --skip-no-markers {} +`. |
| 148 | +No new heading, so the README TOC in `tests/tests.bats` does not change. |
| 149 | + |
| 150 | +### Tests |
| 151 | + |
| 152 | +The new tests write their files into `$BATS_TEST_TMPDIR` with `printf` and run |
| 153 | +`run_to_files`, as the #113 and #119 tests do: |
| 154 | + |
| 155 | +- "Several files" and "Several files without the option": `a.md`, `b.md`, `c.md` as in |
| 156 | + the spec, the files compared with `cmp` and stdout with `grep -Fxc`. Two GitHub API |
| 157 | + requests each. |
| 158 | +- "One file with one marker": `GH_TOC_TOKEN=invalid`, the file in its own directory, so |
| 159 | + `ls` shows that no backup or TOC files were created. With a request to the API, the |
| 160 | + invalid token would give exit code 1, so the test also shows that there is no request. |
| 161 | + No network. |
| 162 | +- "Missing file", "With --stdout": no network. |
| 163 | +- "Without --insert": one GitHub API request; the backup and TOC files stay in |
| 164 | + `$BATS_TEST_TMPDIR`. |
| 165 | +- `test_help`: the new line at index 15, `--stdout` and `--skip-header` at 16 and 17, 19 |
| 166 | + lines in total. |
| 167 | + |
| 168 | +Before the implementation, `--skip-no-markers` is taken as an input, a missing file, so |
| 169 | +the program exits with 1 on it. So "Several files", "One file with one marker", |
| 170 | +"With --stdout", "Without --insert" and `test_help` fail before the implementation. |
| 171 | +"Missing file" and "Several files without the option" pass before it: they pin down the |
| 172 | +behavior that does not change. |
| 173 | + |
| 174 | +## Risks / Trade-offs |
| 175 | + |
| 176 | +- [A broken marker line, e.g. an indented one, makes a file skipped with exit code 0 in |
| 177 | + CI] → The warning on stderr names the file. Without `--skip-no-markers`, the error and |
| 178 | + exit code 1 stay. |
| 179 | +- [`find` also finds files in `node_modules`, `vendor` and the like] → They usually have |
| 180 | + no markers, so they are skipped without API requests. The README example is a starting |
| 181 | + point; `find` options such as `-not -path` narrow it. |
| 182 | +- [`--skip-no-markers` after `--stdout` or `--skip-header` is taken as an input] → The |
| 183 | + same limitation applies to every option; `--help` lists the options in the parse order. |
| 184 | +- [Three new tests call the GitHub API] → Same as the existing tests: `GH_TOC_TOKEN` |
| 185 | + locally, `secrets.GITHUB_TOKEN` in CI. |
0 commit comments