From 192e4f0b1296e31fb3efc81465815d82231dfa2d Mon Sep 17 00:00:00 2001 From: Gaurav Saini <147703805+gauravsaini04@users.noreply.github.com> Date: Tue, 5 Nov 2024 01:27:41 +0000 Subject: [PATCH] changes as suggested --- src/common-utils/devcontainer-feature.json | 6 +++ src/common-utils/main.sh | 45 ++-------------------- src/common-utils/setup_history.sh | 36 +++++++++++++++++ 3 files changed, 46 insertions(+), 41 deletions(-) create mode 100755 src/common-utils/setup_history.sh diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json index 3af06cc10..502345f7d 100644 --- a/src/common-utils/devcontainer-feature.json +++ b/src/common-utils/devcontainer-feature.json @@ -71,9 +71,15 @@ "description": "Preserve shell history across dev container instances? (Currently supports bash, zsh, and fish)" } }, + "postCreateCommand": "export DEVCONTAINER_ID=${devcontainerId} && chmod +x /tmp/setup_history.sh && . /tmp/setup_history.sh", "mounts": [{ "source": "devcontainers", "target": "/devcontainers", "type": "volume" + }, + { + "source": "/workspaces/features/src/common-utils/setup_history.sh", + "target": "/tmp/setup_history.sh", + "type": "bind" }] } \ No newline at end of file diff --git a/src/common-utils/main.sh b/src/common-utils/main.sh index 760f92eba..1f1a0eab0 100644 --- a/src/common-utils/main.sh +++ b/src/common-utils/main.sh @@ -572,48 +572,11 @@ fi # ** Enable shell history ** # ********************************* -if [ "${ALLOW_SHELL_HISTORY}" = "true" ]; then - echo "Activating feature 'shell-history'" - echo "User: ${USERNAME} User home: ${user_home}" +echo export ALLOW_SHELL_HISTORY="${ALLOW_SHELL_HISTORY}" > /tmp/env.sh +echo export user_home="${user_home}" >> /tmp/env.sh +echo export USERNAME="${USERNAME}" >> /tmp/env.sh - if ! command -v uuidgen &> /dev/null; then - sudo apt-get update - sudo apt-get install -y uuid-runtime - fi - # Create the shell history directory in the mounted volume - DEVCONTAINER_ID=$(uuidgen) - HISTORY_DIR="/devcontainers/${DEVCONTAINER_ID}/shellHistory" - USER_HISTORY_FILE="${user_home}/.bash_history" - VOLUME_HISTORY_FILE="${HISTORY_DIR}/.bash_history" - - # Create the history directory in the volume, if it doesn’t already exist - sudo mkdir -p "${HISTORY_DIR}" - sudo chown -R "${USERNAME}" "${HISTORY_DIR}" - sudo chmod -R u+rwx "${HISTORY_DIR}" - - # Ensure the volume's history file exists and set permissions - if [[ ! -f "${VOLUME_HISTORY_FILE}" ]]; then - # Create an empty history file if it doesn’t already exist - sudo touch "${VOLUME_HISTORY_FILE}" - sudo chown -R "${USERNAME}" "${VOLUME_HISTORY_FILE}" - sudo chmod -R u+rwx "${VOLUME_HISTORY_FILE}" - fi - - # Create or update the user’s .bash_history to append to the volume’s history - if [[ ! -f "${USER_HISTORY_FILE}" ]]; then - sudo touch "${USER_HISTORY_FILE}" - sudo chown -R "${USERNAME}" "${USER_HISTORY_FILE}" - sudo chmod -R u+rwx "${USER_HISTORY_FILE}" - fi - - # Symlink for Bash history - sudo ln -sf ${USER_HISTORY_FILE} ${VOLUME_HISTORY_FILE} - - # Configure immediate history saving to the volume - echo 'PROMPT_COMMAND="history -a; history -c; history -r"' >> "${user_home}/.bashrc" - - echo "Shell history setup for persistent appending is complete." -fi +sudo chmod +x /tmp/env.sh # ********************************* # ** Ensure config directory ** diff --git a/src/common-utils/setup_history.sh b/src/common-utils/setup_history.sh new file mode 100755 index 000000000..94d92666e --- /dev/null +++ b/src/common-utils/setup_history.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +# Source the environment variables from env.sh +if [ -f /tmp/env.sh ]; then + . /tmp/env.sh +else + echo "env.sh not found!" +fi + +if [ "${ALLOW_SHELL_HISTORY}" = "true" ]; then + echo "Activating feature 'shell-history'" + echo "User: ${USERNAME} User home: ${user_home}" + + # Create the shell history directory in the mounted volume + HISTORY_DIR="/devcontainers/${DEVCONTAINER_ID}/shellHistory" + USER_HISTORY_FILE="${user_home}/.bash_history" + VOLUME_HISTORY_FILE="${HISTORY_DIR}/.bash_history" + + # Create the history directory in the volume, if it doesn’t already exist + sudo mkdir -p "${HISTORY_DIR}" + sudo chown -R "${USERNAME}" "${HISTORY_DIR}" + sudo chmod -R u+rwx "${HISTORY_DIR}" + + # Ensure the volume's history file exists and set permissions + sudo touch "${VOLUME_HISTORY_FILE}" + sudo chown -R "${USERNAME}" "${VOLUME_HISTORY_FILE}" + sudo chmod -R u+rwx "${VOLUME_HISTORY_FILE}" + + # Symlink for Bash history + sudo ln -sf ${USER_HISTORY_FILE} ${VOLUME_HISTORY_FILE} + + # Configure immediate history saving to the volume + echo 'PROMPT_COMMAND="history -a; history -r;"' >> "${user_home}/.bashrc" + + echo "Shell history setup for persistent appending is complete." +fi \ No newline at end of file