Skip to content

Commit 91f8ea6

Browse files
Implement crud operations for config files
1 parent 98d1fc9 commit 91f8ea6

File tree

2 files changed

+317
-0
lines changed

2 files changed

+317
-0
lines changed

bin/bash-utils-config

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#!/usr/bin/env bash
2+
3+
#: TITLE: bash-utils-config
4+
#: DATE: 2019-07-16
5+
#: VERSION: 0.1.0
6+
#: AUTHOR: "Alex Y" <aleksandryackovlev@yandex.ru>
7+
#: DESCRIPTION: CRUD for configuration files
8+
9+
[[ $bashUtilsConfigLoaded ]] && return
10+
11+
source bash-utils-env
12+
source bash-utils-utils
13+
14+
# DESCRIPTION: Sources the global config file if it exists
15+
# USAGE: readGlobalConfig
16+
readGlobalConfig() {
17+
if [[ -f $(bash-utils-env globalConfigFile) ]]; then
18+
source "$(bash-utils-env globalConfigFile)"
19+
fi
20+
}
21+
22+
# DESCRIPTION: Finds the local config file if it exists
23+
# USAGE: findLocalConfig
24+
findLocalConfig() {
25+
local currentDir
26+
local localConfigFile
27+
28+
currentDir="$PWD"
29+
localConfigFile="$currentDir/$(bash-utils-env configFileName)"
30+
31+
while [[ ! $currentDir = "$HOME" && -n $currentDir && ! -f $localConfigFile ]]; do
32+
currentDir="${currentDir%/*}"
33+
localConfigFile="$currentDir/$(bash-utils-env configFileName)"
34+
done
35+
36+
if [[ -f $localConfigFile ]]; then
37+
printf "%s" "$localConfigFile"
38+
else
39+
echo ''
40+
fi
41+
}
42+
43+
# DESCRIPTION: Sources the local config file if it exists
44+
# USAGE: readLocalConfig
45+
readLocalConfig() {
46+
local localConfigFile
47+
48+
localConfigFile="$(findLocalConfig)"
49+
50+
if [[ -n $localConfigFile ]]; then
51+
source "$localConfigFile"
52+
fi
53+
}
54+
55+
# DESCRIPTION: Sources the both config files if they exist
56+
# USAGE: readConfig
57+
readConfig() {
58+
readGlobalConfig
59+
readLocalConfig
60+
}
61+
62+
# DESCRIPTION: Edits a local config file
63+
# USAGE: editConfigFile
64+
editConfigFile() {
65+
local localConfigFile
66+
local currentEditor
67+
68+
localConfigFile="$(findLocalConfig)"
69+
currentEditor="$(bash-utils-utils getEditor)"
70+
71+
if [[ -z $localConfigFile ]]; then
72+
bash-utils-utils die "$(bash-utils-env generalErrorCode)" "A local config file doesn't exist"
73+
fi
74+
75+
"$currentEditor" "$localConfigFile"
76+
}
77+
78+
# DESCRIPTION: Creates a local config file
79+
# USAGE: createConfigFile [DIRECTORY]
80+
createConfigFile() {
81+
local directory
82+
local localConfigFile
83+
local currentEditor
84+
85+
directory="${1:-$PWD}"
86+
87+
if [[ ! -d $directory ]]; then
88+
bash-utils-utils die "$(bash-utils-env generalErrorCode)" "A directory $directory doesn't exist"
89+
fi
90+
91+
localConfigFile="$directory/$(bash-utils-env configFileName)"
92+
93+
if [[ -f $localConfigFile ]]; then
94+
bash-utils-utils die "$(bash-utils-env generalErrorCode)" "A file $localConfigFile already exists"
95+
fi
96+
97+
currentEditor="$(bash-utils-utils getEditor)"
98+
99+
printf "bashUtilsConfig=example" > "$localConfigFile"
100+
101+
"$currentEditor" "$localConfigFile"
102+
}
103+
104+
# DESCRIPTION: Deletes a local config file
105+
# USAGE: deleteConfigFile
106+
deleteConfigFile() {
107+
local localConfigFile
108+
109+
localConfigFile="$(findLocalConfig)"
110+
111+
if [[ -f $localConfigFile ]]; then
112+
rm "$localConfigFile"
113+
fi
114+
}
115+
116+
# DESCRIPTION: The entry point for the bash-utils-config
117+
# USAGE: bash-utils-config COMMAND
118+
bash-utils-config() {
119+
local commandToExec
120+
commandToExec="$1"
121+
shift
122+
123+
case $commandToExec in
124+
create)
125+
createConfigFile "$@"
126+
;;
127+
read)
128+
readConfig "$@"
129+
;;
130+
edit)
131+
editConfigFile "$@"
132+
;;
133+
delete)
134+
deleteConfigFile "$@"
135+
;;
136+
*)
137+
bash-utils-utils die "$(bash-utils-env misuseErrorCode)" "An unknown command bash-utils-config ""$commandToExec"
138+
;;
139+
esac
140+
}
141+
142+
bashUtilsConfigLoaded=1

