import ballerina/config;
import ballerina/io;function main(string[] args) {
    string[] users;
    var usersConfig  = config:getAsString("username.instances");
    match usersConfig {
        string usersString => {
            users = usersString.split(",");
        }
        int | () => { return; }
    }    string user1Rights;
    var user1RightsConfig = config:getAsString(users[0] + ".access.rights");
    match user1RightsConfig {
        string user1RightsString => {
            user1Rights = user1RightsString;
        }
        int | () => { return; }
    }    string user2Rights;
    var user2RightsConfig = config:getAsString(users[1] + ".access.rights");
    match user2RightsConfig {
        string user2RightsString => {
            user2Rights = user2RightsString;
        }
        int | () => { return; }
    }    io:println(users[0] + " has " + user1Rights + " access");
    io:println(users[1] + " has " + user2Rights + " access");    io:println("Before changing sum.limit in code: " + getLimit());
    config:setConfig("sum.limit", "10");    io:println("After changing sum.limit: " + getLimit());
}function getLimit() returns (string) {
    var limitConfig = config:getAsString("sum.limit");
    match limitConfig {
        string limit => {
            return limit;
        }
        float | () => {
            io:println("Returning default limit: 1000");
            return "1000";
        }
    }
}

Config API

The 'ballerina.config' package provides an API for reading configurations from various sources such as files, environment variables, and CLI parameters.

import ballerina/config;
import ballerina/io;
function main(string[] args) {
    string[] users;
    var usersConfig  = config:getAsString("username.instances");
    match usersConfig {
        string usersString => {
            users = usersString.split(",");
        }
        int | () => { return; }
    }

Using the Ballerina config API, you can look up values from config files, CLI parameters, environment variables, etc. The precedence order for config lookup is as follows:
* CLI parameters
* Environment variables
* Config files

If a particular config defined in the file is also defined as an environment variable, the environment variable takes precedence. Similarly, if the same is set as a CLI parameter, it replaces the environment variable value.
The configs are simply arbitrary key/value pairs with slight structure to it.

    string user1Rights;
    var user1RightsConfig = config:getAsString(users[0] + ".access.rights");
    match user1RightsConfig {
        string user1RightsString => {
            user1Rights = user1RightsString;
        }
        int | () => { return; }
    }
    string user2Rights;
    var user2RightsConfig = config:getAsString(users[1] + ".access.rights");
    match user2RightsConfig {
        string user2RightsString => {
            user2Rights = user2RightsString;
        }
        int | () => { return; }
    }
    io:println(users[0] + " has " + user1Rights + " access");
    io:println(users[1] + " has " + user2Rights + " access");

A sample config file looks as follows:
sum.limit=5
username.instances=“john,peter”
[john]
access.rights=“RW”
[peter]
access.rights=“R”

The same configs can be set using CLI parameters as follows:
-e sum.limit=5 -e username.instances=john,peter -e john.access.rights=RW -e peter.access.rights=R

    io:println("Before changing sum.limit in code: " + getLimit());
    config:setConfig("sum.limit", "10");

You can set configs using the code as well.

    io:println("After changing sum.limit: " + getLimit());
}
function getLimit() returns (string) {
    var limitConfig = config:getAsString("sum.limit");
    match limitConfig {
        string limit => {
            return limit;
        }
        float | () => {
            io:println("Returning default limit: 1000");
            return "1000";
        }
    }
}
$ ballerina run config-api.bal --config path/to/conf/file/custom-config-file-name.conf
john has RW access
peter has R access
Before changing sum.limit in code: 5
After changing sum.limit: 10

To explicitly specify a config file, use the –config or the -c flag. If this flag is not set, Ballerina looks for a ballerina.conf file in the directory from which the user executes the program. The path to the config file can either be an absolute or a relative path.

$ ballerina run config-api.bal -e username.instances=john,peter -e john.access.rights=RW -e peter.access.rights=R -e sum.limit=5
john has RW access
peter has R access
Before changing sum.limit in code: 5
After changing sum.limit: 10

The same configs given through a config file can also be given through CLI parameters.
E.g., john.access.rights