Skip to content

Commit 08faa94

Browse files
authored
Merge pull request #408 from DannyBen/add/ini-library
Add `ini` library for handling INI files with `[sections]`
2 parents 48d0bf1 + 44f0066 commit 08faa94

File tree

17 files changed

+698
-5
lines changed

17 files changed

+698
-5
lines changed

examples/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ Each of these examples demonstrates one aspect or feature of bashly.
5858

5959
## Bashly library features
6060

61-
- [config-ini](config-ini#readme) - using the config (INI) functions
62-
- [colors](colors#readme) - using the color print feature
61+
- [config](config#readme) - using the config functions
62+
- [ini](ini#readme) - using the INI handling functions
6363
- [yaml](yaml#readme) - using the YAML reading functions
64+
- [colors](colors#readme) - using the color print feature
6465
- [completions](completions#readme) - adding bash completion functionality
6566
- [validations](validations#readme) - adding argument validation functions
6667
- [hooks](hooks#readme) - adding before/after hooks

examples/ini/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
configly

examples/ini/README.md

Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
# Config Example
2+
3+
Demonstrates how to add functions for reading and writing INI files.
4+
5+
This example was generated with:
6+
7+
```bash
8+
$ bashly init
9+
# ... now edit src/bashly.yml to match the example ...
10+
$ bashly add config
11+
$ bashly generate
12+
# ... now edit all files in the src folder ...
13+
$ bashly generate
14+
```
15+
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.
19+
20+
See the files in the [src](src) folder for usage examples.
21+
22+
<!-- include: config.ini src/get_command.sh src/list_command.sh src/set_command.sh -->
23+
24+
-----
25+
26+
## `bashly.yml`
27+
28+
```yaml
29+
name: configly
30+
help: Sample application that uses the config functions
31+
version: 0.1.0
32+
33+
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+
51+
- name: set
52+
alias: s
53+
help: Save a value in the config file
54+
55+
args:
56+
- name: key
57+
required: true
58+
help: Config key
59+
- name: value
60+
required: true
61+
help: Config value
62+
63+
examples:
64+
- configly set hello world
65+
- configly set user.email me@example.com
66+
67+
- name: del
68+
alias: d
69+
help: Remove a value from the config file
70+
71+
args:
72+
- name: key
73+
required: true
74+
help: Config key
75+
76+
examples:
77+
- configly del hello
78+
- configly del user.name
79+
```
80+
81+
## `config.ini`
82+
83+
```ini
84+
; comments are allowed, sections are optional
85+
hello = world
86+
bashly = works
87+
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
95+
96+
```
97+
98+
## `src/get_command.sh`
99+
100+
```bash
101+
# Using the standard library (lib/config.sh) to show a value from the config
102+
config_load config.ini
103+
104+
key="${args[key]}"
105+
value=${config[$key]}
106+
107+
if [[ "$value" ]]; then
108+
echo "$key = $value"
109+
else
110+
echo "No such key: $key"
111+
fi
112+
113+
```
114+
115+
## `src/list_command.sh`
116+
117+
```bash
118+
# Using the standard library (lib/config.sh) to show the entire config file
119+
config_load config.ini
120+
config_show
121+
122+
## Or to iterate through keys manually
123+
# for key in $(config_keys); do
124+
# echo "$key = ${config[$key]}"
125+
# done
126+
```
127+
128+
## `src/set_command.sh`
129+
130+
```bash
131+
# Using the standard library (lib/config.sh) to store a value to the config
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
140+
141+
```
142+
143+
144+
## Generated script output
145+
146+
### `$ ./configly -h`
147+
148+
```shell
149+
configly - Sample application that uses the config functions
150+
151+
Usage:
152+
configly COMMAND
153+
configly [COMMAND] --help | -h
154+
configly --version | -v
155+
156+
Commands:
157+
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
161+
162+
Options:
163+
--help, -h
164+
Show this help
165+
166+
--version, -v
167+
Show version number
168+
169+
170+
171+
```
172+
173+
### `$ ./configly set hello WORLD`
174+
175+
```shell
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
186+
187+
188+
```
189+
190+
### `$ ./configly set user.name Megatron`
191+
192+
```shell
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
203+
204+
205+
```
206+
207+
### `$ ./configly get hello`
208+
209+
```shell
210+
hello = world
211+
212+
213+
```
214+
215+
### `$ ./configly get user.name`
216+
217+
```shell
218+
user.name = value for user.name
219+
220+
221+
```
222+
223+
### `$ ./configly get invalid_key`
224+
225+
```shell
226+
No such key: invalid_key
227+
228+
229+
```
230+
231+
### `$ ./configly del user.email`
232+
233+
```shell
234+
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+
244+
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
256+
257+
258+
```
259+
260+
261+

examples/ini/config.ini

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
; comments are allowed, sections are optional
2+
hello = world
3+
bashly = works
4+
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/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/ini/src/bashly.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: configly
2+
help: Sample application that uses the config functions
3+
version: 0.1.0
4+
5+
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+
23+
- name: set
24+
alias: s
25+
help: Save a value in the config file
26+
27+
args:
28+
- name: key
29+
required: true
30+
help: Config key
31+
- name: value
32+
required: true
33+
help: Config value
34+
35+
examples:
36+
- configly set hello world
37+
- configly set user.email me@example.com
38+
39+
- name: del
40+
alias: d
41+
help: Remove a value from the config file
42+
43+
args:
44+
- name: key
45+
required: true
46+
help: Config key
47+
48+
examples:
49+
- configly del hello
50+
- configly del user.name

examples/ini/src/del_command.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Using the standard library (lib/ini.sh) to delete a value from the config
2+
ini_load config.ini
3+
4+
key="${args[key]}"
5+
unset "ini[$key]"
6+
7+
ini_save saved.ini
8+
cat saved.ini
9+

examples/ini/src/get_command.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Using the standard library (lib/ini.sh) to show a value from the config
2+
ini_load config.ini
3+
4+
key="${args[key]}"
5+
value=${ini[$key]}
6+
7+
if [[ "$value" ]]; then
8+
echo "$key = $value"
9+
else
10+
echo "No such key: $key"
11+
fi

0 commit comments

Comments
 (0)