Skip to content

Commit

Permalink
Merge branch 'remove_which'.
Browse files Browse the repository at this point in the history
Susbstitute all the `which` commands to `command -v`.

`command` is a Bash specific way to check and launch a specific
executable. If invoked with -v it provides the full path
to the executable, and it should be faster than invoking `which` in another
process.
Since this script is Bash-dependent, this does not imply any particular
qchange in the script behavior.

Tested against OSX Bash and Ubuntu Bash 5.0.3.

Protect `log` with `command`.

Since `log` executes `tail`, and the user could not have been running
`check` before, it is possible to protect the log action using `command`,
that will fail if no `tail` is in place (that is, in turn, a serious problem
for the user!).

Improve the `config edit` command.

Now there is a check for the EDITOR variable to contain an executable
that can be launched, otherwise the error message is shown.

Close #42
  • Loading branch information
fluca1978 committed Nov 16, 2020
1 parent 2e9a00c commit 71d4dc4
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions bin/pgenv
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pgenv_versions() {
fi

if [ -z "$PGENV_SED" ]; then
PGENV_SED=$(which sed)
PGENV_SED=$(command -v sed)
fi
local version=$( echo $dir | $PGENV_SED 's/^pgsql-//' )
printf "%s %-6s %s\n" "$flags" "$version" "$dir"
Expand Down Expand Up @@ -131,40 +131,40 @@ pgenv_check_dependencies(){


if [ -z "$PGENV_MAKE" ]; then
PGENV_MAKE=$(which make)
PGENV_MAKE=$(command -v make)
if [ -z "$PGENV_MAKE" ]; then
PGENV_MAKE=$(which gmake)
PGENV_MAKE=$(command -v gmake)
fi
fi
pgenv_detail_command "make" $PGENV_MAKE


if [ -z "$PGENV_CURL" ]; then
PGENV_CURL=$(which curl)
PGENV_CURL=$(command -v curl)
fi
pgenv_detail_command "curl" $PGENV_CURL

if [ -z "$PGENV_PATCH" ]; then
PGENV_PATCH=$(which patch)
PGENV_PATCH=$(command -v patch)
fi
pgenv_detail_command "patch" $PGENV_PATCH

if [ -z "$PGENV_TAR" ]; then
PGENV_TAR=$(which tar)
PGENV_TAR=$(command -v tar)
fi
pgenv_detail_command "tar" $PGENV_TAR

if [ -z "$PGENV_SED" ]; then
PGENV_SED=$(which sed)
PGENV_SED=$(command -v sed)
fi
pgenv_detail_command "sed" $PGENV_SED


# check for sort and tr and tail, but not place into
# variables (do we need to push to that extent?)
pgenv_detail_command "sort" $(which sort)
pgenv_detail_command "tr" $(which tr)
pgenv_detail_command "tail" $(which tail)
pgenv_detail_command "sort" $(command -v sort)
pgenv_detail_command "tr" $(command -v tr)
pgenv_detail_command "tail" $(command -v tail)

# Go back to exiting on error.
trap 'exit' ERR
Expand Down Expand Up @@ -370,7 +370,7 @@ EOF
pgenv_current_postgresql_version(){
# find sed even without the configuration/dependencies
if [ -z "$PGENV_SED" ]; then
PGENV_SED=$(which sed)
PGENV_SED=$(command -v sed)
fi
local v=$( readlink pgsql | $PGENV_SED 's/^pgsql-//' )
echo $v
Expand Down Expand Up @@ -1304,11 +1304,12 @@ EOF
pgenv_configuration_write "$v" ;;
edit)
configuration_file=$( pgenv_configuration_file_name $v )
if [ ! -z "$EDITOR" ]; then
if [ ! -z "$EDITOR" ] && [ $(command -v $EDITOR ) ]; then
pgenv_debug "Launching editor $EDITOR"
$EDITOR $configuration_file
else
cat <<EOF
No EDITOR variable set!
No EDITOR variable set (or executable)!
You can either start manually your favourite
editor to edit the configuration file
$configuration_file
Expand Down Expand Up @@ -1349,7 +1350,7 @@ EOF

# tail is not in a PGENV_ variable, but should have been checked
# via `pgenv check`
tail $* "$PGENV_LOG"
command tail $* "$PGENV_LOG"
exit $?
else
# uhm.. no log is there, could it be the database
Expand Down

0 comments on commit 71d4dc4

Please sign in to comment.