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 APIThe '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: |
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: |
|
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 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. |