From 6e37978859e8bdffa80c36a05c8377d6b7e3ecc4 Mon Sep 17 00:00:00 2001 From: Kaitlyn Alphonse Date: Fri, 2 Oct 2026 15:54:11 -0400 Subject: [PATCH 1/2] refactor: make shared message functions reusable --- README.md | 36 ++++++++++++++++++++++++++++++++++++ src/bash/functions | 41 ++++++++--------------------------------- src/bash/messages | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 33 deletions(-) create mode 100644 src/bash/messages diff --git a/README.md b/README.md index 21f1fc5..4f35239 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,41 @@ To install the dotfiles, clone this repository to your 🏠 📂 and then 🏃 t Once you complete the installation steps, you will see a welcome message 📜. +## Messages in scripts and containers + +The shared message functions live in `src/bash/messages`. A script can load +this file to use the existing colours and timestamps without running terminal +setup or creating directories: + +```bash +source "$HOME/dotfiles/src/bash/messages" +msg info "Starting the tests." +kmsg "The tests passed." +wmsg "A dependency is missing." +emsg "The tests failed." +``` + +`msg` accepts `fail`, `ok`, `warn`, or `info` followed by the message. Passing +only a message prints a notice. The shortcuts `emsg`, `kmsg`, `wmsg`, and +`imsg` keep their existing meanings. Message text, spacing, colours, and the +`America/St_Lucia` timestamp timezone are unchanged. + +The regular `src/bash/functions` file loads these same definitions. Existing +terminal configuration therefore keeps using the same function names. The +`ptc` alias remains the existing pytest command. + +To check an unpublished checkout, run these commands from its repository root: + +```bash +bash -c 'source ./src/bash/messages; imsg "Shared messages loaded."; kmsg "Bash check complete."' +zsh -f -c 'source ./src/bash/messages; imsg "Shared messages loaded."; kmsg "Zsh check complete."' +``` + +Use direct sourcing while testing a feature branch: `install` and `update` +select the `production` branch. For containers, include a reviewed dotfiles +revision containing this file in the image and explicitly source the file in +the script. A noninteractive script should not depend on `.bashrc` being read. + ## Uninstallation To uninstall the dotfiles, run the following commands: @@ -100,6 +135,7 @@ Here is the dotfiles's directory structure (note that Zsh uses the bash function |--- bash |--- aliases |--- functions + |--- messages |--- env.env |--- zsh |--- aliases diff --git a/src/bash/functions b/src/bash/functions index d425aff..b806b01 100755 --- a/src/bash/functions +++ b/src/bash/functions @@ -1,39 +1,14 @@ #!/usr/bin/env bash -# Print color coded messages -msg() { - timestamp=$(TZ="America/St_Lucia" date '+%Y-%m-%d %H:%M:%S') - case $1 in - fail) - printf "\n%s \033[1;31m[ FAILURE ]\033[0m %s\n\n" "$timestamp" "$2" - ;; - ok) - printf "\n%s \033[1;32m[ SUCCESS ]\033[0m %s\n\n" "$timestamp" "$2" - ;; - warn) - printf "\n%s \033[1;33m[ WARNING ]\033[0m %s\n\n" "$timestamp" "$2" - ;; - info) - printf "\n%s \033[1;34m[INFORMATION]\033[0m %s\n\n" "$timestamp" "$2" - ;; - *) - printf "\n%s [ NOTICE ] %s\n\n" "$timestamp" "$1" - ;; - esac -} -emsg() { - msg "fail" "$1" -} -kmsg() { - msg "ok" "$1" -} -imsg() { - msg "info" "$1" -} -wmsg() { - msg "warn" "$1" -} +# Load messages from this checkout in either Bash or Zsh. +if [ -n "${ZSH_VERSION:-}" ]; then + # shellcheck disable=SC2296,SC1091 + source "${${(%):-%x}:A:h}/messages" || return +else + # shellcheck source=src/bash/messages + source "$(dirname -- "${BASH_SOURCE[0]}")/messages" || return +fi # A greeting script diff --git a/src/bash/messages b/src/bash/messages new file mode 100644 index 0000000..827f24b --- /dev/null +++ b/src/bash/messages @@ -0,0 +1,36 @@ +#!/usr/bin/env bash + +# Shared message functions. Sourcing this file performs no terminal setup. +# Print color coded messages +msg() { + timestamp=$(TZ="America/St_Lucia" date '+%Y-%m-%d %H:%M:%S') + case $1 in + fail) + printf "\n%s \033[1;31m[ FAILURE ]\033[0m %s\n\n" "$timestamp" "$2" + ;; + ok) + printf "\n%s \033[1;32m[ SUCCESS ]\033[0m %s\n\n" "$timestamp" "$2" + ;; + warn) + printf "\n%s \033[1;33m[ WARNING ]\033[0m %s\n\n" "$timestamp" "$2" + ;; + info) + printf "\n%s \033[1;34m[INFORMATION]\033[0m %s\n\n" "$timestamp" "$2" + ;; + *) + printf "\n%s [ NOTICE ] %s\n\n" "$timestamp" "$1" + ;; + esac +} +emsg() { + msg "fail" "$1" +} +kmsg() { + msg "ok" "$1" +} +imsg() { + msg "info" "$1" +} +wmsg() { + msg "warn" "$1" +} From 262223957813317fd056ff3333d3b74a7f0ec407 Mon Sep 17 00:00:00 2001 From: Kaitlyn Alphonse Date: Fri, 2 Oct 2026 16:08:47 -0400 Subject: [PATCH 2/2] fix: resolve shared-message lint and status reporting errors Explain and narrowly suppress the Bash-only diagnostic for the guarded Zsh expansion. Mark the shared message file executable to match the repository lint convention. Allow only the lint job to read contents and publish commit statuses. --- .github/workflows/linter.yml | 3 +++ src/bash/functions | 3 ++- src/bash/messages | 0 3 files changed, 5 insertions(+), 1 deletion(-) mode change 100644 => 100755 src/bash/messages diff --git a/.github/workflows/linter.yml b/.github/workflows/linter.yml index 94f792b..67378c2 100644 --- a/.github/workflows/linter.yml +++ b/.github/workflows/linter.yml @@ -18,6 +18,9 @@ jobs: run-lint: runs-on: ubuntu-latest + permissions: + contents: read + statuses: write steps: - name: Checkout code uses: actions/checkout@v6 diff --git a/src/bash/functions b/src/bash/functions index b806b01..2c1ba4a 100755 --- a/src/bash/functions +++ b/src/bash/functions @@ -3,7 +3,8 @@ # Load messages from this checkout in either Bash or Zsh. if [ -n "${ZSH_VERSION:-}" ]; then - # shellcheck disable=SC2296,SC1091 + # ShellCheck parses this as Bash; only Zsh evaluates this expansion. + # shellcheck disable=SC2296,SC2298,SC1091 source "${${(%):-%x}:A:h}/messages" || return else # shellcheck source=src/bash/messages diff --git a/src/bash/messages b/src/bash/messages old mode 100644 new mode 100755