mirror of
https://github.com/asdf-vm/asdf.git
synced 2024-11-15 01:28:17 -07:00
Add support for config file.
This commit is contained in:
parent
cf942c8642
commit
d4ee3aa999
8
.travis.yml
Normal file
8
.travis.yml
Normal 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
2
defaults
Normal file
@ -0,0 +1,2 @@
|
||||
# enables the use of .ruby-version like files used by other version managers
|
||||
legacy_version_file = no
|
28
lib/utils.sh
28
lib/utils.sh
@ -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
|
||||
}
|
||||
|
39
test/get_asdf_config_value.bats
Normal file
39
test/get_asdf_config_value.bats
Normal 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" ]
|
||||
}
|
Loading…
Reference in New Issue
Block a user