Skip to main content

🌀 Read URL

An annex allows automatically downloading the newest version of a file to which the URL is hosted on a webpage. It works as follows:

Invoke snippet (or simply pass the https://â€Ļ address using the for syntax) on the web page that hosts the URL to the file to download, provide dlink'â€Ļ' ice with the expected file-download URL replacing the version with the %VERSION% keyword, also provide as'â€Ļ' ice with one of the following values:

  1. readurl,
  2. readurl|command,
  3. readurl|completion,
  4. readurl|null.
note

The part after the | has the same meaning as in the normal as'â€Ļ' ice.

Examples​

zi id-as=fzf as='readurl|command' for \
dlink='/junegunn/fzf/releases/download/%VERSION%/fzf-%VERSION%-linux_amd64.tar.gz' \
https://github.com/junegunn/fzf/releases/

The snippet is just an example. The same effect is obtained by loading as the junegunn/fzf plugin with from'gh-r' ice.

As it can be seen, the dlink'â€Ļ' can be a relative or an absolute path and also a full URL (i.e.: beginning with the https://â€Ļ prefix).

Intermediate download page​

Sometimes, like it is in the case of the terraform command, the final download link isn't on the download page, but on a page, that's listed on it. In such a case use the dlink0'â€Ļ' ice to provide the pattern for the additional, intermediate download page, e.g.:

zi id-as=terraform as='readurl|command' extract for \
dlink0='/terraform/%VERSION%/' \
dlink='/terraform/%VERSION%/terraform_%VERSION%_linux_386.zip' \
https://releases.hashicorp.com/terraform/

Sometimes the URL of the download page differs from the URL of the archive in just a few /-sections. In such a case, it is possible to skip the dlink'â€Ļ' ice by appending a ++-separated fragment of the archive URL, like so:

zi as'readurl|command' extract for \
http://domain.com/download-page++/archive.zip

If the archive URL has some different /-sections, then it's possible to strip the conflicting ones from the download URL by using +++, ++++, etc. – the number of the /-section that'll be stripped equals to the number of the + minus 2. So, for example:

zi as'readurl|command' extract for \
http://domain.com/download-page/removed-section+++/archive.zip

Sorting the matched URLs / package versions​

Sometimes the download page doesn't list the package versions from newest to oldest, but in some other order. In such case, it's possible to sort the URLs / package versions by prepending the chosen dlink ice (dlink0'â€Ļ' or dlink'â€Ļ') with the exclamation mark (dlink'!â€Ļ', etc.). See the next section for an example:

Filtering the matched URLs​

Sometimes some unwanted URLs match the dlink'â€Ļ'/dlink0'â€Ļ' regex/pattern. In such a case it's possible to filter them out by appending a filtering regex to the dlink'â€Ļ' ice as: dlink='the-main-regex~%the-unwanted-URLs-regex%' (or the same for dlink0'â€Ļ'). An example package that can benefit from this is the Open Shift client, which doesn't sort the URLs from latest to the oldest – hence the exclamation mark (!) prepend – and it has special URLs like stable-4.4 or candidate-4.5 together with the regular version URLs (like 4.5.0-rc.1):

zi id-as"ocp" as"readurl|command" for \
dlink0'!%VERSION%~%(stable|latest|fast|candidate).*%' \
dlink"openshift-client-windows-%VERSION%.zip" \
https://mirror.openshift.com/pub/openshift-v4/clients/ocp/

The above snippet of Zsh code / Zi invocation will sort the URLs (dlink0'!â€Ļ') and then filter out the special ones from the results (via â€Ļ~%(stable|latest|fast|candidate).*%), this way selecting the latest version of the Open Shift client.

Other examples​

Pulumi, a tool to create, deploy and manage modern cloud software.

zi id-as'pulumi' as'readurl|null' for \
dlink='https://get.pulumi.com/releases/sdk/pulumi-%VERSION%-linux-x64.tar.gz' \
sbin'pulumi*' \
https://www.pulumi.com/docs/get-started/install/versions/

Install readurl​

Add the following snippet in the .zshrc file:

zi light z-shell/z-a-readurl

This will register the dlink'â€Ļ' and dlink0'â€Ļ' ice-modifiers and also the special as'readurl|â€Ļ' value of the as'â€Ļ'.