Browse code

Add query mode

Robert Cranston authored on 22/05/2026 23:28:22
Showing 2 changed files

... ...
@@ -14,9 +14,11 @@ manually updated, which means they may be out of date.
14 14
 ## Usage
15 15
 
16 16
 ```
17
-distro-url [guess] [<distro> [<variant> [<arch> [<codename> [<version> [<mirror>]]]]]]
17
+distro-url [query] [guess] [<distro> [<variant> [<arch> [<codename> [<version> [<mirror>]]]]]]
18 18
 ```
19 19
 
20
+-   `query`: Optional verbatim string, turning on query mode which will query
21
+    the [mirror][]s for each [URL][] to check that they are valid.
20 22
 -   `guess`: Optional verbatim string, switching from the default filter mode
21 23
     to guess mode. Filter mode interprets the rest of the parameters as
22 24
     [glob][]s (defaulting to `*`, matching everything) to filter an internal
... ...
@@ -35,6 +37,7 @@ distro-url [guess] [<distro> [<variant> [<arch> [<codename> [<version> [<mirror>
35 37
 
36 38
 -   [POSIX][] [shell][].
37 39
 -   `column`, optional, for formatting.
40
+-   `curl`, optional, for query mode.
38 41
 
39 42
 [POSIX]: https://en.wikipedia.org/wiki/POSIX
40 43
 [shell]: https://en.wikipedia.org/wiki/Unix_shell
... ...
@@ -58,9 +61,9 @@ ubuntu    desktop-raspi  arm64  resolute  26.04    https://cdimage.ubuntu.com/ub
58 61
 ubuntu    server-raspi   arm64  resolute  26.04    https://cdimage.ubuntu.com/ubuntu/releases/resolute/release/ubuntu-26.04-preinstalled-server-arm64+raspi.img.xz
59 62
 edubuntu  desktop-raspi  arm64  resolute  26.04    https://cdimage.ubuntu.com/edubuntu/releases/resolute/release/edubuntu-26.04-preinstalled-desktop-arm64+raspi.img.xz
60 63
 
61
-$ distro-url guess debian inst arm64 unknown 99.0.0
62
-DISTRO  VARIANT  ARCH   CODENAME  VERSION  URL
63
-debian  inst     arm64  unknown   99.0.0   https://cdimage.debian.org/debian-cd/99.0.0/arm64/iso-dvd/debian-99.0.0-arm64-DVD-1.iso
64
+$ distro-url query guess debian inst arm64 unknown 99.0.0
65
+DISTRO  VARIANT  ARCH   CODENAME  VERSION  URL                                                                                      QUERY
66
+debian  inst     arm64  unknown   99.0.0   https://cdimage.debian.org/debian-cd/99.0.0/arm64/iso-dvd/debian-99.0.0-arm64-DVD-1.iso  ERR
64 67
 ```
65 68
 
66 69
 ## License
... ...
@@ -1,13 +1,14 @@
1 1
 #!/bin/sh
2 2
 set -euC
3 3
 
4
-# usage: distro-url [guess] [<distro> [<variant> [<arch> [<codename> [<version> [<mirror>]]]]]]
4
+# usage: distro-url [query] [guess] [<distro> [<variant> [<arch> [<codename> [<version> [<mirror>]]]]]]
5 5
 
6 6
 main()
7 7
 {
8
+  [ "${1:-}" = 'query' ] && shift && query='QUERY' || query=
8 9
   [ "${1:-}" = 'guess' ] && shift && guess='GUESS' || guess=
9 10
   {
10
-    echo 'DISTRO' 'VARIANT' 'ARCH' 'CODENAME' 'VERSION' 'URL'
11
+    echo 'DISTRO' 'VARIANT' 'ARCH' 'CODENAME' 'VERSION' 'URL' $query
11 12
     [ "$guess" ] \
12 13
       && guess  "$@" \
13 14
       || filter "$@"
... ...
@@ -16,7 +17,21 @@ main()
16 17
     [ "$(command -v column)" ] \
17 18
       && column -t \
18 19
       || cat
19
-  }
20
+  } \
21
+  | while IFS= read -r line
22
+  do
23
+    printf '%s' "$line"
24
+    set -- $line
25
+    [ "$query" ] && [ "$6" != 'URL' ] && query "$6"
26
+    printf '\n'
27
+  done
28
+}
29
+
30
+query()
31
+{
32
+  curl -sfI "$1" > '/dev/null' \
33
+    && printf '%s' 'OK' \
34
+    || printf '%s' 'ERR'
20 35
 }
21 36
 
22 37
 filter()