From 715f3a465c33e72b1c3e8badd558b3b153a1166b Mon Sep 17 00:00:00 2001 From: David Randall Date: Tue, 21 Nov 2023 01:05:13 +0000 Subject: [PATCH] Add: Devcontainer support Signed-off-by: David Randall --- .devcontainer/devcontainer.json | 38 +++++++++++++++++++++++++++++++++ .gitignore | 3 +++ scripts/modify-environment.sh | 20 +++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 .devcontainer/devcontainer.json create mode 100644 scripts/modify-environment.sh diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..6591822 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,38 @@ +{ + "name": "Default dev container", + "image": "mcr.microsoft.com/devcontainers/go:1.20", + // GitHub's devcontainers are in increments of 2 core/8GB memory. Specifying less will use the minimum. + // https://docs.github.com/en/billing/managing-billing-for-github-codespaces/about-billing-for-github-codespaces#pricing-for-paid-usage + "hostRequirements": { + "cpus": 1, + "memory": "4gb" + }, + "waitFor": "onCreateCommand", + "updateContentCommand": { + "install": "sudo apt-get update && sudo apt-get install --no-install-recommends -y smartmontools vim", + // Modify the environment to provide better developer experience. + "modify-environment": "${PWD}/scripts/modify-environment.sh" + }, + "postCreateCommand": { + }, + "customizations": { + "codespaces": { + // Open files upon start + "openFiles": [ + "metrics.go", + "smartctl.go" + ] + }, + "vscode": { + // VS Code extensions to install + "extensions": [ + "streetsidesoftware.code-spell-checker", + "timonwong.shellcheck", + "golang.Go" + ], + "settings": { + "shellcheck.customArgs": ["-x"] + } + } + } +} diff --git a/.gitignore b/.gitignore index 930cf26..2533db1 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,9 @@ /.tarballs debug/ +# .cache is used to store transient data (bash history) in devcontainers. +.cache/ + Manifest smartctl_exporter *.exe diff --git a/scripts/modify-environment.sh b/scripts/modify-environment.sh new file mode 100644 index 0000000..d8050b2 --- /dev/null +++ b/scripts/modify-environment.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash + +# This script modifies the environemnt, namely ~/.bashrc, to preserve the bash history. + +# The workspace in the devcontainer is preserved across rebuilds. +# Use .cache to keep history and local scripts. +# .cache is excluded from git (i.e. in .gitignore) +mkdir -p "${PWD}/.cache/" + +# Preserve history +[[ ! -L "${HOME}/.bash_history" ]] && ln -sf "${PWD}/.cache/bash_history" "${HOME}/.bash_history" +[[ ! -f "${PWD}/.cache/bash_history" ]] && touch "${PWD}/.cache/bash_history" + +# Write history after every command to preserve it across rebuilds. +if ! grep -q '^### CUSTOM: Preserve Bash History ###$' "${HOME}/.bashrc"; then + cat >> "${HOME}/.bashrc" <<'EOT' +### CUSTOM: Preserve Bash History ### +PROMPT_COMMAND="history -a; ${PROMPT_COMMAND}" +EOT +fi