$ curl -s https://api.github.com/repos/<user>/<repo>/releases/latest \
| awk -F': ' '/browser_download_url/ && /\.<file extension>/ {gsub(/"/, "", $(NF)); system("curl -LO " $(NF))}'
Sometimes, it would be awfully convenient to grab the installer for the latest release of your favorite Github project directly from the command-line. And if it could incorporate awk, that would be great too.[1] Well, the following command-line is a pretty nifty template to do just that! You will need cURL, shipped with macOS, or Wget, shipped with Linux, for this to work. Substitute in the user or organization name, the project’s name, and the desired file extension for the <user>, <repo>, and <extension> fields respectively.
$ curl -s https://api.github.com/repos/<user>/<repo>/releases/latest \
| awk -F': ' '/browser_download_url/ && /\.<file extension>/ {gsub(/"/, "", $(NF)); system("curl -LO " $(NF))}'
$ wget -q -nv -O - https://api.github.com/repos/<user>/<repo>/releases/latest \
| awk -F': ' '/browser_download_url/ && /\.<file extension>/ \
{gsub(/"/, "", $(NF)); system("wget -qi -L " $(NF))}'
Here is a quick explanation.
First, cURL or Wget obtains the response from an HTTP GET request.[2]
This response contains the URLs for the various artifacts for the latest release of the project.
Next, awk
processes the response, finding the line containing a download URL and matching the given file extension.
It then removes the quotation marks surrounding the URL and downloads the file directly with cURL or Wget.
It’s also easy enough to modify the match pattern for the file extension to make it more specific if need be. The following example demonstrates this by fetching the latest Linux release zip file of the Ninja build system.
$ curl -s https://api.github.com/repos/ninja-build/ninja/releases/latest \
| awk -F': ' '/browser_download_url/ && /linux\.zip/ {gsub(/"/, "", $(NF)); system("curl -LO " $(NF))}'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 637 100 637 0 0 1103 0 --:--:-- --:--:-- --:--:-- 1105
100 99913 100 99913 0 0 101k 0 --:--:-- --:--:-- --:--:-- 372k
$ wget -q -nv -O - https://api.github.com/repos/ninja-build/ninja/releases/latest \
| awk -F': ' '/browser_download_url/ && /linux\.zip/ \
{gsub(/"/, "", $(NF)); system("wget -qi -L " $(NF))}'