Skip to content

Commit

Permalink
🔨 (glit): Update installation script to target releases (#16)
Browse files Browse the repository at this point in the history
* 🔨 (install_glit): Update installation script

Updates the installation script to target GitHub releases, rather than cloning main branch.

* 🦺 (install_glit): Check RELEASE_URL is not empty

* 🦺 (install_script): Add a rudimentary not-root check'

* 📝 (README): Update docs on install script usage

* ✨ (glit): Bump version to initial beta
  • Loading branch information
justJackjon authored Oct 10, 2023
1 parent 31cc151 commit 4be367f
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 10 deletions.
34 changes: 31 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ It is important to recognise that every developer has their own unique workflow,

## Installation

To install `glit`, execute the `install_glit.sh` script. The script offers two installation modes. Note that `sudo` or administrative privileges may be necessary for installation.
To install `glit`, execute the `install_glit.sh` script. The script offers various installation modes. Note that `sudo` or administrative privileges may be necessary for installation.

The installation script will:

Expand All @@ -82,18 +82,46 @@ The installation script will:
- Generate a symlink to the main `glit` script in `/usr/local/bin/glit`.
- Assign the necessary permissions.

### Default Installation
### Installation Options

- **`remote`**: Fetch and install `glit` from the remote GitHub repository. This is the default mode.
- **`local`**: Install `glit` from a local directory. Defaults to the parent of the current directory, however a path can be specified after the `local` keyword.
- **`-v, --version`**: Specify a version of `glit` to install in the `remote` mode. Accepts a version identifier like `v1.2.3`.
- **`-u, --unattended`**: Enables unattended installation. In this mode, the script will bypass any prompts.

### Installation Examples

#### Default Installation

To perform the default installation, retrieve the installation script from the remote repository and execute it with bash. Administrative privileges may be necessary, and if so, prefix the command with `sudo`. Whilst this is the easiest way to get started with `glit`, please heed the security warning below.

> :warning: **Security Warning:** Before executing scripts, especially when piping into `sudo` `bash` from remote locations, always verify the trustworthiness of the source. Thoroughly reviewing the script content to understand its actions is crucial. Exercising this level of caution is a foundational security best practice, regardless of the tool or script in question.
```bash
# WARNING: The following code retrieves a remote script and pipes it into sudo bash.
# NOTE: This will install the latest version from GitHub.
curl -sSL https://raw.githubusercontent.com/justjackjon/glit/main/install_glit.sh | sudo bash
```

### Local Installation
#### Specific Version Installation

If you want to install a specific version of `glit`, you can do so by specifying the version using the `-v` or `--version` flags.

```bash
# NOTE: Replace `vX.X.X` with the desired version, e.g., `v1.2.3`.
curl -sSL https://raw.githubusercontent.com/justjackjon/glit/main/install_glit.sh | sudo bash -s -- -v vX.X.X
```

#### Unattended Installation

For unattended installations (e.g., as part of an automation or CI/CD process), you can use the `-u` or `--unattended` flags. This will bypass any prompts.

```bash
# NOTE: Replace `vX.X.X` with the desired version, e.g., `v1.2.3`.
curl -sSL https://raw.githubusercontent.com/justjackjon/glit/main/install_glit.sh | sudo bash -s -- -u -v vX.X.X
```

#### Local Installation

If the repository has already been cloned locally, execute the installation script from the root of the repository:

Expand Down
85 changes: 79 additions & 6 deletions install_glit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,65 @@
# Instruct bash to immediately exit if any command has a non-zero exit status
set -e

# Retrieve installation mode and local path from command line arguments or use default values
INSTALL_MODE="$1"
LOCAL_PATH="${2:-$(dirname "$(pwd)")}"
# Default values
INSTALL_MODE="remote"
LOCAL_PATH="$(dirname "$(pwd)")"
VERSION="latest"
UNATTENDED=false

# Argument parsing
while (( "$#" )); do
case "$1" in
local)
INSTALL_MODE="local"
shift
;;
remote)
INSTALL_MODE="remote"
shift
;;
-v|--version)
VERSION="$2"
shift 2
;;
-u|--unattended)
UNATTENDED=true
shift
;;
*)
LOCAL_PATH="$1"
shift
;;
esac
done

