Skip to content

Commit 2b2ec0a

Browse files
committed
Adapt ramalama script to a command with a dash in its name
Also remove some old cruft from the script. Signed-off-by: Martin Skøtt <[email protected]>
1 parent 3fa5554 commit 2b2ec0a

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed

hack/man-page-checker

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
#!/usr/bin/env bash
2+
#
3+
# man-page-checker - validate and cross-reference man page names
4+
#
5+
verbose=
6+
for i; do
7+
case "$i" in
8+
-v|--verbose) verbose=verbose ;;
9+
esac
10+
done
11+
12+
13+
die() {
14+
echo "$(basename $0): $*" >&2
15+
exit 1
16+
}
17+
18+
cd $(dirname $0)/../docs/ || die "Please run me from top-level libpod dir"
19+
20+
rc=0
21+
22+
for md in *.1.md;do
23+
# Read the first line after '# NAME' (or '## NAME'). (FIXME: # and ##
24+
# are not the same; should we stick to one convention?)
25+
# There may be more than one name, e.g. podman-bootc-info.1.md has
26+
# podman-bootc-system-info then another line with podman-bootc-info. We
27+
# care only about the first.
28+
name=$(grep -E -A1 '^#* NAME' $md|tail -1|awk '{print $1}' | tr -d \\\\)
29+
30+
expect=$(basename $md .1.md)
31+
if [ "$name" != "$expect" ]; then
32+
echo
33+
printf "Inconsistent program NAME in %s:\n" $md
34+
printf " NAME= %s (expected: %s)\n" $name $expect
35+
rc=1
36+
fi
37+
done
38+
39+
# Pass 2: compare descriptions.
40+
#
41+
# Make sure the descriptive text in podman-bootc-foo.1.md matches the one
42+
# in the table in podman-bootc.1.md.
43+
for md in $(ls -1 *-*-*.1.md | grep -v remote);do
44+
desc=$(grep -E -A1 '^#* NAME' $md|tail -1|sed -e 's/^podman-bootc[^ ]\+ - //')
45+
46+
# podman-bootc.1.md has a two-column table; podman-bootc-*-*.1.md all have three.
47+
parent=$(echo $md | sed -e 's/^\(.*\)-.*$/\1.1.md/')
48+
x=3
49+
if expr -- "$parent" : ".*-.*-" >/dev/null; then
50+
x=4
51+
fi
52+
53+
# Find the descriptive text in the parent man page.
54+
# Strip off the final period; let's not warn about such minutia.
55+
parent_desc=$(grep $md $parent | awk -F'|' "{print \$$x}" | sed -e 's/^ \+//' -e 's/ \+$//' -e 's/\.$//')
56+
57+
if [ "$desc" != "$parent_desc" ]; then
58+
echo
59+
printf "Inconsistent subcommand descriptions:\n"
60+
printf " %-32s = '%s'\n" $md "$desc"
61+
printf " %-32s = '%s'\n" $parent "$parent_desc"
62+
printf "Please ensure that the NAME section of $md\n"
63+
printf "matches the subcommand description in $parent\n"
64+
rc=1
65+
fi
66+
done
67+
68+
# Helper function: compares man page synopsis vs --help usage message
69+
function compare_usage() {
70+
local cmd="$1"
71+
local from_man="$2"
72+
73+
# Sometimes in CI we run before podman-bootc gets built.
74+
test -x ../../../bin/podman-bootc || return
75+
76+
# Run 'cmd --help', grab the line immediately after 'Usage:'
77+
local help_output=$(../../../bin/$cmd --help)
78+
local from_help=$(echo "$help_output" | grep -A1 '^Usage:' | tail -1)
79+
80+
# strip off command name from both
81+
from_man=$(sed -e "s/\*\*$cmd\*\*[[:space:]]*//" <<<"$from_man")
82+
from_help=$(sed -e "s/^[[:space:]]*${cmd}[[:space:]]*//" <<<"$from_help")
83+
84+
# man page lists 'foo [*options*]', help msg shows 'foo [flags]'.
85+
# Make sure if one has it, the other does too.
86+
if expr "$from_man" : "\[\*options\*\]" >/dev/null; then
87+
if expr "$from_help" : "\[options\]" >/dev/null; then
88+
:
89+
else
90+
echo "WARNING: $cmd: man page shows '[*options*]', help does not show [options]"
91+
rc=1
92+
fi
93+
elif expr "$from_help" : "\[flags\]" >/dev/null; then
94+
echo "WARNING: $cmd: --help shows [flags], man page does not show [*options*]"
95+
rc=1
96+
fi
97+
98+
# Strip off options and flags; start comparing arguments
99+
from_man=$(sed -e 's/^\[\*options\*\][[:space:]]*//' <<<"$from_man")
100+
from_help=$(sed -e 's/^\[flags\][[:space:]]*//' <<<"$from_help")
101+
102+
# Args in man page are '*foo*', in --help are 'FOO'. Convert all to
103+
# UPCASE simply because it stands out better to the eye.
104+
from_man=$(sed -e 's/\*\([a-z-]\+\)\*/\U\1/g' <<<"$from_man")
105+
106+
# FIXME: one of the common patterns is for --help to show 'POD [POD...]'
107+
# but man page show 'pod ...'. This conversion may help one day, but
108+
# not yet: there are too many inconsistencies such as '[pod ...]'
109+
# (brackets) and 'pod...' (no space between).
110+
# from_help=$(sed -e 's/\([A-Z]\+\)[[:space:]]\+\[\1[[:space:]]*\.\.\.\]/\1 .../' <<<"$from_help")
111+
112+
# Compare man-page and --help usage strings. For now, do so only
113+
# when run with --verbose.
114+
if [[ "$from_man" != "$from_help" ]]; then
115+
if [ -n "$verbose" ]; then
116+
printf "%-25s man='%s' help='%s'\n" "$cmd:" "$from_man" "$from_help"
117+
# Yeah, we're not going to enable this as a blocker any time soon.
118+
# rc=1
119+
fi
120+
fi
121+
}
122+
123+
# Pass 3: compare synopses.
124+
#
125+
# Make sure the SYNOPSIS line in podman-bootc-foo.1.md reads '**podman-bootc foo** ...'
126+
for md in *.1.md;do
127+
# FIXME: several pages have a multi-line form of SYNOPSIS in which
128+
# many or all flags are enumerated. Some of these are trivial
129+
# and really should be made into one line (podman-bootc-container-exists,
130+
# container-prune, others); some are more complicated and I
131+
# would still like to see them one-lined (container-runlabel,
132+
# image-trust) but I'm not 100% comfortable doing so myself.
133+
# To view those:
134+
# $ less $(for i in docs/*.1.md;do x=$(grep -A2 '^#* SYNOPSIS' $i|tail -1); if [ -n "$x" ]; then echo $i;fi;done)
135+
#
136+
synopsis=$(grep -E -A1 '^#* SYNOPSIS' $md|tail -1)
137+
138+
# Command name must be bracketed by double asterisks; options and
139+
# arguments are bracketed by single ones.
140+
# E.g. '**podman-bootc volume inspect** [*options*] *volume*...'
141+
# Get the command name, and confirm that it matches the md file name.
142+
cmd=$(echo "$synopsis" | sed -e 's/\(.*\)\*\*.*/\1/' | tr -d \*)
143+
md_nodash=$(basename "$md" .1.md | sed 's/-/ /2')
144+
if [[ "$cmd" != "$md_nodash" ]]; then
145+
echo
146+
printf "Inconsistent program name in SYNOPSIS in %s:\n" $md
147+
printf " SYNOPSIS = %s (expected: '%s')\n" "$cmd" "$md_nodash"
148+
rc=1
149+
fi
150+
151+
# The convention is to use UPPER CASE in 'podman-bootc foo --help',
152+
# but *lower case bracketed by asterisks* in the man page
153+
if expr "$synopsis" : ".*[A-Z]" >/dev/null; then
154+
echo
155+
printf "Inconsistent capitalization in SYNOPSIS in %s\n" $md
156+
printf " '%s' should not contain upper-case characters\n" "$synopsis"
157+
rc=1
158+
fi
159+
160+
# (for debugging, and getting a sense of standard conventions)
161+
#printf " %-32s ------ '%s'\n" $md "$synopsis"
162+
163+
# If bin/podman-bootc is available, run "cmd --help" and compare Usage
164+
# messages. This is complicated, so do it in a helper function.
165+
compare_usage "$md_nodash" "$synopsis"
166+
done
167+
168+
exit $rc

0 commit comments

Comments
 (0)