-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathopenapi_utils.sh
More file actions
240 lines (215 loc) · 5.32 KB
/
openapi_utils.sh
File metadata and controls
240 lines (215 loc) · 5.32 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
#!/usr/bin/env bash
# WARN: WIP!!!
openapi.filter() {
local deps=(yq jq)
for d in "${deps[@]}"; do
command -v "$d" >/dev/null 2>&1 || {
echo "Error: dependency missing: $d" >&2
return 127 2>/dev/null || exit 127
}
done
local dryrun=0
local showhelp=0
local input=""
local output=""
local usage="$(cat <<-EOF
Usage:
${FUNCNAME[0]} [OPTIONS] [-i] <INPUT> [-o] [<OUTPUT>] [-p] [PATHS...]
Filter OpenAPI spec by paths, keeping only referenced schemas.
Options:
[-i, --input] INPUT Input OpenAPI YAML file.
[-o, --output] OUTPUT (Optional) Output filtered YAML file. If not specified - prints modified YAML to stdout.
[-p, --paths] PATHS Paths to include (optional, can use --paths).
-n, --dry-run Show what would be done without executing.
-h, --help Show help.
Without markers parameter indexed by relative position.
With markers - in any order.
Examples:
${FUNCNAME[0]} -i openapi.yaml -o filtered.yaml /users /posts
${FUNCNAME[0]} -n -i openapi.yaml /users
EOF
)"
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help) showhelp=1; shift ;;
-n|--dry-run) dryrun=1; shift ;;
# TODO: Verify if this won't break anything
--)
shift
break
;;
# TODO: Verify if this won't break anything
-*)
echo "Error: Unknown option: $1" >&2
echo "$usage" >&2
return 1
;;
# TODO: Verify if this won't break anything
*)
break
;;
esac
done
(( showhelp )) && { echo "$usage"; return 0; }
local -a paths=()
local use_markers=0
# First pass: check if any markers are used
for arg in "$@"; do
case "$arg" in
-i|--input|-o|--output|-p|--paths) use_markers=1; break ;;
esac
done
# Parse arguments
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help) showhelp=1; shift ;;
-n|--dry-run) dryrun=1; shift ;;
-i|--input)
input="$2"
shift 2
;;
-o|--output)
output="$2"
shift 2
;;
-p|--paths)
shift
while [[ $# -gt 0 ]] && [[ ! "$1" =~ ^- ]]; do
paths+=("$1")
shift
done
;;
--)
shift
break
;;
-*)
echo "Error: Unknown option: $1" >&2
echo "$usage" >&2
return 1
;;
# TODO: Verify algorithm logic
*)
if (( use_markers )); then
# With markers, positional args after options are paths
paths+=("$1")
shift
else
# Without markers: positional by index
break
fi
;;
esac
done
(( showhelp )) && { echo "$usage"; return 0; }
# Handle positional arguments (when no markers used)
if (( ! use_markers )); then
# Positional: INPUT [OUTPUT] [PATHS...]
if [[ $# -lt 1 ]]; then
echo "Error: Missing required argument: INPUT file" >&2
echo "$usage" >&2
return 1
fi
input="$1"
shift
if [[ $# -gt 0 ]] && [[ ! "$1" =~ ^- ]]; then
output="$1"
shift
fi
# Remaining args are paths
while [[ $# -gt 0 ]]; do
paths+=("$1")
shift
done
fi
# Validate input
[[ -z "$input" ]] && {
echo "Error: Missing required argument: INPUT file" >&2
echo "$usage" >&2
return 1
}
[[ ! -f "$input" ]] && {
echo "Error: Input file not found: $input" >&2
return 1
}
[[ ! -r "$input" ]] && {
echo "Error: Cannot read input file: $input" >&2
return 1
}
# Validate output directory if output specified
if [[ -n "$output" ]]; then
local outdir
outdir="$(dirname "$output")"
[[ ! -d "$outdir" ]] && {
echo "Error: Output directory does not exist: $outdir" >&2
return 1
}
[[ ! -w "$outdir" ]] && {
echo "Error: Cannot write to output directory: $outdir" >&2
return 1
}
fi
# Validate paths
if [[ ${#paths[@]} -eq 0 ]]; then
echo "Error: No paths specified to filter" >&2
return 1
fi
local paths_json
paths_json="$(printf '%s\n' "${paths[@]}" | jq -R . | jq -s .)" || {
echo "Error: Failed to process paths with jq" >&2
return 1
}
run_filter() {
if [[ -n "$output" ]]; then
yq eval "
def refs:
.. | select(has(\"\\\$ref\")) | .\"\\\$ref\" |
select(test(\"^#/components/schemas/\")) |
sub(\"^#/components/schemas/\"; \"\");
. as \$doc
| (\$doc.paths
| with_entries(select(.key as \$k | $paths_json | index(\$k))))
as \$filtered_paths
| (\$filtered_paths | refs | unique) as \$schemas
| \$doc
| .paths = \$filtered_paths
| .components.schemas |=
with_entries(select(.key as \$k | \$schemas | index(\$k)))
" "$input" > "$output"
else
yq eval "
def refs:
.. | select(has(\"\\\$ref\")) | .\"\\\$ref\" |
select(test(\"^#/components/schemas/\")) |
sub(\"^#/components/schemas/\"; \"\");
. as \$doc
| (\$doc.paths
| with_entries(select(.key as \$k | $paths_json | index(\$k))))
as \$filtered_paths
| (\$filtered_paths | refs | unique) as \$schemas
| \$doc
| .paths = \$filtered_paths
| .components.schemas |=
with_entries(select(.key as \$k | \$schemas | index(\$k)))
" "$input"
fi
}
if (( dryrun )); then
echo "DRY-RUN: Would filter '$input'"
if [[ -n "$output" ]]; then
echo "DRY-RUN: Output -> '$output'"
else
echo "DRY-RUN: Output -> stdout"
fi
echo "DRY-RUN: Paths to include:"
printf '%s\n' "${paths[@]}" | sed 's/^/ - /'
return 0
else
run_filter || {
echo "Error: Failed to filter OpenAPI spec" >&2
return 1
}
[[ -n "$output" ]] && echo "Filtered spec written to: $output"
fi
}
export -f openapi.filter