d5 / tengo

A fast script language for Go
https://tengolang.com
MIT License
3.55k stars 303 forks source link

Support for centralized configuration and shared variables in separate Tengo scripts #428

Open pokeyaro opened 1 year ago

pokeyaro commented 1 year ago

Description: I'm currently using Tengo scripts for configuration management in my application. I have a central configuration script (base.tengo) that defines global settings like whether certain features (e.g., MySQL, Redis) should be enabled or not. I then have separate environment-specific scripts (dev.tengo and prod.tengo) that use these global settings to set up connection parameters for different environments.

Here's the structure:

config/base.tengo (Defines global settings) config/dev.tengo (Specific settings for the development environment) config/prod.tengo (Specific settings for the production environment) main.go (Runs the configuration updater) However, I'm facing an issue where I want to control these global settings in the base.tengo script and share them with the environment-specific scripts (dev.tengo, prod.tengo) without having to modify main.go. In other words, I want to achieve centralized configuration management and share these settings among the different Tengo scripts.

Currently, I'm facing difficulties in importing the global settings from base.tengo into dev.tengo and prod.tengo. The import paths seem to be causing issues, and I'm looking for a way to share these settings across different scripts seamlessly.

MyCode:

config/base.tengo

export func() {
    return {
        mysqlEnabled: true
    }
}()

config/dev.tengo

ctl := import("./config/base")

mysql := func() {
    if ctl.mysqlEnabled {
        return {
            host: "dev.mysql.com",
            port: 3306,
            username: "dev_user",
            password: "dev_pass",
            dbname: "dev_db"
        }
    }
    return {}
}()

Is there a recommended approach or any updates planned that would enable centralized configuration management and sharing of variables among separate Tengo scripts? This would greatly improve the maintainability and flexibility of the configuration setup.

Thank you for your time and consideration!

geseq commented 1 year ago

Can you explain what difficulties you're actually facing?