Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -u / --upgrade to update to latest release #150

Merged
merged 4 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,39 @@ Following command will create `wgclient_client_name.conf` file.

./easy-wg-quick client_name

### Special usage

#### -h / --help

Displays help for all supported arguments.

#### -i / --init

Writes the initial configuration to text files without creating a client or
hub configuration. This option is useful if you want to change auto-detected
or default configuration values. Repeat the script with the client name to
create the final configuration.

#### -c / --clear

To start over, manually remove all `*.bak`, `*.conf`, `*.key`, and `*.psk`
files. If you need to remove initial configuration options as well, also
delete all `*.txt` files. This script does not remove anything.

#### -d / --install-wg-quick

This option will download and install the wg-quick script from the official
Wireguard GitHub mirror based on the current operating system (Linux, FreeBSD,
OpenBSD and Darwin are supported).

If run as `root`, it will install `wg-quick` in `/usr/local/sbin`. If run as
a normal user, it will use `$HOME/.local/bin`.

#### -u / --upgrade

This will download the latest release of that script and replace the original
file with the downloaded version.

### Sample output

```
Expand Down
44 changes: 43 additions & 1 deletion easy-wg-quick
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh
# easy-wg-quick - Creates WireGuard configuration for hub and peers with ease
# Copyright (C) 2019-2023 Krzysztof Burghardt <[email protected]>
# Copyright (C) 2019-2024 Krzysztof Burghardt <[email protected]>
#
#
# License
Expand Down Expand Up @@ -691,11 +691,49 @@ download_and_install_wg_quick() {
}
}

upgrade_easy_wg_quick() {
REPO='burghardt/easy-wg-quick'
APIURL="https://api.github.com/repos/${REPO}/releases/latest"

APIDATA="$(curl -sL "${APIURL}" ||
wget -qO- "${APIURL}" ||
fetch -qo- "${APIURL}")"
TAG="$(echo "${APIDATA}" | grep -oP '"tag_name": "\K(.*?)(?=")')"
DLURL="https://raw.githubusercontent.com/${REPO}/${TAG}/easy-wg-quick"

DLFILE=$(mktemp -qt 'wg-quick.XXXXXXXXXX') || {
echo "Failed to create temporary file."
exit 1
}
cleanup() {
# until https://github.com/koalaman/shellcheck/issues/2660 resolved
# shellcheck disable=SC2317
rm -f "${DLFILE}"
}
trap cleanup EXIT

curl -sL "${DLURL}" -o "${DLFILE}" > /dev/null 2>&1 ||
wget -qO "${DLFILE}" "${DLURL}" > /dev/null 2>&1 ||
fetch -q -o "${DLFILE}" "${DLURL}" > /dev/null 2>&1 || {
echo "Download of ${DLURL} failed."
exit 1
}

echo "Installing easy-wg-quick ${TAG} to $0."
chmod +x "${DLFILE}"
mv "${DLFILE}" "$0" || {
echo "Failed to install ${DLFILE} as $0."
exit 1
}
true
}

print_usage() {
echo "Usage: $0 [client_name] - create new client with optional [client_name]"
echo " $0 -i / --init - create initial configuration without any clients"
echo " $0 -c / --clear - clear the configuration and start over"
echo " $0 -d / --install-wg-quick - download and install wg-quick script"
echo " $0 -u / --upgrade - update $0 from latest GitHub release"
exit 1
}

Expand Down Expand Up @@ -725,6 +763,10 @@ main() {
download_and_install_wg_quick
exit 0
;;
"-u" | "--upgrade")
upgrade_easy_wg_quick
exit 0
;;
*)
CONF_NAME="$1"
;;
Expand Down
20 changes: 18 additions & 2 deletions tests/arguments.bats
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ load teardown setup
@test "run with -d parameter" {
run ../easy-wg-quick -d
[[ "$status" -eq 0 ]]
[[ "${#lines[@]}" -ge 0 ]]
[[ "${#lines[@]}" -ge 1 ]]

if [[ "$(id -u)" -eq 0 ]]; then
[[ -x /usr/local/sbin/wg-quick ]]
Expand All @@ -86,7 +86,7 @@ load teardown setup
@test "run with --install-wg-quick parameter" {
run ../easy-wg-quick --install-wg-quick
[[ "$status" -eq 0 ]]
[[ "${#lines[@]}" -ge 0 ]]
[[ "${#lines[@]}" -ge 1 ]]

if [[ "$(id -u)" -eq 0 ]]; then
[[ -x /usr/local/sbin/wg-quick ]]
Expand All @@ -95,6 +95,22 @@ load teardown setup
fi
}

@test "run with -u parameter" {
cp ../easy-wg-quick ./easy-wg-quick-to-upgrade
run ./easy-wg-quick-to-upgrade -u
[[ "$status" -eq 0 ]]
[[ "${#lines[@]}" -ge 1 ]]
rm ./easy-wg-quick-to-upgrade
}

@test "run with --upgrade parameter" {
cp ../easy-wg-quick ./easy-wg-quick-to-upgrade
run ./easy-wg-quick-to-upgrade --upgrade
[[ "$status" -eq 0 ]]
[[ "${#lines[@]}" -ge 1 ]]
rm ./easy-wg-quick-to-upgrade
}

@test "run with too many parameters" {
run ../easy-wg-quick foo bar
[[ "$status" -eq 1 ]]
Expand Down