bin/bash-utils-config.bats

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
#!/usr/bin/env bash
2+
source ./bash-utils-config
3+
4+
setup() {
5+
configFileName='.bash-utils-rc'
6+
globalConfigFile="$HOME/$configFileName"
7+
printf "createFileComponent=0\n someVar=test" > "$globalConfigFile"
8+
printf "createFileComponent=1\n" > ./"$configFileName"
9+
}
10+
11+
teardown() {
12+
if [ -f "$globalConfigFile" ]; then
13+
rm "$globalConfigFile"
14+
fi
15+
16+
if [ -f "./$configFileName" ]; then
17+
rm ./"$configFileName"
18+
fi
19+
}
20+
21+
# readGlobalConfig test cases
22+
@test "[bash-utils-config] readGlobalConfig: should read the variables from the global config file" {
23+
readGlobalConfig
24+
25+
[ "$createFileComponent" -eq 0 ]
26+
}
27+
28+
# readLocalConfig test cases
29+
@test "[bash-utils-config] readLocalConfig: should read the variables from the local config file" {
30+
readLocalConfig
31+
32+
[ "$createFileComponent" -eq 1 ]
33+
}
34+
35+
# findLocalConfig test cases
36+
@test "[bash-utils-config] findLocalConfig: should find the local config file" {
37+
run findLocalConfig
38+
configFile="$PWD"/.bash-utils-rc
39+
40+
[ "$output" = "$configFile" ]
41+
}
42+
43+
# editConfigFile
44+
@test "[bash-utils-config] editConfigFile: should open a config file in a default editor" {
45+
EDITOR=echo
46+
run editConfigFile
47+
expectedConfigFile="$PWD"/.bash-utils-rc
48+
49+
[ "$output" = "$expectedConfigFile" ]
50+
}
51+
52+
@test "[bash-utils-config] editConfigFile: should throw an error if a local config file doesn't exist" {
53+
configFileName=abracadabre
54+
run editConfigFile
55+
[ "$status" -eq 1 ]
56+
57+
value="${csi}31mA local config file doesn't exist"$'\n'"${csi}m"
58+
[ "$output" = "$value" ]
59+
}
60+
61+
# editConfigFile
62+
@test "[bash-utils-config] createConfigFile: should create a config file and open it in a default editor" {
63+
EDITOR=cat
64+
expectedConfigFile="$PWD"/.bash-utils-rc
65+
66+
rm "$expectedConfigFile"
67+
68+
[ ! -f "$expectedConfigFile" ]
69+
70+
run createConfigFile
71+
72+
[ -f "$expectedConfigFile" ]
73+
[ "${lines[0]}" = 'bashUtilsConfig=example' ]
74+
}
75+
76+
@test "[bash-utils-config] createConfigFile: should throw an error if a local config file already exists" {
77+
expectedOutput="${csi}31mA file ${PWD}/.bash-utils-rc already exists"$'\n'"${csi}m"
78+
79+
run createConfigFile
80+
81+
[ "$status" -eq 1 ]
82+
[ "$output" = "$expectedOutput" ]
83+
}
84+
85+
@test "[bash-utils-config] createConfigFile: should create a config file in a given directory and open it in a default editor" {
86+
EDITOR=cat
87+
dir="$HOME"/"$RANDOM"
88+
89+
mkdir "$dir"
90+
91+
expectedConfigFile="$dir"/.bash-utils-rc
92+
93+
[ ! -f "$expectedConfigFile" ]
94+
95+
run createConfigFile "$dir"
96+
97+
[ -f "$expectedConfigFile" ]
98+
[ "${lines[0]}" = 'bashUtilsConfig=example' ]
99+
100+
rm -rf "$dir"
101+
}
102+
103+
@test "[bash-utils-config] createConfigFile: should throw an error if a given directory doesn't exist" {
104+
expectedOutput="${csi}31mA directory /abracadabra doesn't exist"$'\n'"${csi}m"
105+
106+
run createConfigFile /abracadabra
107+
108+
[ "$status" -eq 1 ]
109+
[ "$output" = "$expectedOutput" ]
110+
}
111+
112+
@test "[bash-utils-config] deleteConfigFile: should delete a config file if it exists" {
113+
expectedConfigFile="$PWD"/.bash-utils-rc
114+
115+
[ -f "$expectedConfigFile" ]
116+
117+
run deleteConfigFile
118+
119+
[ ! -f "$expectedConfigFile" ]
120+
}
121+
122+
# readConfig test cases
123+
@test "[bash-utils-config] readConfig: should read the variables from the global and the local config files" {
124+
readConfig
125+
126+
[ "$createFileComponent" -eq 1 ]
127+
[ "$someVar" = test ]
128+
}
129+
130+
# bash-utils-config test cases
131+
@test "[bash-utils-config] create: should proxy its arguments to the createConfigFile function" {
132+
EDITOR=cat
133+
expectedConfigFile="$PWD"/.bash-utils-rc
134+
135+
rm "$expectedConfigFile"
136+
137+
[ ! -f "$expectedConfigFile" ]
138+
139+
run bash-utils-config create
140+
141+
[ -f "$expectedConfigFile" ]
142+
[ "${lines[0]}" = 'bashUtilsConfig=example' ]
143+
}
144+
145+
@test "[bash-utils-config] bash-utils-config edit: should proxy its arguments to the editConfigFile function" {
146+
EDITOR=echo
147+
run bash-utils-config edit
148+
expectedConfigFile="$PWD"/.bash-utils-rc
149+
150+
[ "$output" = "$expectedConfigFile" ]
151+
}
152+
153+
@test "[bash-utils-config] bash-utils-config read: should proxy its arguments to the readConfigFile function" {
154+
bash-utils-config read
155+
156+
[ "$createFileComponent" -eq 1 ]
157+
}
158+
159+
@test "[bash-utils-config] bash-utils-config delete: should proxy its arguments to the deleteConfigFile function" {
160+
expectedConfigFile="$PWD"/.bash-utils-rc
161+
162+
[ -f "$expectedConfigFile" ]
163+
164+
run bash-utils-config delete
165+
166+
[ ! -f "$expectedConfigFile" ]
167+
}
168+
169+
@test "[bash-utils-config] bash-utils-config *: should exit with an error message if an unknow command was given" {
170+
run bash-utils-config something
171+
172+
[ "$status" -eq 2 ]
173+
value="${csi}31mAn unknown command bash-utils-config something"$'\n'"${csi}m"
174+
[ "$output" = "$value" ]
175+
}

0 commit comments

Comments
 (0)