|
1 |
| -function get_headers() { |
2 |
| - local url="${1}" |
3 |
| - local headers=$(curl -sIL "${url}" | grep ':') |
4 |
| - echo "${headers[@]}" |
5 |
| -} |
| 1 | +#!/bin/bash |
6 | 2 |
|
7 | 3 | function get_header() {
|
8 |
| - local needle="^${1}: (.+)$"; shift |
9 |
| - local haystack="${@}" |
10 |
| - grep -Ee "${needle}" <<<"${haystack}" \ |
11 |
| - | sed -Ee "s/${needle}/\1/" |
| 4 | + local url="${2}" |
| 5 | + local header="${1}" |
| 6 | + local header_pattern="^${header}: (.+)$" |
| 7 | + curl -sIL "${url}" \ |
| 8 | + | sed -E \ |
| 9 | + -e "s/${header_pattern}/\1/" \ |
| 10 | + -e 'tx' -e 'd' -e ':x' |
12 | 11 | }
|
13 | 12 |
|
14 | 13 | function get_content() {
|
15 | 14 | local url="${1}"
|
16 |
| - local content=$(curl -sL "${url}") |
17 |
| - echo "${content}" |
| 15 | + curl -sL "${url}" |
18 | 16 | }
|
19 | 17 |
|
20 | 18 | function get_paged_content() {
|
21 |
| - headers=$(get_headers $1) |
22 |
| - link_header=$(get_header "Link" "${headers[@]}") |
| 19 | + local url="${1}" |
| 20 | + local link_pattern="^<(.+)>.*\"(.+)\".*<(.+)>.+$" |
23 | 21 |
|
24 |
| - link_pattern="^<(.+)>.*next.*<(.+)>.+$" |
| 22 | + local link_header |
| 23 | + local link_next |
| 24 | + local link_relation |
25 | 25 |
|
26 |
| - next=$(sed -Ee "s/${link_pattern}/\1/" <<<"${link_header}") |
27 |
| - last=$(sed -Ee "s/${link_pattern}/\2/" <<<"${link_header}") |
| 26 | + link_header=$(get_header "Link" "${url}") |
| 27 | + link_next=$(sed -Ee "s/${link_pattern}/\1/" <<<"${link_header}") |
| 28 | + link_relation=$(sed -Ee "s/${link_pattern}/\2/" <<<"${link_header}") |
28 | 29 |
|
29 |
| - if [ ! -z "${next}" ] && [ next != last ]; then |
30 |
| - printf "%s\n%s" "$(get_content $1)" "$(get_paged_content $next)" |
| 30 | + if [ ! -z "${link_next}" ] && [ "${link_relation}" = "next" ]; then |
| 31 | + printf "%s\n%s" "$(get_content "${url}")" "$(get_paged_content "${link_next}")" |
31 | 32 | else
|
32 |
| - printf "%s" "$(get_content $1)" |
| 33 | + printf "%s" "$(get_content "${url}")" |
33 | 34 | fi
|
34 | 35 | }
|
35 | 36 |
|
36 | 37 | function get_github_repos() {
|
37 | 38 | local name_pattern='.*"name": "(.+)",'
|
38 |
| - repos=($(get_paged_content https://api.github.com/orgs/docker-exec/repos \ |
| 39 | + get_paged_content https://api.github.com/orgs/docker-exec/repos \ |
39 | 40 | | grep '"name"' \
|
40 | 41 | | sed -Ee "s/${name_pattern}/\1/" \
|
41 |
| - | sort)) |
42 |
| - echo "${repos[@]}" |
| 42 | + | sort |
43 | 43 | }
|
0 commit comments