Skip to content

Commit 60fe753

Browse files
authored
Merge pull request #108 from DannyBen/fix/107
Fix config library context contamination
2 parents e9b9118 + e384f8e commit 60fe753

File tree

10 files changed

+187
-13
lines changed

10 files changed

+187
-13
lines changed

lib/bashly/templates/lib/config.sh

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ config_init() {
2020
# Get a value from the config.
2121
# Usage: result=$(config_get hello)
2222
config_get() {
23-
key=$1
24-
regex="^$key *= *(.+)$"
23+
local key=$1
24+
local regex="^$key *= *(.+)$"
25+
local value=""
2526

2627
config_init
2728

@@ -38,15 +39,16 @@ config_get() {
3839
# Add or update a key=value pair in the config.
3940
# Usage: config_set key value
4041
config_set() {
41-
key=$1
42+
local key=$1
4243
shift
43-
value="$*"
44+
local value="$*"
4445

4546
config_init
4647

47-
regex="^($key) *= *.+$"
48-
output=""
49-
found_key=""
48+
local regex="^($key) *= *.+$"
49+
local output=""
50+
local found_key=""
51+
local newline
5052

5153
while IFS= read -r line || [ -n "$line" ]; do
5254
newline=$line
@@ -69,15 +71,14 @@ config_set() {
6971
# Delete a key from the config.
7072
# Usage: config_del key
7173
config_del() {
72-
key=$1
74+
local key=$1
7375

74-
regex="^($key) *="
75-
output=""
76+
local regex="^($key) *="
77+
local output=""
7678

7779
config_init
7880

7981
while IFS= read -r line || [ -n "$line" ]; do
80-
newline=$line
8182
if [[ $line ]] && [[ ! $line =~ $regex ]]; then
8283
output="$output$line\n"
8384
fi
@@ -100,11 +101,13 @@ config_show() {
100101
# done
101102
#
102103
config_keys() {
103-
regex="^([a-zA-Z0-9_\-\/\.]+) *="
104+
local regex="^([a-zA-Z0-9_\-\/\.]+) *="
104105

105106
config_init
106107

107-
keys=()
108+
local keys=()
109+
local key
110+
108111
while IFS= read -r line || [ -n "$line" ]; do
109112
if [[ $line =~ $regex ]]; then
110113
key="${BASH_REMATCH[1]}"

spec/approvals/examples/config-lib

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
+ bundle exec bashly add config --force
2+
created src/lib/config.sh
3+
+ bundle exec bashly generate
4+
creating user files in src
5+
skipped src/initialize.sh (exists)
6+
skipped src/root_command.sh (exists)
7+
created ./download
8+
run ./download --help to test your bash script
9+
+ ./download
10+
foo=BOOM
11+
bar=
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
download
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This fixture tests that the config lib scope bug
2+
Reference issue: https://github.com/DannyBen/bashly/issues/107
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
foo = BOOM
2+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name: download
2+
help: Sample minimal application without commands
3+
version: 0.1.0
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Code here runs inside the initialize() function
2+
# Use it for anything that you need to run before any other function, like
3+
# setting environment vairables:
4+
# CONFIG_FILE=settings.ini
5+
#
6+
# Feel free to empty (but not delete) this file.
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# ---
2+
# Config functions
3+
# This file is a part of Bashly standard library
4+
#
5+
# Usage:
6+
# - In your script, set the CONFIG_FILE variable. For rxample:
7+
# CONFIG_FILE=settings.ini.
8+
# If it is unset, it will default to 'config.ini'.
9+
# - Use any of the functions below to access the config file.
10+
# ---
11+
12+
# Create a new config file.
13+
# There is normally no need to use this function, it is used by other
14+
# functions as needed.
15+
config_init() {
16+
CONFIG_FILE=${CONFIG_FILE:=config.ini}
17+
[[ -f "$CONFIG_FILE" ]] || touch "$CONFIG_FILE"
18+
}
19+
20+
# Get a value from the config.
21+
# Usage: result=$(config_get hello)
22+
config_get() {
23+
local key=$1
24+
local regex="^$key *= *(.+)$"
25+
local value=""
26+
27+
config_init
28+
29+
while IFS= read -r line || [ -n "$line" ]; do
30+
if [[ $line =~ $regex ]]; then
31+
value="${BASH_REMATCH[1]}"
32+
break
33+
fi
34+
done < "$CONFIG_FILE"
35+
36+
echo "$value"
37+
}
38+
39+
# Add or update a key=value pair in the config.
40+
# Usage: config_set key value
41+
config_set() {
42+
local key=$1
43+
shift
44+
local value="$*"
45+
46+
config_init
47+
48+
local regex="^($key) *= *.+$"
49+
local output=""
50+
local found_key=""
51+
local newline
52+
53+
while IFS= read -r line || [ -n "$line" ]; do
54+
newline=$line
55+
if [[ $line =~ $regex ]]; then
56+
found_key="${BASH_REMATCH[1]}"
57+
newline="$key = $value"
58+
output="$output$newline\n"
59+
elif [[ $line ]]; then
60+
output="$output$line\n"
61+
fi
62+
done < "$CONFIG_FILE"
63+
64+
if [[ -z $found_key ]]; then
65+
output="$output$key = $value\n"
66+
fi
67+
68+
printf "%b\n" "$output" > "$CONFIG_FILE"
69+
}
70+
71+
# Delete a key from the config.
72+
# Usage: config_del key
73+
config_del() {
74+
local key=$1
75+
76+
local regex="^($key) *="
77+
local output=""
78+
79+
config_init
80+
81+
while IFS= read -r line || [ -n "$line" ]; do
82+
if [[ $line ]] && [[ ! $line =~ $regex ]]; then
83+
output="$output$line\n"
84+
fi
85+
done < "$CONFIG_FILE"
86+
87+
printf "%b\n" "$output" > "$CONFIG_FILE"
88+
}
89+
90+
# Show the config file
91+
config_show() {
92+
config_init
93+
cat "$CONFIG_FILE"
94+
}
95+
96+
# Return an array of the keys in the config file.
97+
# Usage:
98+
#
99+
# for k in $(config_keys); do
100+
# echo "- $k = $(config_get "$k")";
101+
# done
102+
#
103+
config_keys() {
104+
local regex="^([a-zA-Z0-9_\-\/\.]+) *="
105+
106+
config_init
107+
108+
local keys=()
109+
local key
110+
111+
while IFS= read -r line || [ -n "$line" ]; do
112+
if [[ $line =~ $regex ]]; then
113+
key="${BASH_REMATCH[1]}"
114+
keys+=("$key")
115+
fi
116+
done < "$CONFIG_FILE"
117+
echo "${keys[@]}"
118+
}
119+
120+
# Returns true if the specified key exists in the config file.
121+
# Usage:
122+
#
123+
# if config_has_key "key" ; then
124+
# echo "key exists"
125+
# fi
126+
#
127+
config_has_key() {
128+
[[ $(config_get "$1") ]]
129+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
config_set foo BOOM
2+
echo "foo=$(config_get foo)"
3+
echo "bar=$(config_get bar)"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
3+
# This fixture tests that the config lib scope bug
4+
# It is executed as part of the Runfile examples test
5+
# Reference issue: https://github.com/DannyBen/bashly/issues/107
6+
7+
rm -f ./download
8+
9+
set -x
10+
11+
bundle exec bashly add config --force
12+
bundle exec bashly generate
13+
14+
./download

0 commit comments

Comments
 (0)