Skip to content

Commit

Permalink
is-*: fix for bash v3, for macos cron
Browse files Browse the repository at this point in the history
`env -i DOROTHY="$DOROTHY" "$(which is-linux)"` returned 0 on macos, as it was using `/bin/bash` which is bash v3

this can also be proven by:

```
#!/bin/bash

function is_linux_test() (
	source "$DOROTHY/sources/bash.bash"
	echo-segment --h1="TEST: $0"

	local os
	os="$(uname -s)"
	if test "$os" = 'Linux'; then
		eval-tester --name='is linux' --status='0' \
			-- is-linux --question='is linux'
	else
		eval-tester --name='is not linux' --status='1' \
			-- is-linux --question='is not linux'
	fi

	echo-segment --g1="TEST: $0"
	return 0
)
function is_linux() (
	source "$DOROTHY/sources/bash.bash"

	test "$(uname -s)" = 'Linux'
)

# fire if invoked standalone
if test "$0" = "${BASH_SOURCE[0]}"; then
	if test "$*" = '--test'; then
		is_linux_test
	else
		is_linux "$@"
	fi
fi
```

Note that `/usr/bin/env bash` is replaced with `/bin/bash`, this is because `/usr/bin/env bash` seems to ignore `env -i`.

Other changes include:

- dorothy: move env setup to be prior to bash.bash loading, such that bash.bash can be loaded under cron environment
- `TERM_PROGRAM` may not always be defined
- `is-color-enabled` now checks fron cron environment to disable color
  • Loading branch information
