Skip to content

Latest commit

 

History

History

config

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Config Example

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.


bashly.yml

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

theme = dark

[user]
email = paul@section.one
name = Operations

src/get_command.sh

# 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

# 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

# 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

# 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

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

theme = dark
user.email = paul@section.one
user.name = Operations

$ ./configly set user.name Operations

theme = dark
user.email = paul@section.one
user.name = Operations

$ ./configly set user.email paul@section.one

theme = dark
user.email = paul@section.one
user.name = Operations

$ ./configly set user.password s3cr3t

theme = dark
user.email = paul@section.one
user.name = Operations
user.password = s3cr3t

$ ./configly get theme

dark
dark
dark

$ ./configly get user.name

Operations
Operations
Operations

$ ./configly get invalid_key

No such key: invalid_key
the default value


$ ./configly del user.password

theme = dark
user.email = paul@section.one
user.name = Operations

$ ./configly list

theme = dark
user.email = paul@section.one
user.name = Operations
theme === dark
user.email === paul@section.one
user.name === Operations