Skip to content

Parse a configuration file #274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 73 additions & 8 deletions script/install.sh
Original file line number Diff line number Diff line change
@@ -124,17 +124,19 @@ check_tools() {

# ------------------------------------------------------------------------------

mkdir -p "$ROOT"
TMP_ROOT=$(mktemp -d "$ROOT/.installer_tmp_XXXXXX")
init() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be done after the configuration and CLI arguments are read.
Note that at the moment even if --path was set, the temporary directory was ~/dlang/.installer_tmp.

mkdir -p "$ROOT"
TMP_ROOT=$(mktemp -d "$ROOT/.installer_tmp_XXXXXX")

mkdtemp() {
mktemp -d "$TMP_ROOT/XXXXXX"
}
mkdtemp() {
mktemp -d "$TMP_ROOT/XXXXXX"
}

cleanup() {
rm -rf "$TMP_ROOT";
cleanup() {
rm -rf "$TMP_ROOT";
}
trap cleanup EXIT
}
trap cleanup EXIT

# ------------------------------------------------------------------------------

@@ -155,12 +157,31 @@ Options
-h --help Show this help
-p --path Install location (default ~/dlang)
-v --verbose Verbose output
--config List all available configuration parameters
Run "install.sh <command> --help to get help for a specific command.
If no argument are provided, the latest DMD compiler will be installed.
'
}

command_config() {
log 'Configuration
~/.config/dlang/installer.cfg can be used to store permanent configuration settings.
Alternatively a global configuration file can be used (/etc/dlang/installer.cfg).
The parameters are listed in line-wise a <param>=<value> format.
Lines starting with # are ignored.
Example:
ROOT=~/.dlang
Available options:
ROOT Install location (default ~/dlang)
'
}

command_help() {
if [ -z "${1-}" ]; then
usage
@@ -283,6 +304,11 @@ parse_args() {
COMPILER=$1
;;

--config)
command_config
exit 0
;;

*)
usage
fatal "Unrecognized command-line parameter: $1"
@@ -685,9 +711,48 @@ install_dub() {
ln -s "$dub" "$ROOT/dub"
}

# ------------------------------------------------------------------------------
# Read user configuration
# ------------------------------------------------------------------------------

local config_file config_files
config_files=(
"${XDG_CONFIG_HOME:-$HOME/.config/}/dlang/installer.cfg" \
"${XDG_CONFIG_DIRS:-/etc}/dlang/installer.cfg" \
"$HOME/Library/Application Support/dlang/installer.cfg" \
)
config_file=""
for file in "${config_files[@]}" ; do
if [ -e "${file}" ] ; then
config_file="${file}"
fi
done
if [ -n "${config_file}" ] ; then
# Use a whitelist of allowed parameters
allowedParams=("ROOT")
containsValue() {
local e value="$1"
shift
for e; do [[ "${e}" == "${value}" ]] && return 0; done
return 1
}
IFS="="
# Ignore empty lines or lines with comments
grep -vE "^(#|$)" "${config_file}" | while read -r param value ; do
if containsValue "${param}" "${allowedParams[@]}" ; then
log "${param}: ${value//\"/}"
declare -a "${param}=${value//\"/}"
else
log "${param} is invalid. Please check."
fi
done
ROOT="${ROOT/#\~/$HOME}"
fi

# ------------------------------------------------------------------------------

parse_args "$@"
init
resolve_latest "$COMPILER"
run_command ${COMMAND:-install} "$COMPILER"
}
16 changes: 16 additions & 0 deletions travis.sh
Original file line number Diff line number Diff line change
@@ -91,3 +91,19 @@ git checkout -- script/install.sh
if [ "${TRAVIS_OS_NAME:-}" != "osx" ]; then
shellcheck script/install.sh
fi

# Test user config
mkdir -p ~/.config/dlang
echo "ROOT=~/.dlang" > ~/.config/dlang/installer.cfg
./script/install.sh dmd
has_user_installed_dmd=0
for file in ~/.dlang/dmd* ; do
if [ -e "$file" ] ; then
has_user_installed_dmd=1
break
fi
done
if [ ${has_user_installed_dmd} -eq 0 ] ; then
echo "Failed to install DMD in the user-specified directory."
fi
rm -rf ~/.dlang ~/.config