GH_API_ENDPOINT="https://api.github.com/repos/justJackjon/glit"
GLIT_REPO_URL="https://github.com/justJackjon/glit"
GLIT_DIR="/opt/glit"
SYMLINK_PATH="/usr/local/bin/glit"
DEPENDENCIES=("curl" "git" "rsync" "uname" "realpath")

if [[ "$INSTALL_MODE" == "remote" ]]; then
TEMP_DIR=$(mktemp -d)
fi

# Quick sanity check on the following variables as we rm -rf them later.
if [[ "$GLIT_DIR" == "/" ]] || [[ "$TEMP_DIR" == "/" ]]; then
echo -e "\nInvalid value for GLIT_DIR or TEMP_DIR. Aborting."

exit 1
fi

cleanup() {
echo -e "\n\nInstallation aborted. Cleaning up temporary files..."
rm -rf "$TEMP_DIR"

exit 1
}

# NOTE: Only trap on ERR, INT and TERM signals. TEMP_DIR is explicitly removed on successful EXIT.
trap cleanup ERR INT TERM

HEAVY_CHECK_MARK="\u2714"
RED="\e[31m"
GREEN="\e[32m"
Expand Down Expand Up @@ -41,9 +91,8 @@ print() {
ask_should_reinstall() {
print info "\`glit\` is already installed."

if [[ ! -t 0 ]]; then
if $UNATTENDED || [[ ! -t 0 ]]; then
print info "Running in non-interactive mode. Assuming 'yes' for reinstall."
echo ""

return 0
fi
Expand Down Expand Up @@ -97,7 +146,31 @@ if [[ "$INSTALL_MODE" == "local" ]]; then

cp -r "$LOCAL_PATH/glit" "$GLIT_DIR"
else
git clone "$GLIT_REPO_URL" "$GLIT_DIR"
# NOTE: Using grep and sed to parse JSON so we don't add a dependency on `jq`
if [[ "$VERSION" == "latest" ]]; then
RELEASE_URL=$(curl -s "$GH_API_ENDPOINT/releases/latest" | grep tarball_url | sed 's/.*: "\(.*\)",/\1/')
else
RELEASE_URL=$(curl -s "$GH_API_ENDPOINT/releases/tags/$VERSION" | grep tarball_url | sed 's/.*: "\(.*\)",/\1/')
fi

# Check if RELEASE_URL is empty, which might be due to an invalid or non-existent tag
if [[ -z "$RELEASE_URL" ]]; then
print error "Failed to retrieve the release URL for version '$VERSION'. Please ensure the provided version exists."

exit 1
fi

print info "Downloading \`glit\` from $RELEASE_URL...\n"

curl -L "$RELEASE_URL" -o "$TEMP_DIR/glit.tar.gz"
tar -xzf "$TEMP_DIR/glit.tar.gz" -C "$TEMP_DIR"

mv $TEMP_DIR/justJackjon-glit-* "$GLIT_DIR"

# NOTE: The cleanup trap will remove temp files on ERR, INT and TERM signals, but not on [successful] EXIT.
rm -rf "$TEMP_DIR"

print success "Temporary files have been cleaned up"
fi

# Create a symlink to the main `glit` script
Expand Down
2 changes: 1 addition & 1 deletion modules/variables.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION="v0.0.1-alpha"
VERSION="v0.1-beta.0"
RELEASE_DATE="October 10, 2023"
FORCE_ACTION=0
AUTO_CONFIRM=0
Expand Down

0 comments on commit 4be367f

Please sign in to comment.