examples/config/README.md
# Config Example
Demonstrates how to add functions for reading and writing INI configuration
files.
Note that this library uses the [ini library](https://github.com/DannyBen/bashly/tree/master/examples/ini#readme)
for its low-level INI read/write functions.
This example was generated with:
```bash
$ bashly init
# ... now edit src/bashly.yml to match the example ...
$ bashly add config
$ bashly generate
# ... now edit all files in the src folder ...
$ bashly generate
```
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.
See the files in the [src](src) folder for usage examples.
<!-- include: config.ini src/get_command.sh src/list_command.sh src/set_command.sh src/del_command.sh -->
-----
## `bashly.yml`
````yaml
name: configly
help: Sample application that uses the config functions
version: 0.1.0
commands:
- name: set
alias: s
help: Save a value in the config file
args:
- name: key
required: true
help: Config key
- name: value
required: true
help: Config value
examples:
- configly set hello world
- configly set login.name Megatron
- name: get
alias: g
help: Read a value from the config file
args:
- name: key
required: true
help: Config key
examples:
- configly get hello
- configly get login.name
- name: del
alias: d
help: Delete a value from the config file
args:
- name: key
required: true
help: Config key
examples:
- configly del hello
- configly del login.name
- name: list
alias: l
help: Show all values
````
## `config.ini`
````ini
theme = dark
[user]
email = paul@section.one
name = Operations
````
## `src/get_command.sh`
````bash
# Using the standard library (lib/config.sh) to show a value from the config
key="${args[key]}"
if config_has_key "$key"; then
config_get "$key"
else
echo "No such key: $key"
fi
# Or, assign a default value if value not found
config_get "$key" "the default value"
# Or, assign the result to a variable
result=$(config_get "$key")
echo "$result"
````
## `src/list_command.sh`
````bash
# Using the standard library (lib/config.sh) to show the entire config file
config_show
# Or to iterate through keys
for key in $(config_keys); do
echo "$key === $(config_get "$key")"
done
````
## `src/set_command.sh`
````bash
# Using the standard library (lib/config.sh) to store a value to the config
config_set "${args[key]}" "${args[value]}"
config_show
````
## `src/del_command.sh`
````bash
# Using the standard library (lib/config.sh) to delete a value from the config
key="${args[key]}"
config_del "$key"
config_show
````
## Output
### `$ ./configly -h`
````shell
configly - Sample application that uses the config functions
Usage:
configly COMMAND
configly [COMMAND] --help | -h
configly --version | -v
Commands:
set Save a value in the config file
get Read a value from the config file
del Delete a value from the config file
list Show all values
Options:
--help, -h
Show this help
--version, -v
Show version number
````
### `$ ./configly set theme dark`
````shell
theme = dark
user.email = paul@section.one
user.name = Operations
````
### `$ ./configly set user.name Operations`
````shell
theme = dark
user.email = paul@section.one
user.name = Operations
````
### `$ ./configly set user.email paul@section.one`
````shell
theme = dark
user.email = paul@section.one
user.name = Operations
````
### `$ ./configly set user.password s3cr3t`
````shell
theme = dark
user.email = paul@section.one
user.name = Operations
user.password = s3cr3t
````
### `$ ./configly get theme`
````shell
dark
dark
dark
````
### `$ ./configly get user.name`
````shell
Operations
Operations
Operations
````
### `$ ./configly get invalid_key`
````shell
No such key: invalid_key
the default value
````
### `$ ./configly del user.password`
````shell
theme = dark
user.email = paul@section.one
user.name = Operations
````
### `$ ./configly list`
````shell
theme = dark
user.email = paul@section.one
user.name = Operations
theme === dark
user.email === paul@section.one
user.name === Operations
````