@@ -3,28 +3,28 @@ set -Eeuo pipefail
3
3
4
4
workdir=" $( readlink -f " $BASH_SOURCE " ) "
5
5
workdir=" $( dirname " $workdir " ) "
6
- # cd "$workdir"
6
+ cd " $workdir "
7
7
8
8
jsonFile=' metadata.json'
9
- canonicalMetadataFile=" $workdir /$jsonFile "
10
- maxCategories=3
9
+ canonicalMetadataFile=" . /$jsonFile "
10
+ export maxCategories=3
11
11
12
12
self=" $( basename " $0 " ) "
13
13
14
14
usage () {
15
15
cat << EOUSAGE
16
16
17
- usage: $self [--categories] [--diff] [--fmt ] repo [...]
17
+ usage: $self [--categories] [--diff] [--write ] repo [...]
18
18
ie: $self debian
19
19
$self -d */
20
20
$self -dc python
21
21
22
22
This script checks a repo's metadata.json and checks categories, diffs, or formats it.
23
23
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 .
28
28
29
29
Arguments are the list of repos with a 'metadata.json' in them. 'metadata.json' is expected in every repo
30
30
'.' can also be passed to check the format of the canonical './metadata.json' at
@@ -33,21 +33,21 @@ EOUSAGE
33
33
}
34
34
35
35
# 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 ; }) "
37
37
eval set -- " $opts "
38
38
39
39
categories=
40
40
diff=
41
- fmt =
41
+ write =
42
42
43
43
while true ; do
44
44
flag=" $1 "
45
45
shift
46
46
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 ;;
51
51
--) break ;;
52
52
* )
53
53
{
@@ -60,7 +60,7 @@ while true; do
60
60
done
61
61
62
62
# default to print diff
63
- if [ -z " $diff " ] && [ -z " $categories " ] && [ -z " $fmt " ]; then
63
+ if [ -z " $diff " ] && [ -z " $categories " ] && [ -z " $write " ]; then
64
64
diff=1
65
65
fi
66
66
@@ -70,37 +70,25 @@ if [ "${#repos[@]}" -eq 0 ]; then
70
70
fi
71
71
repos=( " ${repos[@]%/ } " )
72
72
73
- canonicalMetadataFileJson=" $( cat " $canonicalMetadataFile " ) "
74
- export canonicalMetadataFileJson
75
-
76
- failure=
73
+ failures=0
77
74
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 "
82
- continue
83
- fi
84
- repoFile=" $( readlink -f " $repoFile " ) "
75
+ repoFile=" $repo /$jsonFile "
85
76
86
- if [ -n " $diff " ] || [ -n " $fmt " ]; then
77
+ if [ -n " $diff " ] || [ -n " $write " ]; then
87
78
# 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
79
+ # sort categories array, no duplicates
80
+ if ! repoFileJson =" $( jq --sort-keys ' .hub.categories |= unique ' " $repoFile " ) " ; then
81
+ echo >&2 " error parsing ' $repoFile '; invalid JSON? "
82
+ (( failures ++ )) || :
92
83
continue
93
84
fi
94
- if ! filediff=" $( diff -u " $repoFile " <( echo " $repoFilejson " ) ) " ; then
85
+ if ! filediff=" $( diff -u " $repoFile " <( cat <<< " $repoFileJson " ) ) " ; then
95
86
if [ -n " $diff " ]; then
96
- echo >&2 " $repoFile "
97
- # skip diff headers
98
- echo " $filediff " | tail -n +3
99
- failure=1
87
+ cat <<< " $filediff"
88
+ [ -n " write" ] || (( failures++ )) || :
100
89
fi
101
- if [ -n " $fmt " ]; then
102
- # TODO should fmt + categories remove invalid or categories over max?
103
- echo " $repoFilejson " > " $repoFile "
90
+ if [ -n " $write " ]; then
91
+ cat <<< " $repoFileJson" > " $repoFile "
104
92
fi
105
93
fi
106
94
fi
@@ -109,32 +97,39 @@ for repo in "${repos[@]}"; do
109
97
if [ -n " $categories " ]; then
110
98
# the canonicalMetadataFile doesn't have too many categories since it is the source of categories
111
99
# all other metadata.json files must not be more than maxCategories
112
- if [ " $canonicalMetadataFile " != " $repoFile " ]; then
113
- tooManyCategories=" $( jq --raw-output '
100
+ if [ " $repoFile " != " $canonicalMetadataFile " ]; then
101
+ if tooManyCategories=" $( jq --raw-output '
114
102
.hub.categories
115
- | if length > ' " $maxCategories " ' then
116
- length
103
+ | length
104
+ | if . > (env.maxCategories | tonumber) then
105
+ .
117
106
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
107
+ ' " $repoFile " ) " ; then
108
+ if [ -n " $tooManyCategories " ]; then
109
+ echo >&2 " error: $repoFile : too many categories: $tooManyCategories (max $maxCategories )"
110
+ (( failures++ )) || :
111
+ fi
112
+ else
113
+ echo >&2 " error: $repoFile : jq parsing error"
114
+ (( failures++ )) || :
115
+ continue
122
116
fi
117
+
123
118
fi
124
119
# 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
120
+ if extraCategories=" $( jq -c --slurpfile canonical " $canonicalMetadataFile " '
121
+ .hub.categories - $canonical[0].hub.categories
122
+ ' " $repoFile " ) " ; then
123
+ if [ " $extraCategories " != ' []' ]; then
124
+ echo >&2 " error: $repoFile : unkown categories: $extraCategories "
125
+ (( failures++ )) || :
126
+ fi
127
+ else
128
+ echo >&2 " error: $repoFile : jq parsing error"
129
+ (( failures++ )) || :
130
+ continue
134
131
fi
135
132
fi
136
133
done
137
134
138
- if [ " $failure " ]; then
139
- exit 1
140
- fi
135
+ exit " $failures "
0 commit comments