Skip to content

Commit

Permalink
feat: add zoxide feature
Browse files Browse the repository at this point in the history
  • Loading branch information
osechet committed Aug 14, 2024
1 parent 9a1d24b commit fa05d43
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/zoxide/README.md
Original file line number Diff line number Diff line change
@@ -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 | - |
26 changes: 26 additions & 0 deletions src/zoxide/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"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": "bool",
"default": "true",
"proposals": [],
"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"
}
}
}
38 changes: 38 additions & 0 deletions src/zoxide/install.sh
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions test/zoxide/scenarios.json
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}
18 changes: 18 additions & 0 deletions test/zoxide/test.sh
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions test/zoxide/test_no_cd.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit fa05d43

Please sign in to comment.