Skip to content

Commit

Permalink
Environment variable to prevent automatic configuration file creation. (
Browse files Browse the repository at this point in the history
#64)

* Environment variable to prevent config file overwriting.

The variable `PGENV_WRITE_CONFIGURATION_FILE_AUTOMATICALLY`
is used to instrument `pgenv` to decide about the need to
write/overwrite the configuration file at the end
of a build/rebuild process.

If the variable is not set, or set to a true value (i.e., not zero,
not "NO" - case insensitive), the configuration file will be
written and/or overwritten.
If the variable is set to zero or "no" (case insensitive), the
configuration
file will not be written.

Close #62
  • Loading branch information
fluca1978 authored Sep 28, 2023
1 parent 64740d6 commit ea37034
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,16 @@ interpreter, pass it on the command line at the time of build:
PERL=/usr/local/my-fancy-perl pgenv build 10.5
```
At the end of a `build` (or a `rebuild`) phase, `pgenv` creates a configuration
file for the specific PostgreSQL version. If the file already exists, due to
a prior `build` or `rebuild` action, the file will be automatically overwritten.
In order to avoid the creation or overwriting of the configuration file,
it is possible to set the environment variable
`PGENV_WRITE_CONFIGURATION_FILE_AUTOMATICALLY` to a false value
(either `0` or `NO`). If the variable is not set at all,
or it is set to a true value (e.g., `1`, `YES`, etc.)
the configuration file will be created or overwritten (if it already exists).
#### Patching
`pgenv` can patch the source tree before the build process starts. In
Expand Down Expand Up @@ -796,7 +806,9 @@ special `default` keyowrd, for example:
```
export PGENV_CONFIGURATION_FILE=pgenv config path default
```
### pgenv log
The `log` command provides a dump of the cluster log, if it exists, so that you
Expand Down
32 changes: 22 additions & 10 deletions bin/pgenv
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# VERSION
#
PGENV_VERSION="1.3.2"
PGENV_VERSION="1.3.3"

# https://stackoverflow.com/a/19622569/79202
trap 'exit' ERR
Expand Down Expand Up @@ -454,7 +454,7 @@ pgenv_configuration_file_name(){
# if the user has defined an environment variable PGENV_CONFIGURATION_FILE
# I need to use that instead of trying to understand which file to use
if [ ! -z "$PGENV_CONFIGURATION_FILE" -a -z "$skip_env" ]; then
pgenv_debug "Configuration file overwritten by environment variable PGENV_CONFIGURATION_FILE = $PGENV_CONFIGURATION_FILE"
pgenv_debug "Configuration file forced by environment variable PGENV_CONFIGURATION_FILE = $PGENV_CONFIGURATION_FILE"
echo "${PGENV_CONFIGURATION_FILE}"
return
fi
Expand Down Expand Up @@ -675,10 +675,12 @@ pgenv_configuration_write_variable(){
# or the version file specified. It does a backup copy in the case
# the file already exists.
#
# If the environment variable PGENV_CONFIGURATION_FILE is set
# no configuration file (over)writing will happen: it is supposed
# the user has declared the variable and thus it is not required to
# update the configuration.
# If the environment variable PGENV_WRITE_CONFIGURATION_FILE_AUTOMATICALLY
# is set to a non-empty, non-zero value, the configuration file
# will be overwritten. In the case the variable is not set at all
# the application will apply the backward compatible behavior, so
# not setting the PGENV_CONFIGURATION_FILE_AUTOMATICALLY is like
# setting it to "YES".
pgenv_configuration_write() {
local v=$1
CONF=$( pgenv_configuration_file_name $v )
Expand All @@ -689,12 +691,22 @@ pgenv_configuration_write() {
exit 1
fi

# avoid overwriting configuration if the environment variable is set
if [ ! -z "${PGENV_CONFIGURATION_FILE}" ]; then
pgenv_debug "Cannot write configuration file while PGENV_CONFIGURATION_FILE set!"
return

# check if we need to write the configuration file automatically:
# if the variable is not set, assume the user wants the backward
# compatible behavior: write the configuration file automatically
if [ -z "$PGENV_WRITE_CONFIGURATION_FILE_AUTOMATICALLY" ]; then
PGENV_WRITE_CONFIGURATION_FILE_AUTOMATICALLY="YES"
fi

# in the case the variable is set to a non-yes value, avoid overwriting
case "$PGENV_WRITE_CONFIGURATION_FILE_AUTOMATICALLY" in
0|[nN][oO])
pgenv_debug "Not writing config file automatically: set \`PGENV_WRITE_CONFIGURATION_FILE_AUTOMATICALLY\` to a true value to enable the automatic file writing"
return
;;
esac

# check the configuration directory exists
if [ ! -d "$PGENV_CONFIG_ROOT" ]; then
mkdir -p "$PGENV_CONFIG_ROOT"
Expand Down

0 comments on commit ea37034

Please sign in to comment.