Unverified Commit be082b0e authored by naturedamends's avatar naturedamends Committed by GitHub

Common utils: Add config to remove zsh rc files from (#614)

* Add config to remove zsh rc files from common-utils.

* Bump version and add config to install script.

* Checks preventing excess writing to .zshrc.

* Change devcontainer feature option name.

* Coding standards.

* Favour adding feature via config 

and default to overriding .zshrc with dev-container default template.

* Update devcontainer-feature.json

* Update devcontainer-feature.json

* Update src/common-utils/devcontainer-feature.json
Co-authored-by: 's avatarSamruddhi Khandale <samruddhikhandale@github.com>

* Update src/common-utils/devcontainer-feature.json
Co-authored-by: 's avatarSamruddhi Khandale <samruddhikhandale@github.com>

* Testing for using devcontainer .zshrc template file.

* Coding standards.

* Coding standards.

* Update configure_zsh_as_default_shell_no_template.sh

* Grammar in configure_zsh_as_default_shell.sh

* Testing accounts for marked file cache  (#4)

* Account for mark file in testing.

* Remove some debugging and tests back

* Add back tests?

* Update configure_zsh_no_template.sh

---------
Co-authored-by: 's avatarSamruddhi Khandale <samruddhikhandale@github.com>
parent 9e75db9a
{ {
"id": "common-utils", "id": "common-utils",
"version": "2.0.11", "version": "2.1.0",
"name": "Common Utilities", "name": "Common Utilities",
"documentationURL": "https://github.com/devcontainers/features/tree/main/src/common-utils", "documentationURL": "https://github.com/devcontainers/features/tree/main/src/common-utils",
"description": "Installs a set of common command line utilities, Oh My Zsh!, and sets up a non-root user.", "description": "Installs a set of common command line utilities, Oh My Zsh!, and sets up a non-root user.",
...@@ -20,6 +20,11 @@ ...@@ -20,6 +20,11 @@
"default": true, "default": true,
"description": "Install Oh My Zsh!?" "description": "Install Oh My Zsh!?"
}, },
"installOhMyZshConfig": {
"type": "boolean",
"default": true,
"description": "Allow installing the default dev container .zshrc templates?"
},
"upgradePackages": { "upgradePackages": {
"type": "boolean", "type": "boolean",
"default": true, "default": true,
......
...@@ -12,6 +12,7 @@ set -e ...@@ -12,6 +12,7 @@ set -e
INSTALL_ZSH="${INSTALLZSH:-"true"}" INSTALL_ZSH="${INSTALLZSH:-"true"}"
CONFIGURE_ZSH_AS_DEFAULT_SHELL="${CONFIGUREZSHASDEFAULTSHELL:-"false"}" CONFIGURE_ZSH_AS_DEFAULT_SHELL="${CONFIGUREZSHASDEFAULTSHELL:-"false"}"
INSTALL_OH_MY_ZSH="${INSTALLOHMYZSH:-"true"}" INSTALL_OH_MY_ZSH="${INSTALLOHMYZSH:-"true"}"
INSTALL_OH_MY_ZSH_CONFIG="${INSTALLOHMYZSHCONFIG:-"true"}"
UPGRADE_PACKAGES="${UPGRADEPACKAGES:-"true"}" UPGRADE_PACKAGES="${UPGRADEPACKAGES:-"true"}"
USERNAME="${USERNAME:-"automatic"}" USERNAME="${USERNAME:-"automatic"}"
USER_UID="${UID:-"automatic"}" USER_UID="${UID:-"automatic"}"
......
...@@ -12,6 +12,7 @@ set -e ...@@ -12,6 +12,7 @@ set -e
INSTALL_ZSH="${INSTALLZSH:-"true"}" INSTALL_ZSH="${INSTALLZSH:-"true"}"
CONFIGURE_ZSH_AS_DEFAULT_SHELL="${CONFIGUREZSHASDEFAULTSHELL:-"false"}" CONFIGURE_ZSH_AS_DEFAULT_SHELL="${CONFIGUREZSHASDEFAULTSHELL:-"false"}"
INSTALL_OH_MY_ZSH="${INSTALLOHMYZSH:-"true"}" INSTALL_OH_MY_ZSH="${INSTALLOHMYZSH:-"true"}"
INSTALL_OH_MY_ZSH_CONFIG="${INSTALLOHMYZSHCONFIG:-"true"}"
UPGRADE_PACKAGES="${UPGRADEPACKAGES:-"true"}" UPGRADE_PACKAGES="${UPGRADEPACKAGES:-"true"}"
USERNAME="${USERNAME:-"automatic"}" USERNAME="${USERNAME:-"automatic"}"
USER_UID="${USERUID:-"automatic"}" USER_UID="${USERUID:-"automatic"}"
...@@ -390,7 +391,8 @@ else ...@@ -390,7 +391,8 @@ else
fi fi
# Restore user .bashrc / .profile / .zshrc defaults from skeleton file if it doesn't exist or is empty # Restore user .bashrc / .profile / .zshrc defaults from skeleton file if it doesn't exist or is empty
possible_rc_files=( ".bashrc" ".profile" ".zshrc" ) possible_rc_files=( ".bashrc" ".profile" )
[ "$INSTALL_OH_MY_ZSH_CONFIG" == "true" ] && possible_rc_files+=('.zshrc')
for rc_file in "${possible_rc_files[@]}"; do for rc_file in "${possible_rc_files[@]}"; do
if [ -f "/etc/skel/${rc_file}" ]; then if [ -f "/etc/skel/${rc_file}" ]; then
if [ ! -e "${user_home}/${rc_file}" ] || [ ! -s "${user_home}/${rc_file}" ]; then if [ ! -e "${user_home}/${rc_file}" ] || [ ! -s "${user_home}/${rc_file}" ]; then
...@@ -450,10 +452,11 @@ if [ "${INSTALL_ZSH}" = "true" ]; then ...@@ -450,10 +452,11 @@ if [ "${INSTALL_ZSH}" = "true" ]; then
# Adapted, simplified inline Oh My Zsh! install steps that adds, defaults to a codespaces theme. # Adapted, simplified inline Oh My Zsh! install steps that adds, defaults to a codespaces theme.
# See https://github.com/ohmyzsh/ohmyzsh/blob/master/tools/install.sh for official script. # See https://github.com/ohmyzsh/ohmyzsh/blob/master/tools/install.sh for official script.
if [ "${INSTALL_OH_MY_ZSH}" = "true" ]; then
user_rc_file="${user_home}/.zshrc"
oh_my_install_dir="${user_home}/.oh-my-zsh" oh_my_install_dir="${user_home}/.oh-my-zsh"
if [ ! -d "${oh_my_install_dir}" ] && [ "${INSTALL_OH_MY_ZSH}" = "true" ]; then
template_path="${oh_my_install_dir}/templates/zshrc.zsh-template" template_path="${oh_my_install_dir}/templates/zshrc.zsh-template"
user_rc_file="${user_home}/.zshrc" if [ ! -d "${oh_my_install_dir}" ]; then
umask g-w,o-w umask g-w,o-w
mkdir -p ${oh_my_install_dir} mkdir -p ${oh_my_install_dir}
git clone --depth=1 \ git clone --depth=1 \
...@@ -463,20 +466,28 @@ if [ "${INSTALL_ZSH}" = "true" ]; then ...@@ -463,20 +466,28 @@ if [ "${INSTALL_ZSH}" = "true" ]; then
-c fetch.fsck.zeroPaddedFilemode=ignore \ -c fetch.fsck.zeroPaddedFilemode=ignore \
-c receive.fsck.zeroPaddedFilemode=ignore \ -c receive.fsck.zeroPaddedFilemode=ignore \
"https://github.com/ohmyzsh/ohmyzsh" "${oh_my_install_dir}" 2>&1 "https://github.com/ohmyzsh/ohmyzsh" "${oh_my_install_dir}" 2>&1
echo -e "$(cat "${template_path}")\nDISABLE_AUTO_UPDATE=true\nDISABLE_UPDATE_PROMPT=true" > ${user_rc_file}
sed -i -e 's/ZSH_THEME=.*/ZSH_THEME="devcontainers"/g' ${user_rc_file} # Shrink git while still enabling updates
cd "${oh_my_install_dir}"
git repack -a -d -f --depth=1 --window=1
fi
# Add Dev Containers theme # Add Dev Containers theme
mkdir -p ${oh_my_install_dir}/custom/themes mkdir -p ${oh_my_install_dir}/custom/themes
cp -f "${FEATURE_DIR}/scripts/devcontainers.zsh-theme" "${oh_my_install_dir}/custom/themes/devcontainers.zsh-theme" cp -f "${FEATURE_DIR}/scripts/devcontainers.zsh-theme" "${oh_my_install_dir}/custom/themes/devcontainers.zsh-theme"
ln -s "${oh_my_install_dir}/custom/themes/devcontainers.zsh-theme" "${oh_my_install_dir}/custom/themes/codespaces.zsh-theme" ln -sf "${oh_my_install_dir}/custom/themes/devcontainers.zsh-theme" "${oh_my_install_dir}/custom/themes/codespaces.zsh-theme"
# Add devcontainer .zshrc template
if [ "$INSTALL_OH_MY_ZSH_CONFIG" = "true" ]; then
echo -e "$(cat "${template_path}")\nDISABLE_AUTO_UPDATE=true\nDISABLE_UPDATE_PROMPT=true" > ${user_rc_file}
sed -i -e 's/ZSH_THEME=.*/ZSH_THEME="devcontainers"/g' ${user_rc_file}
fi
# Shrink git while still enabling updates
cd "${oh_my_install_dir}"
git repack -a -d -f --depth=1 --window=1
# Copy to non-root user if one is specified # Copy to non-root user if one is specified
if [ "${USERNAME}" != "root" ]; then if [ "${USERNAME}" != "root" ]; then
cp -rf "${user_rc_file}" "${oh_my_install_dir}" /root copy_to_user_files=("${oh_my_install_dir}")
[ -f "$user_rc_file" ] && copy_to_user_files+=("$user_rc_file")
cp -rf "${copy_to_user_files[@]}" /root
chown -R ${USERNAME}:${group_name} "${oh_my_install_dir}" "${user_rc_file}" chown -R ${USERNAME}:${group_name} "${oh_my_install_dir}" "${user_rc_file}"
fi fi
fi fi
......
...@@ -7,6 +7,8 @@ source dev-container-features-test-lib ...@@ -7,6 +7,8 @@ source dev-container-features-test-lib
# Definition specific tests # Definition specific tests
check "default-shell-is-zsh" bash -c "getent passwd $(whoami) | awk -F: '{ print $7 }' | grep '/bin/zsh'" check "default-shell-is-zsh" bash -c "getent passwd $(whoami) | awk -F: '{ print $7 }' | grep '/bin/zsh'"
# check it overrides the ~/.zshrc with default dev containers template
check "default-zshrc-is-dev-container-template" bash -c "cat ~/.zshrc | grep ZSH_THEME | grep devcontainers"
# Report result # Report result
reportResults reportResults
#!/bin/bash
set -e
# Optional: Import test library
source dev-container-features-test-lib
# Definition specific tests
function file_not_overridden() {
cat ~/.zshrc | grep 'alias fnomockalias=' | grep testingmock
}
check "default-zsh-with-no-zshrc" file_not_overridden
# Report result
reportResults
...@@ -115,6 +115,17 @@ ...@@ -115,6 +115,17 @@
} }
} }
}, },
"configure_zsh_no_template": {
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
"postCreateCommand": "echo alias fnomockalias=testingmock >> /root/.zshrc",
"remoteUser": "root",
"features": {
"common-utils": {
"installZsh": true,
"installOhMyZshConfig": false
}
}
},
"config-subdirectory": { "config-subdirectory": {
"image": "alpine", "image": "alpine",
"remoteUser": "devcontainer", "remoteUser": "devcontainer",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment