Skip to content

Commit d9ebf60

Browse files
committed
Adjustments following tianon's review
1 parent e4274de commit d9ebf60

File tree

3 files changed

+70
-72
lines changed

3 files changed

+70
-72
lines changed

get-categories.sh

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,10 @@ workdir="$(dirname "$workdir")"
77
jsonFile='metadata.json'
88
canonicalMetadataFile="$workdir/$jsonFile"
99

10-
allCategories="$(curl -fsSL https://hub.docker.com/v2/categories)"
11-
export allCategories
12-
1310
# add categories slugs to canonicalMetadataFile without losing other keys there
14-
json="$(
15-
jq --sort-keys '
16-
(env.allCategories | fromjson) as $allCategories
17-
| .hub.categories = ( [ $allCategories[].slug ] | sort )
18-
' "$canonicalMetadataFile"
19-
)"
20-
21-
echo "$json" | tee "$canonicalMetadataFile"
11+
curl -fsSL https://hub.docker.com/v2/categories | jq -s --sort-keys '
12+
.[0] as $allCategories
13+
| .[1]
14+
| .hub.categories = ( [ $allCategories[].slug ] | sort )
15+
' - "$canonicalMetadataFile" | tee "$canonicalMetadataFile.new"
16+
mv "$canonicalMetadataFile.new" "$canonicalMetadataFile"

metadata.sh

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,28 @@ set -Eeuo pipefail
33

44
workdir="$(readlink -f "$BASH_SOURCE")"
55
workdir="$(dirname "$workdir")"
6-
#cd "$workdir"
6+
cd "$workdir"
77

88
jsonFile='metadata.json'
9-
canonicalMetadataFile="$workdir/$jsonFile"
10-
maxCategories=3
9+
canonicalMetadataFile="./$jsonFile"
10+
export maxCategories=3
1111

1212
self="$(basename "$0")"
1313

1414
usage() {
1515
cat <<EOUSAGE
1616
17-
usage: $self [--categories] [--diff] [--fmt] repo [...]
17+
usage: $self [--categories] [--diff] [--write] repo [...]
1818
ie: $self debian
1919
$self -d */
2020
$self -dc python
2121
2222
This script checks a repo's metadata.json and checks categories, diffs, or formats it.
2323
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.
26-
-f, --fmt, --format Apply the formatting that the '-d' flag would output.
27-
-h, --help Print this help output and exit.
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.
26+
-h, --help Print this help output and exit.
27+
-w, --write Apply the formatting that the '-d' flag would output.
2828
2929
Arguments are the list of repos with a 'metadata.json' in them. 'metadata.json' is expected in every repo
3030
'.' can also be passed to check the format of the canonical './metadata.json' at
@@ -33,21 +33,21 @@ EOUSAGE
3333
}
3434

3535
# arg handling
36-
opts="$(getopt -o 'cdfh' --long 'categories,diff,fmt,format,help' -- "$@" || { usage >&2 && false; })"
36+
opts="$(getopt -o 'cdhw' --long 'categories,diff,help,write' -- "$@" || { usage >&2 && false; })"
3737
eval set -- "$opts"
3838

3939
categories=
4040
diff=
41-
fmt=
41+
write=
4242

