Add support for config file.

This commit is contained in:
Daniel Perez 2016-04-24 22:20:05 +09:00
parent cf942c8642
commit d4ee3aa999
4 changed files with 77 additions and 0 deletions

8
.travis.yml Normal file
View File

@ -0,0 +1,8 @@
language: c
script: bats test
before_script:
- git clone https://github.com/sstephenson/bats.git /tmp/bats
- export PATH=/tmp/bats/bin:$PATH
os:
- linux
- osx

2
defaults Normal file
View File

@ -0,0 +1,2 @@
# enables the use of .ruby-version like files used by other version managers
legacy_version_file = no

View File

@ -121,3 +121,31 @@ get_tool_version_from_file() {
echo $matching_tool_version
}
get_asdf_config_value_from_file() {
local config_path=$1
local key=$2
if [ ! -f $config_path ]; then
return 0
fi
local result=$(grep -E "^\s*$key\s*=" $config_path | awk -F '=' '{ gsub(/ /, "", $2); print $2 }')
if [ -n "$result" ]; then
echo $result
fi
}
get_asdf_config_value() {
local key=$1
local config_path=${AZDF_CONFIG_FILE:-"$HOME/.asdfrc"}
local default_config_path=${AZDF_CONFIG_DEFAULT_FILE:-"$(asdf_dir)/defaults"}
local result=$(get_asdf_config_value_from_file $config_path $key)
if [ -n "$result" ]; then
echo $result
else
get_asdf_config_value_from_file $default_config_path $key
fi
}

View File

@ -0,0 +1,39 @@
#!/usr/bin/env bats
. $(dirname $BATS_TEST_DIRNAME)/lib/utils.sh
setup() {
AZDF_CONFIG_FILE=$BATS_TMPDIR/asdfrc
cat > $AZDF_CONFIG_FILE <<-EOM
key1 = value1
legacy_version_file = yes
EOM
AZDF_CONFIG_DEFAULT_FILE=$BATS_TMPDIR/asdfrc_defaults
cat > $AZDF_CONFIG_DEFAULT_FILE <<-EOM
# i have a comment, it's ok
key2 = value2
legacy_version_file = no
EOM
}
teardown() {
rm $AZDF_CONFIG_FILE
rm $AZDF_CONFIG_DEFAULT_FILE
unset AZDF_CONFIG_DEFAULT_FILE
unset AZDF_CONFIG_FILE
}
@test "get_config returns default when config file does not exist" {
result=$(AZDF_CONFIG_FILE="/some/fake/path" get_asdf_config_value "legacy_version_file")
[ "$result" = "no" ]
}
@test "get_config returns default value when the key does not exist" {
[ $(get_asdf_config_value "key2") = "value2" ]
}
@test "get_config returns config file value when key exists" {
[ $(get_asdf_config_value "key1") = "value1" ]
[ $(get_asdf_config_value "legacy_version_file") = "yes" ]
}