diff --git a/README.md b/README.md index 6e78115..56a3d12 100755 --- a/README.md +++ b/README.md @@ -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: @@ -82,7 +82,16 @@ 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. @@ -90,10 +99,29 @@ To perform the default installation, retrieve the installation script from the r ```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: diff --git a/install_glit.sh b/install_glit.sh index 66c2dbc..143c114 100755 --- a/install_glit.sh +++ b/install_glit.sh @@ -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" @@ -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 @@ -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 diff --git a/modules/variables.sh b/modules/variables.sh index 06911e1..240e236 100755 --- a/modules/variables.sh +++ b/modules/variables.sh @@ -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