Skip to content

Commit 192e4f0

Browse files
committed
changes as suggested
1 parent 76d048b commit 192e4f0

File tree

3 files changed

+46
-41
lines changed

3 files changed

+46
-41
lines changed

src/common-utils/devcontainer-feature.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,15 @@
7171
"description": "Preserve shell history across dev container instances? (Currently supports bash, zsh, and fish)"
7272
}
7373
},
74+
"postCreateCommand": "export DEVCONTAINER_ID=${devcontainerId} && chmod +x /tmp/setup_history.sh && . /tmp/setup_history.sh",
7475
"mounts": [{
7576
"source": "devcontainers",
7677
"target": "/devcontainers",
7778
"type": "volume"
79+
},
80+
{
81+
"source": "/workspaces/features/src/common-utils/setup_history.sh",
82+
"target": "/tmp/setup_history.sh",
83+
"type": "bind"
7884
}]
7985
}

src/common-utils/main.sh

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -572,48 +572,11 @@ fi
572572
# ** Enable shell history **
573573
# *********************************
574574

575-
if [ "${ALLOW_SHELL_HISTORY}" = "true" ]; then
576-
echo "Activating feature 'shell-history'"
577-
echo "User: ${USERNAME} User home: ${user_home}"
575+
echo export ALLOW_SHELL_HISTORY="${ALLOW_SHELL_HISTORY}" > /tmp/env.sh
576+
echo export user_home="${user_home}" >> /tmp/env.sh
577+
echo export USERNAME="${USERNAME}" >> /tmp/env.sh
578578

579-
if ! command -v uuidgen &> /dev/null; then
580-
sudo apt-get update
581-
sudo apt-get install -y uuid-runtime
582-
fi
583-
# Create the shell history directory in the mounted volume
584-
DEVCONTAINER_ID=$(uuidgen)
585-
HISTORY_DIR="/devcontainers/${DEVCONTAINER_ID}/shellHistory"
586-
USER_HISTORY_FILE="${user_home}/.bash_history"
587-
VOLUME_HISTORY_FILE="${HISTORY_DIR}/.bash_history"
588-
589-
# Create the history directory in the volume, if it doesn’t already exist
590-
sudo mkdir -p "${HISTORY_DIR}"
591-
sudo chown -R "${USERNAME}" "${HISTORY_DIR}"
592-
sudo chmod -R u+rwx "${HISTORY_DIR}"
593-
594-
# Ensure the volume's history file exists and set permissions
595-
if [[ ! -f "${VOLUME_HISTORY_FILE}" ]]; then
596-
# Create an empty history file if it doesn’t already exist
597-
sudo touch "${VOLUME_HISTORY_FILE}"
598-
sudo chown -R "${USERNAME}" "${VOLUME_HISTORY_FILE}"
599-
sudo chmod -R u+rwx "${VOLUME_HISTORY_FILE}"
600-
fi
601-
602-
# Create or update the user’s .bash_history to append to the volume’s history
603-
if [[ ! -f "${USER_HISTORY_FILE}" ]]; then
604-
sudo touch "${USER_HISTORY_FILE}"
605-
sudo chown -R "${USERNAME}" "${USER_HISTORY_FILE}"
606-
sudo chmod -R u+rwx "${USER_HISTORY_FILE}"
607-
fi
608-
609-
# Symlink for Bash history
610-
sudo ln -sf ${USER_HISTORY_FILE} ${VOLUME_HISTORY_FILE}
611-
612-
# Configure immediate history saving to the volume
613-
echo 'PROMPT_COMMAND="history -a; history -c; history -r"' >> "${user_home}/.bashrc"
614-
615-
echo "Shell history setup for persistent appending is complete."
616-
fi
579+
sudo chmod +x /tmp/env.sh
617580

618581
# *********************************
619582
# ** Ensure config directory **

src/common-utils/setup_history.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/bash
2+
3+
# Source the environment variables from env.sh
4+
if [ -f /tmp/env.sh ]; then
5+
. /tmp/env.sh
6+
else
7+
echo "env.sh not found!"
8+
fi
9+
10+
if [ "${ALLOW_SHELL_HISTORY}" = "true" ]; then
11+
echo "Activating feature 'shell-history'"
12+
echo "User: ${USERNAME} User home: ${user_home}"
13+
14+
# Create the shell history directory in the mounted volume
15+
HISTORY_DIR="/devcontainers/${DEVCONTAINER_ID}/shellHistory"
16+
USER_HISTORY_FILE="${user_home}/.bash_history"
17+
VOLUME_HISTORY_FILE="${HISTORY_DIR}/.bash_history"
18+
19+
# Create the history directory in the volume, if it doesn’t already exist
20+
sudo mkdir -p "${HISTORY_DIR}"
21+
sudo chown -R "${USERNAME}" "${HISTORY_DIR}"
22+
sudo chmod -R u+rwx "${HISTORY_DIR}"
23+
24+
# Ensure the volume's history file exists and set permissions
25+
sudo touch "${VOLUME_HISTORY_FILE}"
26+
sudo chown -R "${USERNAME}" "${VOLUME_HISTORY_FILE}"
27+
sudo chmod -R u+rwx "${VOLUME_HISTORY_FILE}"
28+
29+
# Symlink for Bash history
30+
sudo ln -sf ${USER_HISTORY_FILE} ${VOLUME_HISTORY_FILE}
31+
32+
# Configure immediate history saving to the volume
33+
echo 'PROMPT_COMMAND="history -a; history -r;"' >> "${user_home}/.bashrc"
34+
35+
echo "Shell history setup for persistent appending is complete."
36+
fi

0 commit comments

Comments
 (0)