4343
while true; do
4444
flag="$1"
4545
shift
4646
case "$flag" in
47-
--categories|-c) categories=1 ;;
48-
--diff|-d) diff=1 ;;
49-
--fmt|--format|-f) fmt=1 ;;
50-
--help|-h) usage && exit 0 ;;
47+
--categories | -c) categories=1 ;;
48+
--diff | -d) diff=1 ;;
49+
--help | -h) usage && exit 0 ;;
50+
--write | -w) write=1 ;;
5151
--) break ;;
5252
*)
5353
{
@@ -60,7 +60,7 @@ while true; do
6060
done
6161

6262
# default to print diff
63-
if [ -z "$diff" ] && [ -z "$categories" ] && [ -z "$fmt" ]; then
63+
if [ -z "$diff" ] && [ -z "$categories" ] && [ -z "$write" ]; then
6464
diff=1
6565
fi
6666

@@ -70,37 +70,30 @@ if [ "${#repos[@]}" -eq 0 ]; then
7070
fi
7171
repos=( "${repos[@]%/}" )
7272

73-
canonicalMetadataFileJson="$(cat "$canonicalMetadataFile")"
74-
export canonicalMetadataFileJson
75-
76-
failure=
73+
failures=0
7774
for repo in "${repos[@]}"; do
78-
repoFile="$workdir/$repo/$jsonFile"
79-
if [ ! -f "$repoFile" ]; then
80-
failure=1
81-
echo "error file does not exist: $repo/$jsonFile"
75+
repoFile="$repo/$jsonFile"
76+
if [ ! -e "$repoFile" ]; then
77+
echo >&2 "error: $repoFile does not exist"
78+
(( failures++ )) || :
8279
continue
8380
fi
84-
repoFile="$(readlink -f "$repoFile")"
8581

86-
if [ -n "$diff" ] || [ -n "$fmt" ]; then
82+
if [ -n "$diff" ] || [ -n "$write" ]; then
8783
# sort object keys and pretty print with jq as our "cannonical json"
88-
# sort categories array
89-
if ! repoFilejson="$(jq --sort-keys '.hub.categories |= sort' "$repoFile")"; then
90-
echo "error $repo/$jsonFile improper json"
91-
failure=1
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?"
87+
(( failures++ )) || :
9288
continue
9389
fi
94-
if ! filediff="$(diff -u "$repoFile" <(echo "$repoFilejson"))"; then
90+
if ! filediff="$(diff -u "$repoFile" <(cat <<<"$repoFileJson"))"; then
9591
if [ -n "$diff" ]; then
96-
echo >&2 "$repoFile"
97-
# skip diff headers
98-
echo "$filediff" | tail -n +3
99-
failure=1
92+
cat <<<"$filediff"
93+
[ -n "write" ] || (( failures++ )) || :
10094
fi
101-
if [ -n "$fmt" ]; then
102-
# TODO should fmt + categories remove invalid or categories over max?
103-
echo "$repoFilejson" > "$repoFile"
95+
if [ -n "$write" ]; then
96+
cat <<<"$repoFileJson" > "$repoFile"
10497
fi
10598
fi
10699
fi
@@ -109,32 +102,39 @@ for repo in "${repos[@]}"; do
109102
if [ -n "$categories" ]; then
110103
# the canonicalMetadataFile doesn't have too many categories since it is the source of categories
111104
# all other metadata.json files must not be more than maxCategories
112-
if [ "$canonicalMetadataFile" != "$repoFile" ]; then
113-
tooManyCategories="$(jq --raw-output '
105+
if [ "$repoFile" != "$canonicalMetadataFile" ]; then
106+
if tooManyCategories="$(jq --raw-output '
114107
.hub.categories
115-
| if length > '"$maxCategories"' then
116-
length
108+
| length
109+
| if . > (env.maxCategories | tonumber) then
110+
.
117111
else empty end
118-
' "$repoFile")"
119-
if [ -n "$tooManyCategories" ]; then
120-
echo >&2 "$repo, error too many Docker Hub categories: $tooManyCategories, max $maxCategories"
121-
failure=1
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
122121
fi
122+
123123
fi
124124
# check for categories that aren't in the canonical set
125-
extraCategories="$(jq -c '
126-
(env.canonicalMetadataFileJson | fromjson | .hub.categories) as $canonicalCategories
127-
| .hub.categories
128-
| map(. as $cat | select($canonicalCategories | index($cat) < 0))
129-
| if length > 0 then . else empty end
130-
' "$repoFile")"
131-
if [ -n "$extraCategories" ]; then
132-
echo >&2 "$repo, error unkown categories: $extraCategories"
133-
failure=1
125+
if extraCategories="$(jq -c --slurpfile canonical "$canonicalMetadataFile" '
126+
.hub.categories - $canonical[0].hub.categories
127+
' "$repoFile")"; then
128+
if [ "$extraCategories" != '[]' ]; then
129+
echo >&2 "error: $repoFile: unkown categories: $extraCategories"
130+
(( failures++ )) || :
131+
fi
132+
else
133+
echo >&2 "error parsing '$repoFile'; invalid JSON?"
134+
(( failures++ )) || :
135+
continue
134136
fi
135137
fi
136138
done
137139

138-
if [ "$failure" ]; then
139-
exit 1
140-
fi
140+
exit "$failures"

push.pl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,17 +213,17 @@ sub prompt_for_edit {
213213
$repoDetails->{description} //= '';
214214
$repoDetails->{full_description} //= '';
215215
$repoDetails->{categories} //= [];
216-
my @repoCategories = sort map { $_->{slug} } @{$repoDetails->{categories}};
216+
my @repoCategories = sort map { $_->{slug} } @{ $repoDetails->{categories} };
217217

218218
# read local categories from metadata.json
219219
my $repoMetadataBytes = Mojo::File->new($repoName . '/metadata.json')->slurp;
220220
my $repoMetadataJson = decode_json $repoMetadataBytes;
221-
my @localRepoCategories = sort @{$repoMetadataJson->{hub}{categories}};
221+
my @localRepoCategories = sort @{ $repoMetadataJson->{hub}{categories} };
222222

223223
# check if the local categories differ in length or items from the remote
224224
my $needCat = @localRepoCategories != @repoCategories;
225225
if (! $needCat) {
226-
foreach my $i (0..@localRepoCategories) {
226+
foreach my $i (0 .. @localRepoCategories) {
227227
last if ! defined $repoCategories[$i]; # length difference already covered, so we can bail
228228
if ($localRepoCategories[$i] ne $repoCategories[$i]) {
229229
$needCat = 1;
@@ -234,7 +234,10 @@ sub prompt_for_edit {
234234
if ($needCat) {
235235
say 'updating ' . $repoName . ' categories';
236236
my $catsPatch = $ua->patch($repoUrl . 'categories/' => { %$authorizationHeader, Accept => 'application/json' } => json => [
237-
map { { slug => $_ => name => 'All those moments will be lost in time, like tears in rain... Time to die.' } } @{$repoMetadataJson->{hub}{categories}}
237+
map { {
238+
slug => $_,
239+
name => 'All those moments will be lost in time, like tears in rain... Time to die.',
240+
} } @{$repoMetadataJson->{hub}{categories}}
238241
]);
239242
die 'patch to categories failed: ' . $catsPatch->res->text unless $catsPatch->res->is_success;
240243
}

0 commit comments

Comments
 (0)