-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse-repos.sh
More file actions
executable file
·254 lines (236 loc) · 5.86 KB
/
Copy pathparse-repos.sh
File metadata and controls
executable file
·254 lines (236 loc) · 5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/env bash
# Parse structure/repos.json for tooling and AI agents.
#
# Examples:
# parse-repos.sh names
# parse-repos.sh names --simulation
# parse-repos.sh list --format json --simulation
# parse-repos.sh get DopplerEffect
# parse-repos.sh paths --simulation --require-local
# parse-repos.sh for-each --simulation -- echo "$REPO_NAME $REPO_HOMEPAGE"
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=lib/repos.sh
source "$SCRIPT_DIR/lib/repos.sh"
usage() {
cat <<'EOF'
Usage: parse-repos.sh <command> [options]
Commands:
names Print repository names
get NAME Print one repository as JSON
list List repositories
paths Print local workspace paths
for-each Run a command for each repo (sets REPO_* env vars)
summary Print catalog summary
Options:
--type TYPE Filter by repos.json type field
--status STATUS Filter by status field (active|template|draft|wip|archived)
--lineage LINEAGE Filter by lineage (original|phet|naap)
--simulation Only repositories with isSimulation=true
--no-simulation Only repositories with isSimulation=false
--catalog PATH Override path to repos.json
--format FORMAT list output: text, tsv, json, names (default: text)
--fields FIELDS Comma-separated fields for text/tsv list output
--require-local paths: only print paths that exist on disk
-h, --help Show this help
Environment:
REPOS_JSON Path to structure/repos.json
FLEET_WORKSPACE Workspace root containing member repos
EOF
}
COMMAND=""
FORMAT="text"
FIELDS=""
GET_NAME=""
FOR_EACH_CMD=()
consume_filter_arg() {
case "$1" in
--type)
FILTER_TYPE="${2:?Missing value for --type}"
;;
--status)
FILTER_STATUS="${2:?Missing value for --status}"
;;
--lineage)
FILTER_LINEAGE="${2:?Missing value for --lineage}"
;;
--simulation)
FILTER_SIMULATION="true"
;;
--no-simulation)
FILTER_SIMULATION="false"
;;
--catalog)
REPOS_JSON="${2:?Missing value for --catalog}"
;;
--require-local)
REQUIRE_LOCAL=1
;;
*)
return 1
;;
esac
return 0
}
shift_parsed_filter_arg() {
case "$1" in
--type|--status|--lineage|--catalog)
echo 2
;;
*)
echo 1
;;
esac
}
parse_global_args() {
repos_reset_filters
while [[ $# -gt 0 ]]; do
case "$1" in
names|get|list|paths|for-each|summary)
COMMAND="$1"
shift
break
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown argument: $1" >&2
usage >&2
exit 2
;;
esac
done
if [[ -z "$COMMAND" ]]; then
usage >&2
exit 2
fi
if [[ "$COMMAND" == "get" && $# -gt 0 && "$1" != --* ]]; then
GET_NAME="$1"
shift
fi
if [[ "$COMMAND" == "for-each" ]]; then
while [[ $# -gt 0 && "$1" != "--" ]]; do
consume_filter_arg "$@" || {
echo "Unknown option for for-each: $1" >&2
exit 2
}
shift "$(shift_parsed_filter_arg "$1")"
done
[[ "${1:-}" == "--" ]] && shift
FOR_EACH_CMD=("$@")
return
fi
while [[ $# -gt 0 ]]; do
case "$1" in
--format)
FORMAT="${2:?Missing value for --format}"
shift 2
;;
--fields)
FIELDS="${2:?Missing value for --fields}"
shift 2
;;
--type|--status|--lineage|--simulation|--no-simulation|--catalog|--require-local)
consume_filter_arg "$@" || {
echo "Unknown option: $1" >&2
exit 2
}
shift "$(shift_parsed_filter_arg "$1")"
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown option: $1" >&2
usage >&2
exit 2
;;
esac
done
}
print_list_text() {
local default_fields="name,type,status,description,githubHomepage,localExists"
local fields="${FIELDS:-$default_fields}"
local field value
IFS=',' read -r -a field_array <<<"$fields"
while IFS= read -r repo; do
if [[ "$FORMAT" == "tsv" ]]; then
local values=()
for field in "${field_array[@]}"; do
if [[ "$field" == "localExists" ]]; then
value="$(jq -r '.localExists' <<<"$repo")"
else
value="$(jq -r --arg field "$field" '.[$field] // "" | if type == "array" then join(",") else tostring end' <<<"$repo")"
fi
values+=("$value")
done
local IFS=$'\t'
echo "${values[*]}"
else
local parts=()
for field in "${field_array[@]}"; do
if [[ "$field" == "localExists" ]]; then
value="$(jq -r '.localExists' <<<"$repo")"
else
value="$(jq -r --arg field "$field" '.[$field] // "" | if type == "array" then join(",") else tostring end' <<<"$repo")"
fi
parts+=("${field}=${value}")
done
local IFS=' | '
echo "${parts[*]}"
fi
done < <(repos_filtered_json_lines | repos_add_local_exists)
}
main() {
parse_global_args "$@"
case "$COMMAND" in
names)
repos_names
;;
get)
if [[ -z "$GET_NAME" ]]; then
echo "get requires a repository name" >&2
exit 2
fi
repos_get "$GET_NAME"
;;
list)
case "$FORMAT" in
json)
repos_list_json
;;
names)
repos_names
;;
text|tsv)
print_list_text
;;
*)
echo "Unknown format: $FORMAT" >&2
exit 2
;;
esac
;;
paths)
repos_paths
;;
for-each)
if [[ ${#FOR_EACH_CMD[@]} -eq 0 ]]; then
echo "for-each requires a command after --" >&2
exit 2
fi
repos_for_each "${FOR_EACH_CMD[@]}"
;;
summary)
repos_summary
;;
*)
usage >&2
exit 2
;;
esac
}
main "$@"