balupton committed Oct 25, 2023
1 parent ac3f5eb commit 3b85eb1
Show file tree
Hide file tree
Showing 38 changed files with 252 additions and 101 deletions.
98 changes: 45 additions & 53 deletions commands/dorothy
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,52 @@ function dorothy() (
exit 6 # ENXIO 6 Device not configured
fi

# vars that should be exported to subshells, which may or may not be inherited, as setup-environment-commands has not run yet
local self
export DOROTHY ZDOTDIR PATH XDG_CONFIG_HOME XDG_CACHE_HOME XDG_BIN_HOME XDG_DATA_HOME XDG_STATE_HOME
if test -z "${DOROTHY-}"; then
# handle cron situation (dorothy is installed, however environment is empty)
# `env -i "$(which dorothy)" run env` <-- whoami returns user who has dorothy installed:
# `sudo env -i "$(which dorothy)" run env` <-- whoami returns root, who does not have dorothy installed
# @todo consider if this runs on zsh and bash v3
self="${BASH_SOURCE:-"$0"}"
if [[ $self == *'.local/share/dorothy/commands/dorothy' ]] && test -f "$self"; then
DOROTHY="${self%/commands/dorothy*}"
if test -z "${HOME-}"; then
export HOME
HOME="${self%/.local/share/dorothy/commands/dorothy*}"
fi
if test -z "${USER-}"; then
export USER
USER="$(basename "$HOME")"
fi
else
# handle fresh install situation
DOROTHY=''
fi
fi
if test -z "${ZDOTDIR-}"; then
ZDOTDIR=''
fi
if test -z "${XDG_CONFIG_HOME-}"; then
XDG_CONFIG_HOME="$HOME/.config"
fi
if test -z "${XDG_CACHE_HOME-}"; then
XDG_CACHE_HOME="$HOME/.cache"
fi
if test -z "${XDG_BIN_HOME-}"; then
XDG_BIN_HOME="$HOME/.local/bin"
fi
if test -z "${XDG_DATA_HOME-}"; then
XDG_DATA_HOME="$HOME/.local/share"
fi
if test -z "${XDG_STATE_HOME-}"; then
XDG_STATE_HOME="$HOME/.local/state"
fi
mkdir -p "$XDG_CONFIG_HOME" "$XDG_CACHE_HOME" "$XDG_BIN_HOME" "$XDG_DATA_HOME" "$XDG_STATE_HOME"

# source [bash.bash] for sensible defaults if it exists
if test -n "${DOROTHY-}" -a -f "${DOROTHY-}/sources/bash.bash"; then
if test -n "$DOROTHY" -a -f "$DOROTHY/sources/bash.bash"; then
source "$DOROTHY/sources/bash.bash"
else
# otherwise apply them manually
Expand Down Expand Up @@ -177,58 +221,6 @@ function dorothy() (
done
}

# =====================================
# Environment

# vars that should be exported to subshells, which may or may not be inherited, as setup-environment-commands has not run yet
local self
export DOROTHY ZDOTDIR PATH XDG_CONFIG_HOME XDG_CACHE_HOME XDG_BIN_HOME XDG_DATA_HOME XDG_STATE_HOME
if test -z "${DOROTHY-}"; then
# handle cron situation (dorothy is installed, however environment is empty)
# `env -i "$(which dorothy)" run env` <-- whoami returns user who has dorothy installed:
# `sudo env -i "$(which dorothy)" run env` <-- whoami returns root, who does not have dorothy installed
# @todo consider if this runs on zsh and bash v3
self="${BASH_SOURCE:-"$0"}"
if [[ $self == *'.local/share/dorothy/commands/dorothy' ]] && test -f "$self"; then
DOROTHY="${self%/commands/dorothy*}"
if test -z "${HOME-}"; then
export HOME
HOME="${self%/.local/share/dorothy/commands/dorothy*}"
fi
if test -z "${USER-}"; then
export USER
USER="$(basename "$HOME")"
fi
if test -n "${CRON-}" -o -n "${CRONITOR_EXEC-}"; then
# disable colour, different lines of bash v3 compat
export COLOR
COLOR=false
fi
else
# handle fresh install situation
DOROTHY=''
fi
fi
if test -z "${ZDOTDIR-}"; then
ZDOTDIR=''
fi
if test -z "${XDG_CONFIG_HOME-}"; then
XDG_CONFIG_HOME="$HOME/.config"
fi
if test -z "${XDG_CACHE_HOME-}"; then
XDG_CACHE_HOME="$HOME/.cache"
fi
if test -z "${XDG_BIN_HOME-}"; then
XDG_BIN_HOME="$HOME/.local/bin"
fi
if test -z "${XDG_DATA_HOME-}"; then
XDG_DATA_HOME="$HOME/.local/share"
fi
if test -z "${XDG_STATE_HOME-}"; then
XDG_STATE_HOME="$HOME/.local/state"
fi
mkdir -p "$XDG_CONFIG_HOME" "$XDG_CACHE_HOME" "$XDG_BIN_HOME" "$XDG_DATA_HOME" "$XDG_STATE_HOME"

# =====================================
# Variables

Expand Down
6 changes: 5 additions & 1 deletion commands/is-admin
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
function is_admin() (
source "$DOROTHY/sources/bash.bash"

is-user-in-group --group=admin
if is-user-in-group --group=admin; then
return 0
else
return 1
fi
)

# fire if invoked standalone
Expand Down
4 changes: 2 additions & 2 deletions commands/is-alpine
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
function is_alpine() (
source "$DOROTHY/sources/bash.bash"

if test -f /etc/os-release; then
grep -qi 'ID=alpine' /etc/os-release 2>/dev/null
if test -f /etc/os-release && grep -qi 'ID=alpine' /etc/os-release 2>/dev/null; then
return 0
else
return 1
fi
Expand Down
6 changes: 5 additions & 1 deletion commands/is-apk
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
function is_apk() (
source "$DOROTHY/sources/bash.bash"

command-exists apk
if command-exists apk; then
return 0
else
return 1
fi
)

# fire if invoked standalone
Expand Down
6 changes: 5 additions & 1 deletion commands/is-appimage
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
function is_appimage() (
source "$DOROTHY/sources/bash.bash"

command-exists 'appimaged' 'appimagetool' 'mkappimage'
if command-exists 'appimaged' 'appimagetool' 'mkappimage'; then
return 0
else
return 1
fi
)

# fire if invoked standalone
Expand Down
6 changes: 5 additions & 1 deletion commands/is-apple-silicon
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
function is_apple_silicon() (
source "$DOROTHY/sources/bash.bash"

is-mac && test "$(uname -p)" = 'arm' -o "$(uname -m)" = 'arm64'
if is-mac && test "$(uname -p)" = 'arm' -o "$(uname -m)" = 'arm64'; then
return 0
else
return 1
fi
)

# fire if invoked standalone
Expand Down
6 changes: 5 additions & 1 deletion commands/is-apt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
function is_apt() (
source "$DOROTHY/sources/bash.bash"

command-exists apt-get
if command-exists apt-get; then
return 0
else
return 1
fi
)

# fire if invoked standalone
Expand Down
6 changes: 5 additions & 1 deletion commands/is-arch
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
function is_arch() (
source "$DOROTHY/sources/bash.bash"

test -f /etc/arch-release
if test -f /etc/arch-release; then
return 0
else
return 1
fi
)

# fire if invoked standalone
Expand Down
6 changes: 5 additions & 1 deletion commands/is-array-count
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
function is_array_count() (
source "$DOROTHY/sources/bash.bash"

test "$(get-array-count "${@:2}")" -eq "$1"
if test "$(get-array-count "${@:2}")" -eq "$1"; then
return 0
else
return 1
fi
)

# fire if invoked standalone
Expand Down
6 changes: 5 additions & 1 deletion commands/is-array-count-ge
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
function is_array_count_ge() (
source "$DOROTHY/sources/bash.bash"

test "$(get-array-count "${@:2}")" -ge "$1"
if test "$(get-array-count "${@:2}")" -ge "$1"; then
return 0
else
return 1
fi
)

# fire if invoked standalone
Expand Down
9 changes: 4 additions & 5 deletions commands/is-brew
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ function is_brew() (
source "$DOROTHY/sources/bash.bash"

# workaround for our [brew] helper
if is-mac; then
if test -x "${HOMEBREW_PREFIX-}/bin/brew"; then
return 0
fi
if is-mac && test -x "${HOMEBREW_PREFIX-}/bin/brew"; then
return 0
else
return 1
fi
return 1
)

# fire if invoked standalone
Expand Down
1 change: 1 addition & 0 deletions commands/is-btrfs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function is_btrfs() (
return 1
fi
done
return 0
)

# fire if invoked standalone
Expand Down
6 changes: 5 additions & 1 deletion commands/is-ci
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ function is_ci() (
# =====================================
# Action

is-affirmative --ignore-empty -- "${CI-}" "${CONTINUOUS_INTEGRATION-}" "${BUILD_NUMBER-}" "${RUN_ID-}" || return 1
if is-affirmative --ignore-empty -- "${CI-}" "${CONTINUOUS_INTEGRATION-}" "${BUILD_NUMBER-}" "${RUN_ID-}"; then
return 0
else
return 1
fi
)

# fire if invoked standalone
Expand Down
6 changes: 5 additions & 1 deletion commands/is-color-enabled
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function is_color_enabled() (
use_color="\$(is-color-enabled <default> -- "\$@")"
QUIRKS:
Checks for [--[no-]color[s]=[yes|no]] values, as well as [[NO[_]]COLOR] environment variables.
Checks for [--[no-]color[s]=[yes|no]] values, as well as [[NO[_]]COLOR] and [CRON], [CRONITOR_EXEC], and [TERM] environment variables.
EOF
if test "$#" -ne 0; then
echo-error "$@"
Expand Down Expand Up @@ -52,6 +52,10 @@ function is_color_enabled() (
echo-non-affirmative -- "$NO_COLOR"
elif test -n "${NOCOLOR-}"; then
echo-non-affirmative -- "$NOCOLOR"
elif test -n "${CRON-}" -o -n "${CRONITOR_EXEC-}" -o -z "${TERM-}" -o "${TERM-}" = 'dumb'; then
# cron strips most env vars, except that defined in [crontab -e], bash however does itself set TERM=dumb
# https://unix.stackexchange.com/a/411097
print_line 'no'
else
print_line "$default"
fi
Expand Down
6 changes: 5 additions & 1 deletion commands/is-desktop-session
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
function is_desktop_session() (
source "$DOROTHY/sources/bash.bash"

is-mac || test -n "${XDG_CURRENT_DESKTOP-}" -o -n "${DESKTOP_SESSION-}"
if is-mac || test -n "${XDG_CURRENT_DESKTOP-}" -o -n "${DESKTOP_SESSION-}"; then
return 0
else
return 1
fi
)

# fire if invoked standalone
Expand Down
6 changes: 5 additions & 1 deletion commands/is-digit
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
function is_digit() (
source "$DOROTHY/sources/bash.bash"

is-number "$1" && test "$1" -ge 0 -a "$1" -le 9
if is-number "$1" && test "$1" -ge 0 -a "$1" -le 9; then
return 0
else
return 1
fi
)

# fire if invoked standalone
Expand Down
6 changes: 5 additions & 1 deletion commands/is-dnf
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ function is_dnf() (
# =====================================
# Action

command-exists dnf
if command-exists dnf; then
return 0
else
return 1
fi
)

# fire if invoked standalone
Expand Down
6 changes: 5 additions & 1 deletion commands/is-empty-ls
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
function is_empty_ls() (
source "$DOROTHY/sources/bash.bash"

test "$(ls -A "$1")" = ''
if test -z "$(ls -A "$1")"; then
return 0
else
return 1
fi
)

# fire if invoked standalone
Expand Down
6 changes: 5 additions & 1 deletion commands/is-empty-size
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
function is_empty_size() (
source "$DOROTHY/sources/bash.bash"

test "$(du -s "$1")" = $'0\t'"$1"
if test "$(du -s "$1")" = $'0\t'"$1"; then
return 0
else
return 1
fi
)

# fire if invoked standalone
Expand Down
6 changes: 4 additions & 2 deletions commands/is-empty-string
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ function is_empty_string() (
help 'No <input> provided.'
elif test "$*" = '--help'; then
help
elif [[ $* =~ ^[[:space:]]*$ ]]; then
# value is only whitespace characters
return 0
else
# check if the value only whitespace characters
[[ $* =~ ^[[:space:]]*$ ]]
return 1
fi
)

Expand Down
8 changes: 6 additions & 2 deletions commands/is-empty-value
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ function is_empty_value() (
local value="${1-}"

# check for empty values, or check for an empty string
test -z "$value" -o \
if test -z "$value" -o \
"$value" = 'null' -o "$value" = 'NULL' -o \
"$value" = 'void' -o "$value" = 'VOID' -o \
"$value" = 'undefined' -o "$value" = 'UNDEFINED' || is-empty-string "$value"
"$value" = 'undefined' -o "$value" = 'UNDEFINED' || is-empty-string "$value"; then
return 0
else
return 1
fi

# don't consider 0 empty as version-compare believes 0 is a valid value
)
Expand Down
6 changes: 4 additions & 2 deletions commands/is-generic
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ function is_generic() (
help
elif test -z "$*"; then
return 0 # is generic
else
elif [[ $* =~ ubuntu|root|admin|super|user|localhost ]]; then
# don't include [local], as macos hostnames end in [.local]
[[ $* =~ ubuntu|root|admin|super|user|localhost ]]
return 0
else
return 1
fi
)

Expand Down
Loading

0 comments on commit 3b85eb1

Please sign in to comment.