Skip to content

Commit af08880

Browse files
committed
Simplify metadata.sh use cases (CI or interactive); just diff and check all the time
Update README.md about metadata.sh usage
1 parent ae120b3 commit af08880

File tree

3 files changed

+47
-64
lines changed

3 files changed

+47
-64
lines changed

.ci/check-metadata.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ cd "$(dirname "$(readlink -f "$BASH_SOURCE")")/.."
66
# metadata.sh takes directories with a 'metadata.json' in them
77
# metadata.json is expected in every repo
88
# "." so that the canonical source metadata.json is checked too
9-
./metadata.sh -d -c */ .
9+
./metadata.sh */ .

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ This file should contain a link to the maintainers of the Dockerfile.
131131

132132
## `metadata.json`
133133

134-
This file contains data about the repo for Docker Hub. The minimum file is defined below. `./metadata.sh [repo-name]` must be used to correctly format it (use `-f` to apply its suggested changes). There is a limit to the number of Docker Hub categories allowed; run with `-c` to check the limit and categories. `metadata.json` in the root contains the list of categories to choose from.
134+
This file contains data about the repo for Docker Hub. The minimum file is defined below. `./metadata.sh [repo-name]` must be used to correctly format it (use `-w` to apply its suggested format changes). Only three sorted unique Docker Hub categories are allowed. `metadata.json` in the root contains the list of categories to choose from.
135135

