Skip to content

Commit

Permalink
Refactor postAttachCommand (#2312)
Browse files Browse the repository at this point in the history
* Refactor postAttachCommand

* Refactor comment
  • Loading branch information
ykadowak committed Jan 30, 2024
1 parent 6c41eac commit b3958f4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
7 changes: 2 additions & 5 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@
// To access grafana
"appPort": "3000:3000",

// Persist zsh history settings below
// overwrite HISTFILE to store history file in the named volume defined below
// also setting INC_APPEND_HISTORY to immediately flush command history to the file to keep history when rebuilding the devcontainer
"postStartCommand": "echo 'export HISTFILE=/commandhistory/.zsh_history' >> /root/.zshrc && echo 'setopt INC_APPEND_HISTORY' >> /root/.zshrc",
// define named volume to store zsh history file
"mounts": ["source=zshhistory-named-volume,target=/commandhistory,type=volume"],
"postAttachCommand": "mkdir -p /etc/server && ln -s $(pwd)/cmd/agent/core/ngt/sample.yaml /etc/server/config.yaml"

"postAttachCommand": ["/bin/bash", ".devcontainer/postAttachCommand.sh"]
}
35 changes: 35 additions & 0 deletions .devcontainer/postAttachCommand.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash -eu
#
# This script is executed as postAttachCommand in devcontainer.json
# This script does...
# - create symbolic link of config.yaml for easier development
# - add command history setting to .zshrc to persist history
#

echo "creating symbolic link of config ZSHRC..."

LINK_TARGET="$(pwd)/cmd/agent/core/ngt/sample.yaml"
LINK_SRC="/etc/server/config.yaml"

mkdir -p /etc/server

if [ ! -e "$LINK_SRC" ]; then
ln -s "$LINK_TARGET" "$LINK_SRC"
echo "created symbolic link: $LINK_SRC -> $LINK_TARGET"
else
echo "$LINK_SRC already exists"
fi


echo "adding history setting to .zshrc..."

LINE1="export HISTFILE=/commandhistory/.zsh_history"
LINE2="setopt INC_APPEND_HISTORY"

ZSHRC="/root/.zshrc"

# write only if those lines don't exist
grep -qxF "$LINE1" "$ZSHRC" || echo "$LINE1" >> "$ZSHRC"
grep -qxF "$LINE2" "$ZSHRC" || echo "$LINE2" >> "$ZSHRC"

echo "added history setting to .zshrc"

0 comments on commit b3958f4

Please sign in to comment.