Skip to content

Commit da97b04

Browse files
authored
Merge pull request #406 from DannyBen/refactor/config-lib
Breaking: Rewrite config (INI) library to provide an associative array
2 parents f0a1c9a + 2e69b1f commit da97b04

File tree

21 files changed

+350
-327
lines changed

21 files changed

+350
-327
lines changed

examples/command-paths/README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,24 +137,37 @@ Options:
137137
138138
```
139139

140-
### `$ ./docker download something`
140+
### `$ ./docker container run alpine`
141141

142142
```shell
143-
invalid command: download
143+
# this file is located in 'src/commands/container/run.sh'
144+
# code for 'docker container run' goes here
145+
# you can edit it freely and regenerate (it will not be overwritten)
146+
args:
147+
- ${args[image]} = alpine
144148
145149
146150
```
147151

148-
### `$ ls -1 src/*`
152+
### `$ ls -R src`
149153

150154
```shell
151-
src/bashly.yml
155+
src:
156+
bashly.yml
157+
commands
152158
153159
src/commands:
154160
container
155161
image
156162
ps.sh
157163
164+
src/commands/container:
165+
run.sh
166+
stop.sh
167+
168+
src/commands/image:
169+
ls.sh
170+
158171
159172
```
160173

examples/config-ini/README.md

Lines changed: 107 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ $ bashly generate
1313
$ bashly generate
1414
```
1515

16-
Running the `bashly add config` command simply added the [src/lib/config.sh](src/lib/config.sh) file, which includes functions for reading and writing values from an INI file.
16+
Running the `bashly add config` command simply added the
17+
[src/lib/config.sh](src/lib/config.sh) file, which includes functions for
18+
reading and writing values from an INI file.
1719

1820
See the files in the [src](src) folder for usage examples.
1921

@@ -29,6 +31,23 @@ help: Sample application that uses the config functions
2931
version: 0.1.0
3032

3133
commands:
34+
- name: list
35+
alias: l
36+
help: Show the entire config file
37+
38+
- name: get
39+
alias: g
40+
help: Read a value from the config file
41+
42+
args:
43+
- name: key
44+
required: true
45+
help: Config key
46+
47+
examples:
48+
- configly get hello
49+
- configly get user.name
50+
3251
- name: set
3352
alias: s
3453
help: Save a value in the config file
@@ -43,72 +62,81 @@ commands:
4362

4463
examples:
4564
- configly set hello world
65+
- configly set user.email [email protected]
4666

47-
- name: get
48-
alias: g
49-
help: Read a value from the config file
67+
- name: del
68+
alias: d
69+
help: Remove a value from the config file
5070

5171
args:
5272
- name: key
5373
required: true
5474
help: Config key
5575

5676
examples:
57-
- configly set hello
58-
59-
- name: list
60-
alias: l
61-
help: Show the entire config file
77+
- configly del hello
78+
- configly del user.name
6279
```
6380
6481
## `config.ini`
6582

6683
```ini
67-
; comments are allowed
84+
; comments are allowed, sections are optional
6885
hello = world
6986
bashly = works
7087
88+
[options]
89+
name = value for options.name
90+
path = value for options.path
91+
92+
[user]
93+
name = value for user.name
94+
email = value for user.email
7195
7296
```
7397

7498
## `src/get_command.sh`
7599

76100
```bash
77101
# Using the standard library (lib/config.sh) to show a value from the config
102+
config_load config.ini
78103
79104
key="${args[key]}"
80-
if config_has_key "$key"; then
81-
config_get "$key"
105+
value=${config[$key]}
106+
107+
if [[ "$value" ]]; then
108+
echo "$key = $value"
82109
else
83110
echo "No such key: $key"
84111
fi
85112
86-
# Example of how to assign the config value to a variable:
87-
# result=$(config_get "${args[key]}")
88-
# echo $result
89-
90-
91113
```
92114

93115
## `src/list_command.sh`
94116

95117
```bash
96118
# Using the standard library (lib/config.sh) to show the entire config file
119+
config_load config.ini
97120
config_show
98121
99-
# Or to iterate through keys
100-
for key in $(config_keys); do
101-
echo "$key === $(config_get "$key")"
102-
done
103-
122+
## Or to iterate through keys manually
123+
# for key in $(config_keys); do
124+
# echo "$key = ${config[$key]}"
125+
# done
104126
```
105127

106128
## `src/set_command.sh`
107129

108130
```bash
109131
# Using the standard library (lib/config.sh) to store a value to the config
110-
config_set "${args[key]}" "${args[value]}"
111-
echo "saved: ${args[key]} = ${args[value]}"
132+
config_load config.ini
133+
134+
key="${args[key]}"
135+
value="${args[value]}"
136+
137+
config["$key"]="$value"
138+
config_save saved.ini
139+
cat saved.ini
112140
113141
```
114142

@@ -126,9 +154,10 @@ Usage:
126154
configly --version | -v
127155
128156
Commands:
129-
set Save a value in the config file
130-
get Read a value from the config file
131157
list Show the entire config file
158+
get Read a value from the config file
159+
set Save a value in the config file
160+
del Remove a value from the config file
132161
133162
Options:
134163
--help, -h
@@ -141,26 +170,52 @@ Options:
141170
142171
```
143172

144-
### `$ ./configly set hello world`
173+
### `$ ./configly set hello WORLD`
145174

146175
```shell
147-
saved: hello = world
176+
bashly = works
177+
hello = WORLD
178+
179+
[options]
180+
name = value for options.name
181+
path = value for options.path
182+
183+
[user]
184+
email = value for user.email
185+
name = value for user.name
148186
149187
150188
```
151189

152-
### `$ ./configly set bashly works`
190+
### `$ ./configly set user.name Megatron`
153191

154192
```shell
155-
saved: bashly = works
193+
bashly = works
194+
hello = world
195+
196+
[options]
197+
name = value for options.name
198+
path = value for options.path
199+
200+
[user]
201+
email = value for user.email
202+
name = Megatron
156203
157204
158205
```
159206

160207
### `$ ./configly get hello`
161208

162209
```shell
163-
world
210+
hello = world
211+
212+
213+
```
214+
215+
### `$ ./configly get user.name`
216+
217+
```shell
218+
user.name = value for user.name
164219
165220
166221
```
@@ -173,15 +228,31 @@ No such key: invalid_key
173228
174229
```
175230

176-
### `$ ./configly list`
231+
### `$ ./configly del user.email`
177232

178233
```shell
179-
; comments are allowed
180-
hello = world
181234
bashly = works
235+
hello = world
236+
237+
[options]
238+
name = value for options.name
239+
path = value for options.path
240+
241+
[user]
242+
name = value for user.name
243+
182244
183-
hello === world
184-
bashly === works
245+
```
246+
247+
### `$ ./configly list`
248+
249+
```shell
250+
bashly = works
251+
hello = world
252+
options.name = value for options.name
253+
options.path = value for options.path
254+
user.email = value for user.email
255+
user.name = value for user.name
185256
186257
187258
```

examples/config-ini/config.ini

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
; comments are allowed
1+
; comments are allowed, sections are optional
22
hello = world
33
bashly = works
44

5+
[options]
6+
name = value for options.name
7+
path = value for options.path
8+
9+
[user]
10+
name = value for user.name
11+
email = value for user.email

examples/config-ini/saved.ini

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
bashly = works
2+
hello = world
3+
4+
[options]
5+
name = value for options.name
6+
path = value for options.path
7+
8+
[user]
9+
name = value for user.name

examples/config-ini/src/bashly.yml

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@ help: Sample application that uses the config functions
33
version: 0.1.0
44

55
commands:
6+
- name: list
7+
alias: l
8+
help: Show the entire config file
9+
10+
- name: get
11+
alias: g
12+
help: Read a value from the config file
13+
14+
args:
15+
- name: key
16+
required: true
17+
help: Config key
18+
19+
examples:
20+
- configly get hello
21+
- configly get user.name
22+
623
- name: set
724
alias: s
825
help: Save a value in the config file
@@ -17,19 +34,17 @@ commands:
1734

1835
examples:
1936
- configly set hello world
37+
- configly set user.email [email protected]
2038

21-
- name: get
22-
alias: g
23-
help: Read a value from the config file
39+
- name: del
40+
alias: d
41+
help: Remove a value from the config file
2442

2543
args:
2644
- name: key
2745
required: true
2846
help: Config key
2947

3048
examples:
31-
- configly set hello
32-
33-
- name: list
34-
alias: l
35-
help: Show the entire config file
49+
- configly del hello
50+
- configly del user.name
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Using the standard library (lib/config.sh) to delete a value from the config
2+
config_load config.ini
3+
4+
key="${args[key]}"
5+
unset "config[$key]"
6+
7+
config_save saved.ini
8+
cat saved.ini
9+
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
# Using the standard library (lib/config.sh) to show a value from the config
2+
config_load config.ini
23

34
key="${args[key]}"
4-
if config_has_key "$key"; then
5-
config_get "$key"
5+
value=${config[$key]}
6+
7+
if [[ "$value" ]]; then
8+
echo "$key = $value"
69
else
710
echo "No such key: $key"
811
fi
9-
10-
# Example of how to assign the config value to a variable:
11-
# result=$(config_get "${args[key]}")
12-
# echo $result
13-

0 commit comments

Comments
 (0)