diff --git a/src/zoxide/README.md b/src/zoxide/README.md new file mode 100644 index 000000000..b306bdcf3 --- /dev/null +++ b/src/zoxide/README.md @@ -0,0 +1,19 @@ + +# zoxide + +Install [zoxide](https://github.com/ajeetdsouza/zoxide) + +## Example Usage + +```json +"features": { + "ghcr.io/devcontainers-contrib/features/zoxide:0": {} +} +``` + +## Options + +| Options Id | Description | Type | Default Value | +|-----|-----|-----|-----| +| replaceCd | Use zoxide when calling cd (see ZOXIDE_CMD_OVERRIDE) | bool | true | +| username | For which user to setup zoxide, by default uses 'remoteUser' or 'containerUser' from config | string | - | diff --git a/src/zoxide/devcontainer-feature.json b/src/zoxide/devcontainer-feature.json new file mode 100644 index 000000000..6c699d8ea --- /dev/null +++ b/src/zoxide/devcontainer-feature.json @@ -0,0 +1,25 @@ +{ + "name": "zoxide", + "id": "zoxide", + "version": "0.1.0", + "description": "Install zoxide", + "documentationURL": "http://github.com/devcontainers-contrib/features/tree/main/src/zoxide", + "installsAfter": [], + "options": { + "replaceCd": { + "type": "boolean", + "default": "true", + "description": "Use zoxide when calling cd (see ZOXIDE_CMD_OVERRIDE)" + }, + "username": { + "type": "string", + "default": "", + "proposals": [ + "root", + "node", + "vscode" + ], + "description": "For which user to setup zoxide, by default uses 'remoteUser' or 'containerUser' from config" + } + } +} \ No newline at end of file diff --git a/src/zoxide/install.sh b/src/zoxide/install.sh new file mode 100755 index 000000000..b02ed0bba --- /dev/null +++ b/src/zoxide/install.sh @@ -0,0 +1,38 @@ +#!/usr/bin/env bash +set -x +REPLACE_CD=${REPLACECD:-""} +USERNAME=${USERNAME:-$_REMOTE_USER} + +prefix=/usr/local +shells="bash zsh" + +install_zoxide() { + curl -sSfL https://raw.githubusercontent.com/ajeetdsouza/zoxide/main/install.sh | sh -s -- --bin-dir ${prefix}/bin --man-dir ${prefix}/share/man +} + +# Install zoxide +install_zoxide + +if [ "$USERNAME" = "root" ]; then + USER_LOCATION="/root" +else + USER_LOCATION="/home/$USERNAME" +fi + +# Check available shells +for s in $shells; do + if ! command -v "$s" > /dev/null; then + shells=${shells/$s/} + fi +done + +# Configure available shells +for shell in $shells; do + [ $shell = zsh ] && dest=${ZDOTDIR:-$USER_LOCATION}/.zshrc || dest=$USER_LOCATION/.bashrc + # Set ZOXIDE_CMD_OVERRIDE + if [ "${REPLACE_CD}" = "true" ]; then + echo -e '\n# Replace cd with zoxide\nexport ZOXIDE_CMD_OVERRIDE="cd"' >> $dest + fi + + echo -e "\n# Enable zoxide\neval \"\$(zoxide init $shell)\"" >> $dest +done diff --git a/test/zoxide/scenarios.json b/test/zoxide/scenarios.json new file mode 100644 index 000000000..643798754 --- /dev/null +++ b/test/zoxide/scenarios.json @@ -0,0 +1,18 @@ +{ + "test": { + "image": "mcr.microsoft.com/devcontainers/base:debian", + "features": { + "zoxide": { + "replaceCd": true + } + } + }, + "test_no_cd": { + "image": "mcr.microsoft.com/devcontainers/base:debian", + "features": { + "zoxide": { + "replaceCd": false + } + } + } +} diff --git a/test/zoxide/test.sh b/test/zoxide/test.sh new file mode 100755 index 000000000..2173d7a12 --- /dev/null +++ b/test/zoxide/test.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +set -e + +source dev-container-features-test-lib + +# Test zoxide installed +check "zoxide installed" command -v zoxide &>/dev/null + +# Test zsh shell configured +check "ZOXIDE_CMD_OVERRIDE is set" zsh -i -c "[[ -n \"\${ZOXIDE_CMD_OVERRIDE+1}\" ]]" +check "zoxide is enabled" zsh -i -c "command -v z &>/dev/null" + +# Test bash shell configured +check "ZOXIDE_CMD_OVERRIDE is set" bash -i -c "[[ -n \"\${ZOXIDE_CMD_OVERRIDE+1}\" ]]" +check "zoxide is enabled" bash -i -c "command -v z &>/dev/null" + +reportResults diff --git a/test/zoxide/test_no_cd.sh b/test/zoxide/test_no_cd.sh new file mode 100755 index 000000000..486790a2e --- /dev/null +++ b/test/zoxide/test_no_cd.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +set -e + +source dev-container-features-test-lib + +# Test zoxide installed +check "zoxide installed" command -v zoxide + +# Test zsh shell configured +check "ZOXIDE_CMD_OVERRIDE is set" zsh -i -c "[[ -z \"\${ZOXIDE_CMD_OVERRIDE+1}\" ]]" +check "zoxide is enabled" zsh -i -c "command -v z &>/dev/null" + +# Test bash shell configured +check "ZOXIDE_CMD_OVERRIDE is set" bash -i -c "[[ -z \"\${ZOXIDE_CMD_OVERRIDE+1}\" ]]" +check "zoxide is enabled" bash -i -c "command -v z &>/dev/null" + +reportResults