136136
```json
137137
{

metadata.sh

Lines changed: 45 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,31 @@ self="$(basename "$0")"
1414
usage() {
1515
cat <<EOUSAGE
1616
17-
usage: $self [--categories] [--diff] [--write] repo [...]
17+
usage: $self [--write] repo [...]
1818
ie: $self debian
19-
$self -d */
20-
$self -dc python
19+
$self -w python
2120
22-
This script checks a repo's metadata.json and checks categories, diffs, or formats it.
21+
This script checks a givens repo's metadata.json. It checks formating (providing a diff), checks categories, and can write the formatting changes.
2322
24-
-c, --categories Check that the categories are from the valid set of categories and that there are no more than $maxCategories. Exits non-zero if there are too many categories or they are unkown.
25-
-d, --diff Check formatting of the '[repo]/metadata.json' and print a diff. Default action if no flags are supplied. Exits non-zero if there is a difference.
2623
-h, --help Print this help output and exit.
27-
-w, --write Apply the formatting that the '-d' flag would output.
24+
-w, --write Apply json formatting (run without to see the diff that would be applied).
2825
29-
Arguments are the list of repos with a 'metadata.json' in them. 'metadata.json' is expected in every repo
26+
Arguments are the list of repos with a 'metadata.json' in them. 'metadata.json' is expected in every repo.
3027
'.' can also be passed to check the format of the canonical './metadata.json' at
3128
the root of the repo, but the max categories of '-c' is skipped for it.
3229
EOUSAGE
3330
}
3431

3532
# arg handling
36-
opts="$(getopt -o 'cdhw' --long 'categories,diff,help,write' -- "$@" || { usage >&2 && false; })"
33+
opts="$(getopt -o 'hw' --long 'help,write' -- "$@" || { usage >&2 && false; })"
3734
eval set -- "$opts"
3835

39-
categories=
40-
diff=
4136
write=
4237

4338
while true; do
4439
flag="$1"
4540
shift
4641
case "$flag" in
47-
--categories | -c) categories=1 ;;
48-
--diff | -d) diff=1 ;;
4942
--help | -h) usage && exit 0 ;;
5043
--write | -w) write=1 ;;
5144
--) break ;;
@@ -59,11 +52,6 @@ while true; do
5952
esac
6053
done
6154

62-
# default to print diff
63-
if [ -z "$diff" ] && [ -z "$categories" ] && [ -z "$write" ]; then
64-
diff=1
65-
fi
66-
6755
repos=( "$@" )
6856
if [ "${#repos[@]}" -eq 0 ]; then
6957
repos=( */ )
@@ -73,60 +61,41 @@ repos=( "${repos[@]%/}" )
7361
failures=0
7462
for repo in "${repos[@]}"; do
7563
repoFile="$repo/$jsonFile"
76-
if [ ! -e "$repoFile" ]; then
77-
echo >&2 "error: $repoFile does not exist"
64+
if [ ! -s "$repoFile" ]; then
65+
echo >&2 "error: $repoFile does not exist or is empty"
7866
(( failures++ )) || :
7967
continue
8068
fi
8169

82-
if [ -n "$diff" ] || [ -n "$write" ]; then
83-
# sort object keys and pretty print with jq as our "cannonical json"
84-
# sort categories array, no duplicates
85-
if ! repoFileJson="$(jq --sort-keys '.hub.categories |= unique' "$repoFile")"; then
86-
echo >&2 "error parsing '$repoFile'; invalid JSON?"
70+
# sort object keys and pretty print with jq as our "cannonical json"
71+
# sort categories array, no duplicates
72+
if ! repoFileJson="$(jq -s --sort-keys '.[0] | .hub.categories |= unique' "$repoFile")"; then
73+
echo >&2 "error parsing '$repoFile'; invalid JSON?"
74+
(( failures++ )) || :
75+
continue
76+
fi
77+
if ! filediff="$(diff -u "$repoFile" <(cat <<<"$repoFileJson"))"; then
78+
cat <<<"$filediff"
79+
if [ -n "$write" ]; then
80+
cat <<<"$repoFileJson" > "$repoFile"
81+
else
8782
(( failures++ )) || :
88-
continue
89-
fi
90-
if ! filediff="$(diff -u "$repoFile" <(cat <<<"$repoFileJson"))"; then
91-
if [ -n "$diff" ]; then
92-
cat <<<"$filediff"
93-
[ -n "write" ] || (( failures++ )) || :
94-
fi
95-
if [ -n "$write" ]; then
96-
cat <<<"$repoFileJson" > "$repoFile"
97-
fi
9883
fi
9984
fi
10085

10186
# TODO also check for required keys and/or types?
102-
if [ -n "$categories" ]; then
103-
# the canonicalMetadataFile doesn't have too many categories since it is the source of categories
104-
# all other metadata.json files must not be more than maxCategories
105-
if [ "$repoFile" != "$canonicalMetadataFile" ]; then
106-
if tooManyCategories="$(jq --raw-output '
107-
.hub.categories
108-
| length
109-
| if . > (env.maxCategories | tonumber) then
110-
.
111-
else empty end
112-
' "$repoFile")"; then
113-
if [ -n "$tooManyCategories" ]; then
114-
echo >&2 "error: $repoFile: too many categories: $tooManyCategories (max $maxCategories)"
115-
(( failures++ )) || :
116-
fi
117-
else
118-
echo >&2 "error parsing '$repoFile'; invalid JSON?"
119-
(( failures++ )) || :
120-
continue
121-
fi
122-
123-
fi
124-
# check for categories that aren't in the canonical set
125-
if extraCategories="$(jq -c --slurpfile canonical "$canonicalMetadataFile" '
126-
.hub.categories - $canonical[0].hub.categories
87+
# the canonicalMetadataFile doesn't have too many categories since it is the source of categories
88+
# all other metadata.json files must not be more than maxCategories
89+
if [ "$repoFile" != "$canonicalMetadataFile" ]; then
90+
if tooManyCategories="$(jq --raw-output '
91+
.hub.categories
92+
| length
93+
| if . > (env.maxCategories | tonumber) then
94+
.
95+
else empty end
12796
' "$repoFile")"; then
128-
if [ "$extraCategories" != '[]' ]; then
129-
echo >&2 "error: $repoFile: unkown categories: $extraCategories"
97+
if [ -n "$tooManyCategories" ]; then
98+
echo >&2 "error: $repoFile: too many categories: $tooManyCategories (max $maxCategories)"
13099
(( failures++ )) || :
131100
fi
132101
else
@@ -135,6 +104,20 @@ for repo in "${repos[@]}"; do
135104
continue
136105
fi
137106
fi
107+
108+
# check for categories that aren't in the canonical set
109+
if extraCategories="$(jq -c --slurpfile canonical "$canonicalMetadataFile" '
110+
.hub.categories - $canonical[0].hub.categories
111+
' "$repoFile")"; then
112+
if [ "$extraCategories" != '[]' ]; then
113+
echo >&2 "error: $repoFile: unkown categories: $extraCategories"
114+
(( failures++ )) || :
115+
fi
116+
else
117+
echo >&2 "error parsing '$repoFile'; invalid JSON?"
118+
(( failures++ )) || :
119+
continue
120+
fi
138121
done
139122

140123
exit "$failures"

0 commit comments

Comments
 (0)