VPSMATE

  • HOME
  • NEWS
  • VPS
  • DEDICATED
  • ARCHIVES
  • GoDaddy
  • SCRIPT
  • TOOLS
    • Traceroute
    • Benchmark
    • Useful Sites
  • Themes
  • GFW
VPSMATE
VPS COUPON & PROMOS | VPS & DEDICATED SERVERS OFFERS | VPS REVIEWS
  1. Main page
  2. Tips
  3. Main content

One Liner to Fetch the Latest Github Release

16/09/2022 44hotness 0likes 0comments

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.

Download the latest Github release (cURL)
$ curl -s https://api.github.com/repos/<user>/<repo>/releases/latest \
  | awk -F': ' '/browser_download_url/ && /\.<file extension>/ {gsub(/"/, "", $(NF)); system("curl -LO " $(NF))}'
Download the latest Github release (Wget)
$ 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.

Download the latest Ninja release (cURL)
$ 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
Download the latest Ninja release (Wget)
$ 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))}'

1. Github Gist: One Liner to Download the Latest Release from Github Repo
2. RFC 2616

Related

Tag: github script
Last updated:16/09/2022

kyaky

This person is a lazy dog and has left nothing

Like
< Last article

Comments

Cancel

This site uses Akismet to reduce spam. Learn how your comment data is processed.

COPYRIGHT © 2021 vpsmate.net. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang