In a package I'm writing, I have a config module that looks like this:
use v6.d;
use JSON::Fast;
use PSBot::Tools;
sub EXPORT(--> Hash) {
my Str $path = do if %*ENV<TESTING> {
$*REPO.Str.IO.child('META6.json').e
?? $*REPO.Str.IO.child('t/config.json').Str # For when testing using zef
!! $*REPO.Str.IO.parent.child('t/config.json').Str; # For when testing using prove
} elsif $*DISTRO.is-win {
"%*ENV<LOCALAPPDATA>\\PSBot\\config.json"
} else {
"$*HOME/.config/PSBot/config.json"
};
unless $path.IO.e {
note "PSBot config at $path does not exist!";
note "Copy psbot.json.example there and read the README for instructions on how to set up the config file.";
exit 1;
}
with from-json slurp $path -> %config {
%(
USERNAME => %config<username>,
PASSWORD => %config<password>,
AVATAR => %config<avatar>,
HOST => %config<host>,
PORT => %config<port>,
SERVERID => %config<serverid>,
COMMAND => %config<command>,
ROOMS => set(%config<rooms>.map: &to-roomid),
ADMINS => set(%config<admins>.map: &to-id),
MAX_RECONNECT_ATTEMPTS => %config<max_reconnect_attempts>,
GIT => %config<git>,
DICTIONARY_API_ID => %config<dictionary_api_id>,
DICTIONARY_API_KEY => %config<dictionary_api_key>,
YOUTUBE_API_KEY => %config<youtube_api_key>,
TRANSLATE_API_KEY => %config<translate_api_key>
)
}
}
Every time I make changes to the config file, I have to delete the precomp files for the changes to appear. Is there a way I can write this so the exports aren't defined at compile time so users don't have to do this?
The most common and standardized formats are YAML, JSON, TOML and INI. A good configuration file should meet at least these 3 criteria: Easy to read and edit: It should be text-based and structured in such a way that is easy to understand. Even non-developers should be able to read.
Simple configuration languages, such as JSON, work for many applications, but when you need proper validation, schema, and namespace support, XML is often best.
Python can have config files with all settings needed by the application dynamically or periodically. Python config files have the extension as . ini. We'll use VS Code (Visual Studio Code) to create a main method that uses config file to read the configurations and then print on the console.
These config files are typically placed under separate root directory than the rest of application code. For example, in case of Java they are typically under src/main/resources .
Assuming I understand your intentions correctly, one way to do it would be this:
EXPORT
sub$path
and %config
into the module's mainlinedeclare your 'constants' as terms such as
sub term:<USERNAME> is export { %config<username> }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With