Skip to content
/ envig Public

A lightweight and flexible environment variable manager for @vlang, inspired by dotenv and dotenv-expand. πŸ› οΈ

License

Notifications You must be signed in to change notification settings

siguici/envig

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

20 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

🌟 Envig – Unified Configuration and Environment Manager for V

Envig License CI

Envig is a lightweight and flexible configuration and environment manager for V.
It unifies configuration loading from TOML files and environment variables, and supports dynamic expansion like dotenv-expand.


πŸš€ Features

βœ… Load configuration from TOML files dynamically
βœ… Support for environment variables via .env files
βœ… Dynamic variable expansion (${VAR_NAME} style)
βœ… Load from directories, single files, or raw text
βœ… Fallback and default values for missing keys
βœ… Type-safe retrieval with automatic conversion
βœ… Lightweight and fast, designed specifically for the V language


πŸ“¦ Installation

Install it via VPM:

v install siguici.envig

Or add it to your dependencies:

import siguici.envig

βš™οΈ Usage

πŸ”Ή Loading Configuration

Use default options

import siguici.envig

mut config := envig.ConfigManager.load()
println(config.value('database.host'))

Specify the config file and/or directory

import siguici.envig

mut config := envig.ConfigManager.load(
  file: 'config.toml'
  dir: 'config'
)
println(config.value('database.host'))

From a Single File

import siguici.envig

mut config := envig.Config.new('config.toml')
println(config.value('database.host'))

From a Directory

mut config := envig.Config.new('config')
println(config.value('app.debug'))

From Raw TOML Text

toml_text := """
[database]
host = "localhost"
port = 5432
"""
mut config := envig.Config.new(toml_text)
println(config.value('database.port')) // 5432

πŸ”Ή Handling Environment Variables

Loading .env Files

import siguici.envig

mut env := envig.Env.load('.env')
println(env.get('APP_ENV'))

Expanding Variables Dynamically

# .env
APP_ENV=production
DB_URL="postgres://user:password@localhost:5432/${APP_ENV}"
mut env := envig.Env.load('.env').expand()
println(env.get('DB_URL')) // "postgres://user:password@localhost:5432/production"

πŸ”Ή Unified Interface: The Envig Manager

Envig provides a powerful unified interface combining environment and configuration:

mut e := envig.envig()

port := e.env_or_default('PORT', '8080')
db_url := e.get('database.url')
debug := e.get_as[bool]('app.debug')

println('Server running on port $port')
println('Database URL: $db_url')
println('Debug mode: $debug')

Or specify custom options:

mut e := envig.envig(envig.EnvigOptions{
    dir: 'settings',
    file: 'app.toml',
    env: 'production',
})

πŸ’‘ Examples of Unified Access

Purpose Example
Get environment variable val := e.env('PORT')
Get environment with default val := e.env_or_default('PORT', '3000')
Get config value (toml.Any) cfg := e.config('database.url')
Config with default fallback cfg := e.config_or_default('db', 'localhost')
Expand variables in a string expanded := e.expand('${DATABASE_URL}')
Unified get (config + expansion) val := e.get('database.url')
Unified get with default fallback val := e.get_or_default('service.url', 'https://default')
Get as a specific type debug := e.get_as[bool]('app.debug')

πŸ“š API Reference

Config & ConfigManager (Configuration Handling)

Method Description
Config.new(raw string) Creates a config from raw text or TOML file (panics on error).
Config.new_opt(raw string) !Config Optional version returning error instead of panic.
ConfigManager.new(opts ConfigOptions) Creates a config manager with options (dir, file).
ConfigManager.load(opts ConfigOptions) Loads configuration from file, directory or raw text.
ConfigManager.load_dir(path string) Loads all TOML files in a directory.
ConfigManager.load_file(path string) Loads a single TOML file.
ConfigManager.load_text(text string) Loads config from raw TOML text.
ConfigManager.value(path string) Retrieves a value by key (supports nested keys like config.key).
ConfigManager.value_or_default(key string, default toml.Any) Retrieves a value or returns a default.

Env & Dotenv (Environment Variable Handling)

Method Description
Env.new(options EnvOptions) Creates an environment with a map of variables.
Env.set(key, value) Sets an environment variable.
Env.get(key) Retrieves a variable (returns empty if missing).
Env.get_or_default(key, default) Retrieves a variable or returns default.
Env.apply() Applies all variables to the OS environment.
Dotenv.load(options DotenvOptions) Loads a .env file (appends .APP_ENV if defined).
Dotenv.load_multiple(files []string) Loads multiple .env files.
Dotenv.expand() Expands variables inside other variables (e.g. ${VAR}).

Envig (Unified Manager)

Method Description
new(options EnvigOptions) Initialize a new Envig instance
config(path string) Get a configuration value (toml.Any)
env(name string) Get an environment variable
get(key string) Get config with expansion
value(key string) Get as toml.Any
get_as[T](key string) Type-safe retrieval (T can be int, bool, etc.)
config_or_default(path, default) Config value or fallback
env_or_default(name, default) Env variable or fallback
expand(value toml.Any) Expand variable references
get_or_default(key, default) Unified get with default

πŸ—οΈ Roadmap

  • Support for JSON and YAML configuration formats
  • CLI tool for managing environment variables
  • Integration with logging and debugging tools

🀝 Contributing

Feel free to submit issues, ideas, or pull requests! Let’s build something great together.


πŸ“œ License

Envig is released under the MIT License.


⭐ Show Your Support

If you like Envig, give it a ⭐ on GitHub!


Enjoy seamless configuration management with Envig! πŸš€

About

A lightweight and flexible environment variable manager for @vlang, inspired by dotenv and dotenv-expand. πŸ› οΈ

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

No packages published

Languages