A bash library providing TOML file parsing utilities for configuration management.
# Load the library using grab
if [ ! -f "$HOME/bin/grab" ]; then
mkdir -p $HOME/bin
export PATH=$PATH:$HOME/bin
curl -o $HOME/bin/grab -L https://github.com/shellib/grab/raw/master/grab.sh && chmod +x $HOME/bin/grab
fi
source $(grab github.com/shellib/toml as toml)toml::has_section(section, file)- Check if section existstoml::list_sections(file)- List all sections in file
toml::get_value(section, key, file)- Get value for key in sectiontoml::get_section(section, file)- Get all key-value pairs from section
Given a TOML file config.toml:
[default]
method = "fallback"
["github.com"]
method = "ssh"
username = "myuser"
["gitlab.example.com/org/repo"]
method = "https"# Check if section exists
if toml::has_section "default" "config.toml"; then
echo "Default section found"
fi
# Get a value
method=$(toml::get_value "default" "method" "config.toml")
echo "Method: $method" # Output: Method: fallback
# Get value from quoted section
username=$(toml::get_value '"github.com"' "username" "config.toml")
echo "Username: $username" # Output: Username: myuser
# List all sections
toml::list_sections "config.toml"
# Output:
# default
# "github.com"
# "gitlab.example.com/org/repo"
# Get all key-value pairs from a section
toml::get_section '"github.com"' "config.toml"
# Output:
# method|ssh
# username|myuserFor sections with special characters (like URLs), use quotes:
# Correct
toml::get_value '"github.com"' "method" "config.toml"
toml::get_value '"gitlab.example.com/org/repo"' "method" "config.toml"./library.sh --test- Supports both quoted and unquoted section names
- Handles trailing whitespace in TOML files
- Robust parsing using awk for cross-platform compatibility
- Comprehensive test suite included