diff --git a/src/bitwarden-cli-persistence/devcontainer-feature.json b/src/bitwarden-cli-persistence/devcontainer-feature.json new file mode 100644 index 0000000..a284a9f --- /dev/null +++ b/src/bitwarden-cli-persistence/devcontainer-feature.json @@ -0,0 +1,19 @@ +{ + "id": "bitwarden-cli-persistence", + "version": "0.1.0", + "name": "Bitwarden CLI Persistence", + "description": "Avoid extra logins from the Bitwarden CLI by preserving the `~/.config/Bitwarden CLI` folder across container instances.", + "documentationURL": "https://github.com/flokoe/devcontainer-features/tree/main/src/bitwarden-cli-persistence", + "licenseURL": "https://github.com/flokoe/devcontainer-features/blob/main/LICENSE", + "options": {}, + "mounts": [ + { + "source": "${devcontainerId}-bitwarden-cli", + "target": "/dc/bitwarden-cli", + "type": "volume" + } + ], + "installsAfter": [ + "ghcr.io/devcontainers-contrib/features/bitwarden-cli" + ] +} diff --git a/src/bitwarden-cli-persistence/install.sh b/src/bitwarden-cli-persistence/install.sh new file mode 100644 index 0000000..3957f5a --- /dev/null +++ b/src/bitwarden-cli-persistence/install.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash + +set -e + +if [ -z "$_REMOTE_USER" ] || [ -z "$_REMOTE_USER_HOME" ]; then + echo "***********************************************************************************" + echo "*** Require _REMOTE_USER and _REMOTE_USER_HOME to be set (by dev container CLI) ***" + echo "***********************************************************************************" + exit 1 +fi + +# Ensure ~/.config exists to prevent tests failing if root user +mkdir -p "$_REMOTE_USER_HOME/.config" + +# Create /dc/bitwarden-cli folder if doesn't exist. +mkdir -p "/dc/bitwarden-cli" + +# As to why we move around the folder, check `github-cli-persistence/install.sh` @github.com/joshuanianji/devcontainer-features +if [ -d "$_REMOTE_USER_HOME/.config/Bitwarden CLI" ]; then + echo "Moving existing '~/.config/Bitwarden CLI' folder to '~/.config/Bitwarden CLI OLD'" + mv "$_REMOTE_USER_HOME/.config/Bitwarden CLI" "$_REMOTE_USER_HOME/.config/Bitwarden CLI OLD" +fi + +ln -s /dc/bitwarden-cli "$_REMOTE_USER_HOME/.config/Bitwarden CLI" + +# Change owner of folder. +chown -R "${_REMOTE_USER}:${_REMOTE_USER}" "$_REMOTE_USER_HOME/.config/Bitwarden CLI" + +# chown mount (only attached on startup) +cat <>"$_REMOTE_USER_HOME/.bashrc" +sudo chown -R "${_REMOTE_USER}:${_REMOTE_USER}" /dc/bitwarden-cli +EOF +chown -R "$_REMOTE_USER" "$_REMOTE_USER_HOME/.bashrc" + +echo "done!" diff --git a/test/bitwarden-cli-persistence/scenarios.json b/test/bitwarden-cli-persistence/scenarios.json new file mode 100644 index 0000000..3c5cb2b --- /dev/null +++ b/test/bitwarden-cli-persistence/scenarios.json @@ -0,0 +1,9 @@ +{ + "with_vscode": { + "image": "mcr.microsoft.com/devcontainers/base:bookworm", + "features": { + "bitwarden-cli-persistence": {}, + "ghcr.io/devcontainers-contrib/features/bitwarden-cli": {} + } + } +} diff --git a/test/bitwarden-cli-persistence/test.sh b/test/bitwarden-cli-persistence/test.sh new file mode 100644 index 0000000..4a12435 --- /dev/null +++ b/test/bitwarden-cli-persistence/test.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash + +set -e + +# Optional: Import test library bundled with the devcontainer CLI +# See https://github.com/devcontainers/cli/blob/HEAD/docs/features/test.md#dev-container-features-test-lib +# Provides the 'check' and 'reportResults' commands. +source dev-container-features-test-lib + +# Feature-specific tests +# The 'check' command comes from the dev-container-features-test-lib. Syntax is... +# check