Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I better handle config files?

Tags:

config

raku

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?

like image 947
Kaiepi Avatar asked May 26 '19 21:05

Kaiepi


People also ask

How do you make a good config file?

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.

What is the best configuration language?

Simple configuration languages, such as JSON, work for many applications, but when you need proper validation, schema, and namespace support, XML is often best.

How do I manage config files in Python?

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.

Where should config files be stored?

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 .


1 Answers

Assuming I understand your intentions correctly, one way to do it would be this:

  1. get rid of the EXPORT sub
  2. place the computation of $path and %config into the module's mainline
  3. declare your 'constants' as terms such as

    sub term:<USERNAME> is export { %config<username> }
    
like image 74
Christoph Avatar answered Oct 11 '22 12:10

Christoph