env2js
env2js is a tool that will transfer environment variable to a Javascript configuration file.
This is especially useful within virtual machines environments like Docker, where you want to modify the configuration based on the container environment variables.
How it works :
Given the config.js file :
const AppSettings = {
isServed: true,
API: {
apiRoot: "url/server/app",
},
}
and the environment variables :
AppSettings_API_apiRoot="custom/url/app"
After env2js usage, config.js equals :
const AppSettings = {
isServed: true,
API: {
apiRoot: "custom/url/app",
},
}
Start developing :
1) Install the dependencies :
go mod tidy
ℹ More informations about : go mod tidy
2) Define the three required environment variables :
SETTINGS_FOLDER_PATH : Folder that includes the configuration files
SETTINGS_FILE_PREFIX : File name without the extension, eg : "example.js"
SETTINGS_VARIABLE_NAME : Key name to read inside the file
export SETTINGS_FOLDER_PATH=/path/to/my/config
export SETTINGS_FILE_PREFIX=example
export SETTINGS_VARIABLE_NAME=AppSettings
3) Run the program :
go run .
ℹ More informations about : go run
- String :
AppSettings_myValue="MyValue"
- Int :
AppSettings_myInt=10
- Boolean :
AppSettings_myBool=true
- Array index :
AppSettings_MyArray_[0]="MyValue"
- Nested value :
AppSettings_MyObject_MyValue="MyValue"