Skip to content

Commit d4ee3aa

Browse files
author
Daniel Perez
committed
Add support for config file.
1 parent cf942c8 commit d4ee3aa

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: c
2+
script: bats test
3+
before_script:
4+
- git clone https://github.com/sstephenson/bats.git /tmp/bats
5+
- export PATH=/tmp/bats/bin:$PATH
6+
os:
7+
- linux
8+
- osx

defaults

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# enables the use of .ruby-version like files used by other version managers
2+
legacy_version_file = no

lib/utils.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,31 @@ get_tool_version_from_file() {
121121

122122
echo $matching_tool_version
123123
}
124+
125+
get_asdf_config_value_from_file() {
126+
local config_path=$1
127+
local key=$2
128+
129+
if [ ! -f $config_path ]; then
130+
return 0
131+
fi
132+
133+
local result=$(grep -E "^\s*$key\s*=" $config_path | awk -F '=' '{ gsub(/ /, "", $2); print $2 }')
134+
if [ -n "$result" ]; then
135+
echo $result
136+
fi
137+
}
138+
139+
get_asdf_config_value() {
140+
local key=$1
141+
local config_path=${AZDF_CONFIG_FILE:-"$HOME/.asdfrc"}
142+
local default_config_path=${AZDF_CONFIG_DEFAULT_FILE:-"$(asdf_dir)/defaults"}
143+
144+
local result=$(get_asdf_config_value_from_file $config_path $key)
145+
146+
if [ -n "$result" ]; then
147+
echo $result
148+
else
149+
get_asdf_config_value_from_file $default_config_path $key
150+
fi
151+
}

test/get_asdf_config_value.bats

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env bats
2+
3+
. $(dirname $BATS_TEST_DIRNAME)/lib/utils.sh
4+
5+
setup() {
6+
AZDF_CONFIG_FILE=$BATS_TMPDIR/asdfrc
7+
cat > $AZDF_CONFIG_FILE <<-EOM
8+
key1 = value1
9+
legacy_version_file = yes
10+
EOM
11+
12+
AZDF_CONFIG_DEFAULT_FILE=$BATS_TMPDIR/asdfrc_defaults
13+
cat > $AZDF_CONFIG_DEFAULT_FILE <<-EOM
14+
# i have a comment, it's ok
15+
key2 = value2
16+
legacy_version_file = no
17+
EOM
18+
}
19+
20+
teardown() {
21+
rm $AZDF_CONFIG_FILE
22+
rm $AZDF_CONFIG_DEFAULT_FILE
23+
unset AZDF_CONFIG_DEFAULT_FILE
24+
unset AZDF_CONFIG_FILE
25+
}
26+
27+
@test "get_config returns default when config file does not exist" {
28+
result=$(AZDF_CONFIG_FILE="/some/fake/path" get_asdf_config_value "legacy_version_file")
29+
[ "$result" = "no" ]
30+
}
31+
32+
@test "get_config returns default value when the key does not exist" {
33+
[ $(get_asdf_config_value "key2") = "value2" ]
34+
}
35+
36+
@test "get_config returns config file value when key exists" {
37+
[ $(get_asdf_config_value "key1") = "value1" ]
38+
[ $(get_asdf_config_value "legacy_version_file") = "yes" ]
39+
}

0 commit comments

Comments
 (0)