diff --git a/src/jnv/devcontainer-feature.json b/src/jnv/devcontainer-feature.json index 52822ac..5a655d7 100644 --- a/src/jnv/devcontainer-feature.json +++ b/src/jnv/devcontainer-feature.json @@ -1,8 +1,9 @@ { "name": "jnv", "id": "jnv", - "version": "1.0.0", + "version": "1.0.1", "description": "jnv is designed for navigating JSON, offering an interactive JSON viewer and jq filter editor.", + "documentationURL": "https://github.com/ynqa/jnv", "options": { "version": { "type": "string", diff --git a/src/jnv/install.sh b/src/jnv/install.sh index dd00ca6..cf46e46 100755 --- a/src/jnv/install.sh +++ b/src/jnv/install.sh @@ -1,102 +1,130 @@ #!/usr/bin/env bash +# Variables +REPO_OWNER="ynqa" +REPO_NAME="jnv" JNV_VERSION="${VERSION:-"latest"}" -JNV_API_URL="https://api.github.com/repos/ynqa/jnv/releases" -JNV_RELEASES_URL="https://github.com/ynqa/jnv/releases" +GITHUB_API_REPO_URL="https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/releases" -set -e -x +set -e if [ "$(id -u)" -ne 0 ]; then - echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.' - exit 1 + echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.' + exit 1 fi # Clean up rm -rf /var/lib/apt/lists/* -architecture="$(uname -m)" -case ${architecture} in -x86_64) architecture="x86_64" ;; -*) - echo "(!) Architecture ${architecture} unsupported" - exit 1 - ;; +# Determine the OS and architecture +OS=$(uname -s | tr '[:upper:]' '[:lower:]') +ARCH=$(uname -m) + +case "$ARCH" in + x86_64) + ARCH="x86_64" + ;; + aarch64) + ARCH="aarch64" + ;; + armv7l) + ARCH="armv7" + ;; + *) + echo "Unsupported architecture: $ARCH" + exit 1 + ;; esac -# check if linux/windows/macOS -OS="$(uname -s)" -case ${OS} in -Linux) OS="unknown-linux-gnu" ;; -Darwin) OS="apple-darwin" ;; -*) - echo "(!) Platform ${OS} unsupported" - exit 1 - ;; +case "$OS" in + linux) + OS="unknown-linux-gnu" + ;; + *) + echo "Unsupported OS: $OS (devcontainer features only support Linux)" + exit 1 + ;; esac # Checks if packages are installed and installs them if not check_packages() { - if ! dpkg -s "$@" >/dev/null 2>&1; then - if [ "$(find /var/lib/apt/lists/* -print0 | wc -l)" = "0" ]; then - echo "Running apt-get update..." - apt-get update -y - fi - apt-get -y install --no-install-recommends "$@" - fi + if ! dpkg -s "$@" >/dev/null 2>&1; then + if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then + echo "Running apt-get update..." + apt-get update -y + fi + apt-get -y install --no-install-recommends "$@" + fi } -# Figure out correct version of a three part version number is not passed -validate_version_exists() { - local variable_name=$1 - local requested_version=$2 - if [ "${requested_version}" = "latest" ]; then requested_version=$(curl -sL ${JNV_API_URL}/latest | jq -r ".tag_name"); fi - local version_list - version_list=$(curl -sL ${JNV_API_URL} | jq -r ".[].tag_name") - if [ -z "${variable_name}" ] || ! echo "${version_list}" | grep "${requested_version}" >/dev/null 2>&1; then - echo -e "Invalid ${variable_name} value: ${requested_version}\nValid values:\n${version_list}" >&2 - exit 1 - fi - echo "${variable_name}=${requested_version}" +# Function to get the latest version from GitHub API +get_latest_version() { + curl -s "${GITHUB_API_REPO_URL}/latest" | jq -r ".tag_name" } -# make sure we have curl +# Make sure we have required packages check_packages curl tar jq ca-certificates xz-utils -# make sure version is available -if [ "${JNV_VERSION}" = "latest" ]; then JNV_VERSION=$(curl -sL ${JNV_API_URL}/latest | jq -r ".tag_name"); fi -validate_version_exists JNV_VERSION "${JNV_VERSION}" +# Check if a version is passed as an argument +if [ -z "$JNV_VERSION" ] || [ "$JNV_VERSION" == "latest" ]; then + # No version provided, get the latest version + JNV_VERSION=$(get_latest_version) + echo "No version provided or 'latest' specified, installing the latest version: $JNV_VERSION" +else + echo "Installing version from environment variable: $JNV_VERSION" +fi -# download and install binary -JNV_FILENAME="jnv-${architecture}-${OS}.tar.xz" +# Construct the download URL +DOWNLOAD_URL="https://github.com/${REPO_OWNER}/${REPO_NAME}/releases/download/${JNV_VERSION}/jnv-${ARCH}-${OS}.tar.xz" -url="${JNV_RELEASES_URL}/download/${JNV_VERSION}/${JNV_FILENAME}" -echo "Downloading ${url}..." -curl -sSL "$url" -o "${JNV_FILENAME}" -if [ $? -ne 0 ]; then - echo "Failed to download ${url}" - exit 1 -fi +# Create a temporary directory for the download +TMP_DIR=$(mktemp -d) +cd "$TMP_DIR" || exit + +echo "Downloading jnv from $DOWNLOAD_URL" +curl -sSL "$DOWNLOAD_URL" -o "jnv.tar.xz" || { + echo "Failed to download $DOWNLOAD_URL" + exit 1 +} -# Custom steps for jnv -# unxz jnv-x86_64-unknown-linux-gnu.tar.xz -# tar xf jnv-x86_64-unknown-linux-gnu.tar -# mv jnv-x86_64-unknown-linux-gnu/jnv /usr/local/bin/jnv -# rm -rf jnv-x86_64-unknown-linux-gnu -# rm jnv-x86_64-unknown-linux-gnu.tar +# Extract the archive +echo "Extracting jnv..." +unxz "jnv.tar.xz" || { + echo "Failed to decompress jnv.tar.xz" + exit 1 +} -unxz "${JNV_FILENAME}" -tar xf "${JNV_FILENAME%.xz}" -install -m 555 "${JNV_FILENAME%.tar.xz}/jnv" /usr/local/bin/jnv +tar -xf "jnv.tar" || { + echo "Failed to extract jnv.tar" + exit 1 +} -if [ $? -ne 0 ]; then - echo "Failed to extract ${JNV_FILENAME}" - exit 1 +# Find the extracted directory (it should match the archive name pattern) +EXTRACTED_DIR=$(find . -name "jnv-${ARCH}-${OS}" -type d | head -1) +if [ -z "$EXTRACTED_DIR" ]; then + echo "ERROR: Could not find extracted jnv directory" + exit 1 fi -rm -rf "${JNV_FILENAME%.tar.xz}" -rm "${JNV_FILENAME%.xz}" +# Move the binary to /usr/local/bin +echo "Installing jnv..." +install -m 755 "${EXTRACTED_DIR}/jnv" /usr/local/bin/jnv || { + echo "Failed to install jnv binary" + exit 1 +} + +# Cleanup +cd - || exit +rm -rf "$TMP_DIR" # Clean up -# rm -rf /var/lib/apt/lists/* +rm -rf /var/lib/apt/lists/* + +# Verify installation +echo "Verifying installation..." +jnv --version || { + echo "Installation verification failed" + exit 1 +} echo "Done!" diff --git a/test/_global/jnv-specific-version.sh b/test/_global/jnv-specific-version.sh index 4ae80ee..6f163c4 100644 --- a/test/_global/jnv-specific-version.sh +++ b/test/_global/jnv-specific-version.sh @@ -2,6 +2,6 @@ set -e source dev-container-features-test-lib -check "jnv with specific version" /bin/bash -c "jnv -V | grep '0.1.2'" +check "jnv with specific version" /bin/bash -c "jnv -V | grep '0.6.1'" reportResults diff --git a/test/_global/scenarios.json b/test/_global/scenarios.json index f2545e1..fdd03ad 100644 --- a/test/_global/scenarios.json +++ b/test/_global/scenarios.json @@ -98,7 +98,7 @@ "image": "mcr.microsoft.com/devcontainers/base:ubuntu", "features": { "jnv": { - "version": "v0.1.2" + "version": "v0.6.1" } } },