Skip to content

Commit

Permalink
fix: zsh syntax bug & format markdown (#12)
Browse files Browse the repository at this point in the history
Signed-off-by: Vladislav Doster <mvdoster@gmail.com>
  • Loading branch information
vladdoster authored Jul 9, 2022
1 parent f97ab9d commit 65cc56e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 83 deletions.
136 changes: 65 additions & 71 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,103 +2,67 @@

<!-- mdformat-toc start --slug=github --maxlevel=6 --minlevel=2 -->

- [Installation](#installation)
- [Summary](#summary)
- [Sorting matched URLs](#sorting-matched-urls)
- [Filtering The Matched URLs](#filtering-the-matched-urls)
- [Intermediate Download Page](#intermediate-download-page)
- [Usage](#usage)
- [Skipping `dlink''` Ice](#skipping-dlink-ice)
- [Other Examples](#other-examples)
- [Sorting matched URLs/versions](#sorting-matched-urlsversions)
- [Filtering The Matched URLs](#filtering-the-matched-urls)
- [Examples](#examples)

<!-- mdformat-toc end -->

A Zinit extension to automatically download the newest version of a file to which the URL is hosted
on a webpage.

It works as follows:

- Invoke `snippet` (or pass the `http://…` 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,
- Provide `as''` ice with one of the following values: `readurl`, `readurl|command`,
`readurl|completion`, `readurl|null`; the part after the `|` has the same meaning as in the normal
`as''` ice.
## Usage<a name="usage"></a>

For example:
Load as a regular plugin via:

```zsh
zinit for \
as'readurl|command' \
dlink'/junegunn/fzf-bin/releases/download/%VERSION%/fzf-%VERSION%-linux_amd64.tgz'
extract \
id-as'fzf'
https://github.com/junegunn/fzf-bin/releases/
```

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

As demonstrated, the `dlink''` can be a relative or an absolute path and also a full URL (i.e.:
beginning with the `http://…` prefix).

## Installation<a name="installation"></a>

Load as a regular plugin, via:

```zsh
zinit light zdharma-continuum/zinit-annex-readurl
zinit light zdharma-continuum/z-a-readurl
```

After executing the above command (possibly via `zshrc`), it's then possible to use the `dlink''`
and `dlink0''` ices and the special `as'readurl|…'` value of the `as''` ice.

## Summary<a name="summary"></a>
This Zinit extension allows to automatically download the newest version of a file to which the URL
is hosted on a webpage.

The annex provides:

1. Two new ices: `dlink''` and `dlink0''`.
2. A handling of the special values of the `as''` ice, i.e.: of `as'readurl'`,
`as'readurl|command'`, etc.

The annex works only with snippets, not plugins.
> **Note**: The annex works only with snippets, not plugins.
## Sorting matched URLs<a name="sorting-matched-urls"></a>

Sometimes the download page doesn't list the package versions from newest to the 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.
It works as follows:

## Filtering The Matched URLs<a name="filtering-the-matched-urls"></a>
- invoke `snippet` (or pass the `http://…` 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: `readurl`, `readurl|command`,
`readurl|completion`, `readurl|null`; the part after the `|` has the same meaning as in the normal
`as''` ice.

Sometimes some unwanted URLs match the `dlink''`/`dlink0''` regex/pattern. In such 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](https://www.openshift.com/) 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`):
So, for example:

```zsh
````zsh
zinit for \
as'readurl|command' \
dlink'openshift-client-windows-%VERSION%.zip' \
dlink0'!%VERSION%~%(stable|latest|fast|candidate).*%' \
id-as'ocp' \
https://mirror.openshift.com/pub/openshift-v4/clients/ocp/
dlink'/junegunn/fzf-bin/releases/download/%VERSION%/fzf-%VERSION%-linux_amd64.tgz'
extract \
id-as'fzf'
https://github.com/junegunn/fzf-bin/releases/
```
The snippet is just an example. The same effect is obtained by loading as `junegunn/fzf-bin` plugin
with `from'gh-r'` ice.

The above snippet of Zsh code / Zinit 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.
As demonstrated, the `dlink''` can be a relative or an absolute path and also a full URL (i.e.:
beginning with the `http://…` prefix).

## Intermediate Download Page<a name="intermediate-download-page"></a>
## Intermediate Download Page

Sometimes, like in the case of the [terraform](http://releases.hashicorp.com/terraform) command, the
download link isn't on the download page but on a page listed on it. In such cases utilize the
`dlink0''` ice and provide the pattern for the additional, intermediate download page. For For
example, in the case of `terraform`, the Zinit command is:
Sometimes, like in the case of the [terraform](http://releases.hashicorp.com/terraform) command, the download link isn't on the download page but on a page listed on it. In such cases utilize
the `dlink0''` ice and provide the pattern for the additional, intermediate download page. For
For example, in the case of `terraform`, the Zinit command is:
```zsh
zinit for \
Expand All @@ -108,7 +72,7 @@ zinit for \
extract \
id-as'terraform' \
http://releases.hashicorp.com/terraform/
```
````
## Skipping `dlink''` Ice<a name="skipping-dlink-ice"></a>
Expand All @@ -134,12 +98,42 @@ zinit for \
http://domain.com/download-page/removed-section+++/archive.zip
```
## Other Examples<a name="other-examples"></a>
## Sorting matched URLs/versions<a name="sorting-matched-urlsversions"></a>
[**Pulumi**](https://www.pulumi.com/), a tool to create, deploy, and manage modern cloud software.
Sometimes the download page doesn't list the package versions from newest to the 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<a name="filtering-the-matched-urls"></a>
Sometimes some unwanted URLs match the `dlink''`/`dlink0''` regex/pattern. In such 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](https://www.openshift.com/) 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`):
```zsh
zinit for \
as'readurl|command' \
dlink'openshift-client-windows-%VERSION%.zip' \
dlink0'!%VERSION%~%(stable|latest|fast|candidate).*%' \
id-as'ocp' \
https://mirror.openshift.com/pub/openshift-v4/clients/ocp/
```
The above snippet of Zsh code / Zinit 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.
## Examples<a name="examples"></a>
[**Pulumi**](https://www.pulumi.com/), a tool to create, deploy, and manage modern cloud software.
```zsh
zi for \
as'readurl|null' \
dlink'https://get.pulumi.com/releases/sdk/pulumi-%VERSION%-linux-x64.tar.gz' \
extract'!' \
Expand Down
20 changes: 8 additions & 12 deletions za-readurl-preinit-handler
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

#!/usr/bin/env zsh
# -*- mode: sh; sh-indentation: 4; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
# -*- mode: sh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
# vim:ft=zsh:sw=2:sts=2:et:foldmarker=[[[,]]]:foldmethod=marker
#
# Copyright (c) 2016-2020 Sebastian Gniazdowski and contributors
# Copyright (c) 2021-2022 zdharma-continuum and contributors
Expand Down Expand Up @@ -159,20 +159,18 @@ za-readurl-preinit-handler() {

if [[ -z $selected ]] {
+zinit-message "{pre}readurl annex: {error}ERROR:{rst}" \
"couldn't match the URL${${(M)cur_paturl:#$pattern_url0}:+-0} at the download page" \
"(which is {url}${dlpage//\%/%%}{rst}${${__id_as:#$__url}:+; the snippet is being identified as" \
"{meta}${__id_as}{rst}}; was matching {data2}${cur_paturl}{rst})."
"No valid URLs (${${(M)cur_paturl:#$pattern_url0}:+-0}) when searching {url}${dlpage//\%/%%}{rst}${${__id_as:#$__url}:+ --" \
"the snippet is being identified as {meta}${__id_as}{rst}}; was matching {data2}${cur_paturl}{rst})."
return 9
}

if [[ $selected = /* ]] {
if [[ $selected = /* ]]; then
local domain protocol
if [[ $dlpage = (#b)(#i)((http(s|)|ftp(s|)|ssh|scp)://|)([^/]##)(*) ]] {
protocol=$match[1] domain=$match[5]
}
if [[ -z $domain ]] {
+zinit-message "{pre}readurl annex: {ehi}ERROR:{error} couldn't establish the domain name (unsupported" \
"protocol? supported are: {data}http(s){error},{data}ftp(s){error},{data}ssh{error},{data}scp{error}).{rst}"
+zinit-message "{pre}readurl annex: {ehi}ERROR:{error} couldn't establish the domain name (unsupported protocol? supported are: {data}http(s){error},{data}ftp(s){error},{data}ssh{error},{data}scp{error}).{rst}"
return 9
}
local new_url=${protocol:-http://}$domain$selected
Expand All @@ -197,10 +195,10 @@ za-readurl-preinit-handler() {
fi

# Handle the update
if [[ $__subtype == update(|:*) && -z $opts[(r)-f] ]] {
if [[ $__subtype == update(|:*) && -z $opts[(r)-f] ]]; then
{ local old_url="$(<$__dir/._zinit/url_rsvd)"; } 2>/dev/null
[[ $old_url == $new_url ]] && return 8
}
fi

# Assign outer-scope parameters
url=$new_url
Expand All @@ -214,5 +212,3 @@ za-readurl-preinit-handler() {
+zinit-message "{pre}readurl annex: {msg}Matched the following URL: {url}${new_url}{rst}"
return 0
}

# vim:ft=zsh:sw=2:sts=2:et:foldmarker=[[[,]]]:foldmethod=marker

0 comments on commit 65cc56e

Please sign in to comment.