Demonstrates how to add functions for reading and writing INI configuration files.
Note that this library uses the ini library for its low-level INI read/write functions.
This example was generated with:
$ 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 file, which includes functions for reading and writing values from an INI file.
See the files in the src folder for usage examples.
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
theme = dark
[user]
email = paul@section.one
name = Operations
# 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"
# 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
# Using the standard library (lib/config.sh) to store a value to the config
config_set "${args[key]}" "${args[value]}"
config_show
# Using the standard library (lib/config.sh) to delete a value from the config
key="${args[key]}"
config_del "$key"
config_show
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
theme = dark
user.email = paul@section.one
user.name = Operations
theme = dark
user.email = paul@section.one
user.name = Operations
theme = dark
user.email = paul@section.one
user.name = Operations
theme = dark
user.email = paul@section.one
user.name = Operations
user.password = s3cr3t
dark
dark
dark
Operations
Operations
Operations
No such key: invalid_key
the default value
theme = dark
user.email = paul@section.one
user.name = Operations
theme = dark
user.email = paul@section.one
user.name = Operations
theme === dark
user.email === paul@section.one
user.name === Operations