Skip to content

Commit

Permalink
Simplify parameter expansion defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamVig committed Jan 27, 2023
1 parent b45693f commit cfaade0
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 38 deletions.
4 changes: 2 additions & 2 deletions bin/,remotecontrol
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ Commands:
Flags:
-h, --help show this help text"

if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then
if [ "${1-}" = "-h" ] || [ "${1-}" = "--help" ]; then
echo "$usage"
exit 0
fi

readonly command="${1:-}"
readonly command="${1-}"

if [ -z "$command" ]; then
echo 'error: must provide a command'
Expand Down
8 changes: 4 additions & 4 deletions bin/,update
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Update applications, packages, and tools.
Flags:
-h, --help show this help text"

if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then
if [ "${1-}" = "-h" ] || [ "${1-}" = "--help" ]; then
echo "$usage"
exit 0
fi
Expand All @@ -23,7 +23,7 @@ background_red="$(tput setab 1)"
reset="$(tput sgr0)"

log_operation() {
operation="${1:-}"
operation="${1-}"
if [ -z "$operation" ]; then
echo 'internal error: log_operation called without an operation'
return 1
Expand All @@ -43,13 +43,13 @@ log_failure() {
# $1: PID of process to wait for
# $2: path to file containing output to log
log_output() {
pid="${1:-}"
pid="${1-}"
if [ -z "$pid" ]; then
echo 'internal error: log_output called without PID'
return 1
fi

output_path="${2:-}"
output_path="${2-}"
if [ -z "$output_path" ]; then
echo 'internal error: log_output called without output path'
return 1
Expand Down
4 changes: 2 additions & 2 deletions bin/complex-files
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ Arguments:
Flags:
-h, --help show this help text"

if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then
if [ "${1-}" = "-h" ] || [ "${1-}" = "--help" ]; then
echo "$usage"
exit 0
fi

path="${1:-}"
path="${1-}"
if [ -z "$path" ]; then
echo 'error: must pass a path'
exit 1
Expand Down
2 changes: 1 addition & 1 deletion bin/github-link
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ while true; do
done

# Read from argument
url="${1:-}"
url="${1-}"
# If argument is missing and stdin is not a terminal, read from stdin
if [ -z "$url" ] && ! [ -t 0 ]; then
url="$(</dev/stdin)"
Expand Down
8 changes: 4 additions & 4 deletions bin/home-assistant-daemon
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

set -euo pipefail

if [ -z "${HOME_ASSISTANT_HOST:-}" ]; then
if [ -z "${HOME_ASSISTANT_HOST-}" ]; then
echo >&2 'error: HOME_ASSISTANT_HOST must be set'
exit 1
fi

if [ -z "${HOME_ASSISTANT_TOKEN:-}" ]; then
if [ -z "${HOME_ASSISTANT_TOKEN-}" ]; then
echo >&2 'error: HOME_ASSISTANT_TOKEN must be set'
exit 1
fi
Expand Down Expand Up @@ -42,8 +42,8 @@ main() {
# $1: event JSON
# $2: key in event's .event.data object
get_event_data() {
event="${1:-}"
key="${2:-}"
event="${1-}"
key="${2-}"
if [ -z "$event" ]; then
log 'missing event'
return 1
Expand Down
2 changes: 1 addition & 1 deletion bin/jira-link
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ while true; do
done

# Read from argument
url="${1:-}"
url="${1-}"
# If argument is missing and stdin is not a terminal, read from stdin
if [ -z "$url" ] && ! [ -t 0 ]; then
url="$(</dev/stdin)"
Expand Down
2 changes: 1 addition & 1 deletion bin/merge-latest
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Arguments:
Flags:
-h, --help show this help text"

if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then
if [ "${1-}" = "-h" ] || [ "${1-}" = "--help" ]; then
echo "$usage"
exit 0
fi
Expand Down
6 changes: 3 additions & 3 deletions bin/next-default-sink
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ Toggle the default sink to the next available sink using pactl.
Flags:
-h, --help show this help text"

if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then
if [ "${1-}" = "-h" ] || [ "${1-}" = "--help" ]; then
echo "$usage"
exit 0
fi

speakers_sink="${SPEAKERS_SINK:-}"
speakers_sink="${SPEAKERS_SINK-}"
if [ -z "$speakers_sink" ]; then
echo 'error: must set SPEAKERS_SINK environment variable'
exit 1
fi

headphones_sink="${HEADPHONES_SINK:-}"
headphones_sink="${HEADPHONES_SINK-}"
if [ -z "$headphones_sink" ]; then
echo 'error: must set HEADPHONES_SINK environment variable'
exit 1
Expand Down
4 changes: 2 additions & 2 deletions bin/status-pulseaudio
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ get_volume_percentage() {
# $1: name of default sink
get_sink_type() {
sink_name="${1:?missing sink name}"
headphones_sink="${HEADPHONES_SINK:-}"
speakers_sink="${SPEAKERS_SINK:-}"
headphones_sink="${HEADPHONES_SINK-}"
speakers_sink="${SPEAKERS_SINK-}"
if [ -z "$speakers_sink" ] || [ -z "$headphones_sink" ]; then
return
fi
Expand Down
10 changes: 5 additions & 5 deletions lib/apt
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ is_repository_configured() {
# $3: latter part of repository configuration string, e.g. "http://repository.spotify.com stable non-free", not
# including the arch or signed-by options
add_repository() {
local friendly_name="${1:-}"
local key_url="${2:-}"
local repo_config="${3:-}"
local friendly_name="${1-}"
local key_url="${2-}"
local repo_config="${3-}"

local key_path=/usr/share/keyrings/"$friendly_name"-archive-keyring.gpg

Expand All @@ -56,8 +56,8 @@ add_repository() {
# $3: latter part of repository configuration string, e.g. "http://repository.spotify.com stable non-free", not
# including the arch or signed-by options
add_ppa() {
local key_fingerprint="${2:-}"
local repo_name="${3:-}"
local key_fingerprint="${2-}"
local repo_name="${3-}"

add_repository \
"$1" \
Expand Down
26 changes: 13 additions & 13 deletions lib/helpers
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ log() {
# $1: name of an operation
# $@: passed to log
log_start() {
operation_name="${1:-}"
operation_name="${1-}"
if [ -z "$operation_name" ]; then
echo 'internal error: log_start called without an operation name'
return 1
Expand Down Expand Up @@ -74,7 +74,7 @@ log_end() {
# $1: component name
# $@: passed to log
log_with_component() {
component="${1:-}"
component="${1-}"
if [ -z "$component" ]; then
echo 'internal error: log_with_component called without a component'
return 1
Expand Down Expand Up @@ -133,7 +133,7 @@ _ln() {
# Get the latest GitHub release in JSON format
# $1: GitHub repository URL in format: [owner]/[repo name]
get_latest_github_release() {
repository="${1:-}"
repository="${1-}"

url="https://api.github.com/repos/${repository}/releases/latest"
if ! latest_release="$(_curl "$url")"; then
Expand All @@ -149,8 +149,8 @@ get_latest_github_release() {
# $2: pattern to match
# $3: optional matching mode: startswith, contains, or endswith (default endswith)
get_release_url() {
repository="${1:-}"
pattern="${2:-}"
repository="${1-}"
pattern="${2-}"
matching_mode="${3:-endswith}"
# Check if jq is available for JSON parsing
if ! command -v jq >/dev/null; then
Expand All @@ -169,8 +169,8 @@ get_release_url() {
# $1: binary name
# $2: URL
install_binary_from_url() {
local name="${1:-}"
local url="${2:-}"
local name="${1-}"
local url="${2-}"
local path="$HOME"/.local/bin/"$name"
output_path="$(mktemp)"

Expand All @@ -189,9 +189,9 @@ install_binary_from_url() {
# $2: repository URL in format: [owner]/[repo name]
# $3: pattern to match
install_binary_from_github_release() {
local name="${1:-}"
local repository="${2:-}"
local pattern="${3:-}"
local name="${1-}"
local repository="${2-}"
local pattern="${3-}"

if ! url="$(get_release_url "$repository" "$pattern")"; then
fatal 'failed to get release URL'
Expand All @@ -205,7 +205,7 @@ install_binary_from_github_release() {
# Install a .deb from a URL
# $1: url
install_deb_from_url() {
local url="${1:-}"
local url="${1-}"
output_path="$(mktemp --suffix='.deb')"

if ! _curl "$url" --output "$output_path"; then
Expand All @@ -226,8 +226,8 @@ install_deb_from_url() {
# $1: repository URL in format: [owner]/[repo name]
# $2: pattern to match
install_deb_from_github_release() {
local repository="${1:-}"
local pattern="${2:-}"
local repository="${1-}"
local pattern="${2-}"

if ! url="$(get_release_url "$repository" "$pattern")"; then
fatal 'failed to get release URL'
Expand Down

0 comments on commit cfaade0

Please sign in to comment.