Skip to content

Commit f8995c6

Browse files
authored
Merge pull request #409 from DannyBen/refactor/config
Config Library: Refactor to allow sections and use the low level INI library
2 parents 64e93fa + 2f9a27f commit f8995c6

File tree

18 files changed

+468
-267
lines changed

18 files changed

+468
-267
lines changed

examples/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ Each of these examples demonstrates one aspect or feature of bashly.
5858

5959
## Bashly library features
6060

61-
- [config](config#readme) - using the config functions
62-
- [ini](ini#readme) - using the INI handling functions
61+
- [config](config#readme) - using the config library for easy access to INI files
62+
- [ini](ini#readme) - using the ini library for direct, low level access to INI files
6363
- [yaml](yaml#readme) - using the YAML reading functions
6464
- [colors](colors#readme) - using the color print feature
6565
- [completions](completions#readme) - adding bash completion functionality

examples/config/README.md

+100-24
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# Config Example
22

3-
Demonstrates how to add functions for reading and writing INI-like files with
4-
`key = value` pairs.
3+
Demonstrates how to add functions for reading and writing INI configuration
4+
files.
5+
6+
Note that this library uses the [ini library](https://github.com/DannyBen/bashly/tree/master/examples/ini#readme)
7+
for its low-level INI read/write functions.
58

69
This example was generated with:
710

@@ -18,7 +21,7 @@ Running the `bashly add config` command simply added the [src/lib/config.sh](src
1821

1922
See the files in the [src](src) folder for usage examples.
2023

21-
<!-- include: config.ini src/get_command.sh src/list_command.sh src/set_command.sh -->
24+
<!-- include: config.ini src/get_command.sh src/list_command.sh src/set_command.sh src/del_command.sh -->
2225

2326
-----
2427

@@ -44,6 +47,7 @@ commands:
4447

4548
examples:
4649
- configly set hello world
50+
- configly set login.name Megatron
4751

4852
- name: get
4953
alias: g
@@ -55,20 +59,35 @@ commands:
5559
help: Config key
5660

5761
examples:
58-
- configly set hello
62+
- configly get hello
63+
- configly get login.name
64+
65+
- name: del
66+
alias: d
67+
help: Delete a value from the config file
68+
69+
args:
70+
- name: key
71+
required: true
72+
help: Config key
73+
74+
examples:
75+
- configly del hello
76+
- configly del login.name
5977

6078
- name: list
6179
alias: l
62-
help: Show the entire config file
80+
help: Show all values
6381
```
6482
6583
## `config.ini`
6684

6785
```ini
68-
; comments are allowed
69-
hello = world
70-
bashly = works
86+
theme = dark
7187
88+
[user]
89+
email = paul@section.one
90+
name = Operations
7291
7392
```
7493

@@ -112,7 +131,18 @@ done
112131
```bash
113132
# Using the standard library (lib/config.sh) to store a value to the config
114133
config_set "${args[key]}" "${args[value]}"
115-
echo "saved: ${args[key]} = ${args[value]}"
134+
config_show
135+
136+
```
137+
138+
## `src/del_command.sh`
139+
140+
```bash
141+
# Using the standard library (lib/config.sh) to delete a value from the config
142+
143+
key="${args[key]}"
144+
config_del "$key"
145+
config_show
116146
117147
```
118148

@@ -132,7 +162,8 @@ Usage:
132162
Commands:
133163
set Save a value in the config file
134164
get Read a value from the config file
135-
list Show the entire config file
165+
del Delete a value from the config file
166+
list Show all values
136167
137168
Options:
138169
--help, -h
@@ -145,28 +176,63 @@ Options:
145176
146177
```
147178

148-
### `$ ./configly set hello world`
179+
### `$ ./configly set theme dark`
149180

150181
```shell
151-
saved: hello = world
182+
theme = dark
183+
user.email = paul@section.one
184+
user.name = Operations
152185
153186
154187
```
155188

156-
### `$ ./configly set bashly works`
189+
### `$ ./configly set user.name Operations`
157190

158191
```shell
159-
saved: bashly = works
192+
theme = dark
193+
user.email = paul@section.one
194+
user.name = Operations
160195
161196
162197
```
163198

164-
### `$ ./configly get hello`
199+
### `$ ./configly set user.email paul@section.one`
165200

166201
```shell
167-
world
168-
world
169-
world
202+
theme = dark
203+
user.email = paul@section.one
204+
user.name = Operations
205+
206+
207+
```
208+
209+
### `$ ./configly set user.password s3cr3t`
210+
211+
```shell
212+
theme = dark
213+
user.email = paul@section.one
214+
user.name = Operations
215+
user.password = s3cr3t
216+
217+
218+
```
219+
220+
### `$ ./configly get theme`
221+
222+
```shell
223+
dark
224+
dark
225+
dark
226+
227+
228+
```
229+
230+
### `$ ./configly get user.name`
231+
232+
```shell
233+
Operations
234+
Operations
235+
Operations
170236
171237
172238
```
@@ -181,15 +247,25 @@ the default value
181247
182248
```
183249

184-
### `$ ./configly list`
250+
### `$ ./configly del user.password`
185251

186252
```shell
187-
; comments are allowed
188-
hello = world
189-
bashly = works
253+
theme = dark
254+
user.email = paul@section.one
255+
user.name = Operations
256+
190257
191-
hello === world
192-
bashly === works
258+
```
259+
260+
### `$ ./configly list`
261+
262+
```shell
263+
theme = dark
264+
user.email = paul@section.one
265+
user.name = Operations
266+
theme === dark
267+
user.email === paul@section.one
268+
user.name === Operations
193269
194270
195271
```

examples/config/config.ini

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
; comments are allowed
2-
hello = world
3-
bashly = works
1+
theme = dark
42

3+
[user]
4+
email = paul@section.one
5+
name = Operations

examples/config/src/bashly.yml

+17-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ commands:
1717

1818
examples:
1919
- configly set hello world
20+
- configly set login.name Megatron
2021

2122
- name: get
2223
alias: g
@@ -28,8 +29,22 @@ commands:
2829
help: Config key
2930

3031
examples:
31-
- configly set hello
32+
- configly get hello
33+
- configly get login.name
34+
35+
- name: del
36+
alias: d
37+
help: Delete a value from the config file
38+
39+
args:
40+
- name: key
41+
required: true
42+
help: Config key
43+
44+
examples:
45+
- configly del hello
46+
- configly del login.name
3247

3348
- name: list
3449
alias: l
35-
help: Show the entire config file
50+
help: Show all values

examples/config/src/del_command.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Using the standard library (lib/config.sh) to delete a value from the config
2+
3+
key="${args[key]}"
4+
config_del "$key"
5+
config_show

0 commit comments

Comments
 (0)