|
| 1 | +#! /usr/bin/env bash |
| 2 | +# |
| 3 | +# Checks for the existence and accessibility of files and commands |
| 4 | +# |
| 5 | +# Many of the actual checks are Bash builtins, but these functions provide |
| 6 | +# standard error messages to make reporting missing or inaccessible files |
| 7 | +# easier. They eliminate a lot of boilerplate from scripts that check for |
| 8 | +# initial conditions before processing files and executing commands. |
| 9 | +# |
| 10 | +# Exports: |
| 11 | +# @go.check_file_exists |
| 12 | +# Checks whether a file exists and prints an error if not |
| 13 | +# |
| 14 | +# @go.check_file_readable |
| 15 | +# Checks whether a file exists and is readable, and prints an error if not |
| 16 | +# |
| 17 | +# @go.pick_command |
| 18 | +# Selects the first installed command from a list of possible commands/names |
| 19 | +# |
| 20 | +# @go.check_command_installed |
| 21 | +# Checks whether a required command is installed on the system |
| 22 | + |
| 23 | +# Checks whether a file exists and prints an error if not. |
| 24 | +# |
| 25 | +# Arguments: |
| 26 | +# label: Label describing the type of file |
| 27 | +# file_path: Path to the file to check |
| 28 | +@go.check_file_exists() { |
| 29 | + if [[ ! -e "$2" ]]; then |
| 30 | + @go.printf "%s doesn't exist: %s\n" "$1" "$2" >&2 |
| 31 | + return 1 |
| 32 | + fi |
| 33 | +} |
| 34 | + |
| 35 | +# Checks whether a file exists and is readable, and prints an error if not. |
| 36 | +# |
| 37 | +# Arguments: |
| 38 | +# label: Label describing the type of file |
| 39 | +# file_path: Path to the file to check |
| 40 | +@go.check_file_readable() { |
| 41 | + if [[ ! -r "$2" ]]; then |
| 42 | + @go.printf "%s doesn't exist or isn't readable: %s\n" "$1" "$2" >&2 |
| 43 | + return 1 |
| 44 | + fi |
| 45 | +} |
| 46 | + |
| 47 | +# Selects the first installed command from a list of possible commands/names. |
| 48 | +# |
| 49 | +# This may be used to select from a number of different commands, or to select |
| 50 | +# the correct name for a command whose name may differ from system to system. |
| 51 | +# |
| 52 | +# Arguments: |
| 53 | +# result_var: Name of the caller-defined variable for the resulting command |
| 54 | +# ...: List of commands from which to select the first available |
| 55 | +# |
| 56 | +# Returns: |
| 57 | +# Zero if any of the commands are installed, nonzero otherwise |
| 58 | +@go.pick_command() { |
| 59 | + local __go_pick_cmd |
| 60 | + for __go_pick_cmd in "${@:2}"; do |
| 61 | + if command -v "$__go_pick_cmd" >/dev/null; then |
| 62 | + printf -v "$1" -- '%s' "$__go_pick_cmd" |
| 63 | + return |
| 64 | + fi |
| 65 | + done |
| 66 | + @go.printf 'None of the following commands were found on the system:\n' >&2 |
| 67 | + printf ' %s\n' "${@:2}" >&2 |
| 68 | + return 1 |
| 69 | +} |
| 70 | + |
| 71 | +# Checks whether a required command is installed on the system. |
| 72 | +# |
| 73 | +# If the command isn't installed, prints an error message to standard error |
| 74 | +# instructing the user to install the required command. |
| 75 | +# |
| 76 | +# Arguments: |
| 77 | +# cmd_name: Name of the command that must be required on the system |
| 78 | +# err_msg: Name of the command or other info to use in the error message |
| 79 | +# |
| 80 | +# Returns: |
| 81 | +# Zero if the command is installed, nonzero otherwise |
| 82 | +@go.check_command_installed() { |
| 83 | + if ! command -v "$1" >/dev/null; then |
| 84 | + @go.printf 'Please install %s on your system and try again.\n' \ |
| 85 | + "${2:-$1}" >&2 |
| 86 | + return 1 |
| 87 | + fi |
| 88 | +} |
0 commit comments