ConfigManager
configManager is a Go module that simplifies the management of configuration data from various sources such as environment variables, JSON, YAML, and .env files. It provides a flexible way to handle complex configurations, including nested structs, validation, default values, and custom parsing.
Install / Use
/learn @syntaxLabz/ConfigManagerREADME
configManager: Advanced Configuration Loader for Go
Overview
configManager is a Go module that simplifies the management of configuration data from various sources such as environment variables, JSON, YAML, and .env files. It provides a flexible way to handle complex configurations, including nested structs, validation, default values, and custom parsing. This module also caches configuration values in memory for efficient access.
Features
- Multiple Sources: Supports
.env, JSON, and YAML files. - Default Values: Automatically applies default values when environment variables are missing.
- Validation: Supports required fields and throws errors for missing variables.
- Nested Structs: Handles deeply nested structs with ease.
- Custom Parsing: Allows custom parsing (e.g., JSON strings).
- Environment-Specific Files: Supports app-specific configurations based on the
APP_ENVvariable (e.g.,.dev.env,.prod.env). - Caching: Caches frequently accessed configuration data to improve performance.
- Recursive Directory Search: Searches up to 3 levels of subdirectories to find configuration files(configs dir).
- Automatic Binding: Automatically binds configuration data to struct fields.
Installation
To install the module, use the following command:
go get github.com/syntaxLabz/configManager
Example Usage
1. Load Configurations
package main
import (
"fmt"
"log"
"github.com/syntaxLabz/configManager"
)
func main() {
// Create a new config manager
cm := configManager.New()
// Define a struct to hold your configuration
var config AppConfig
// Unmarshal data from environment variables into the config struct
err := cm.Unmarshal(&config)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Config Loaded: %+v\n", config)
}
2. Define Configuration Struct
type AppConfig struct {
DBUrl string `env:"DB_URL" default:"postgres://localhost:5432" required:"true"`
APIToken string `env:"API_TOKEN"`
ServerPort int `env:"SERVER_PORT" default:"8080"`
DebugMode bool `env:"DEBUG_MODE" default:"false"`
}
3. Unmarshal Function
The Unmarshal function automatically populates your struct fields based on environment variables or configuration file data.
err := cm.Unmarshal(&config)
if err != nil {
log.Fatalf("Error loading config: %v", err)
}
4. Caching Configuration Data
To optimize access to frequently used configuration data, the module provides in-memory caching. Data is stored in a small in-memory cache to avoid repeatedly accessing the OS or reading from files.
5. Clearing Cache
You can clear the in-memory cache if needed:
cm.ClearCache()
Configuration File Support
This module supports three main configuration file types:
-
.envFiles- Each line in the
.envfile contains a key-value pair. - Example:
DB_URL=postgres://localhost:5432 API_TOKEN=my-api-token SERVER_PORT=8080 DEBUG_MODE=true
- Each line in the
-
JSON Files
- Configuration data can be in standard JSON format.
- Example:
{ "DB_URL": "postgres://localhost:5432", "API_TOKEN": "my-api-token", "SERVER_PORT": 8080, "DEBUG_MODE": true }
-
YAML Files
- Configuration data in YAML format.
- Example:
DB_URL: postgres://localhost:5432 API_TOKEN: my-api-token SERVER_PORT: 8080 DEBUG_MODE: true
How It Works
The configManager module performs the following actions:
- It searches for configuration files (up to 3 levels deep) in a specified directory (
basePath). - It loads configuration files based on a priority order: first
.env, then.json, and lastly.yaml. If a configuration file is found, it will not search for other file types. - It supports environment-specific configurations using the
APP_ENVenvironment variable. For example, ifAPP_ENVis set todev, the module will look for.dev.env,.dev.json, or.dev.yamlfiles in the specified directory. - It parses the configuration files and environment variables.
- It binds the data to a provided struct using reflection.
- It supports caching to avoid re-reading the same configuration multiple times, ensuring better performance.
Configuration Loading Process:
- Search for Config Files: The module scans the specified directory for valid
.env,.json, or.yamlfiles.- First, it looks for
.env,.json, or.yamlfiles in the base directory in the order of priority. - Then, if the
APP_ENVenvironment variable is set, it looks for environment-specific files, such as.dev.env,.prod.env,.dev.json, or.prod.json, in the same priority order.
- First, it looks for
- Load Data: It reads the file content, parses the data, and loads it into memory.
- Environment Variables: Configuration values are loaded into environment variables, and the struct fields are populated from these values using reflection.
- Cache: Frequently accessed configuration data is cached in memory to avoid reloading it repeatedly, improving performance.
- Validation and Defaults: It ensures that required fields are set and assigns default values to fields that are missing.
Advanced Features
- File Priority: The module loads configuration files based on a defined priority:
.env>.json>.yaml. If a file is found in one of these formats, it will stop searching for the other formats. - Environment-Specific Files: Supports loading different configuration files based on the environment (e.g.,
.dev.env,.prod.env). If theAPP_ENVenvironment variable is set, it will attempt to load corresponding environment-specific files. - Nested Structs: Supports nested structs, allowing for more complex configuration structures (e.g., YAML, JSON files with nested fields).
- Custom Parsing: Supports custom types by implementing the
Unmarshalinterface. This allows for more advanced data manipulation during the unmarshalling process. - Validation: Ensures that required configuration fields are set and validates their values, ensuring that no required configurations are missing.
- Cache Management: The cache can be cleared manually or set to expire after a certain period, ensuring that configuration data remains up-to-date.
- Flexible Configuration Sources: The module supports loading configuration data from
.env,.json, and.yamlfiles. The order of loading is flexible based on whetherAPP_ENVis set or not.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions
Feel free to fork the repository, make changes, and create pull requests! We welcome contributions that improve functionality or fix bugs.
Related Skills
diffs
341.8kUse the diffs tool to produce real, shareable diffs (viewer URL, file artifact, or both) instead of manual edit summaries.
clearshot
Structured screenshot analysis for UI implementation and critique. Analyzes every UI screenshot with a 5×5 spatial grid, full element inventory, and design system extraction — facts and taste together, every time. Escalates to full implementation blueprint when building. Trigger on any digital interface image file (png, jpg, gif, webp — websites, apps, dashboards, mockups, wireframes) or commands like 'analyse this screenshot,' 'rebuild this,' 'match this design,' 'clone this.' Skip for non-UI images (photos, memes, charts) unless the user explicitly wants to build a UI from them. Does NOT trigger on HTML source code, CSS, SVGs, or any code pasted as text.
openpencil
1.9kThe world's first open-source AI-native vector design tool and the first to feature concurrent Agent Teams. Design-as-Code. Turn prompts into UI directly on the live canvas. A modern alternative to Pencil.
ui-ux-designer
Use this agent when you need to design, implement, or improve user interface components and user experience flows. Examples include: creating new pages or components, improving existing UI layouts, implementing responsive designs, optimizing user interactions, building forms or dashboards, analyzing existing UI through browser snapshots, or when you need to ensure UI components follow design system standards and shadcn/ui best practices.\n\n<example>\nContext: User needs to create a new dashboard page for team management.\nuser: "I need to create a team management dashboard where users can view team members, invite new members, and manage roles"\nassistant: "I'll use the ui-ux-designer agent to design and implement this dashboard with proper UX considerations, using shadcn/ui components and our design system tokens."\n</example>\n\n<example>\nContext: User wants to improve the user experience of an existing form.\nuser: "The signup form feels clunky and users are dropping off. Can you improve it?"\nassistant: "Let me use the ui-ux-designer agent to analyze the current form UX and implement improvements using our design system and shadcn/ui components."\n</example>\n\n<example>\nContext: User wants to evaluate and improve existing UI.\nuser: "Can you take a look at our pricing page and see how we can make it more appealing and user-friendly?"\nassistant: "I'll use the ui-ux-designer agent to take a snapshot of the current pricing page, analyze the UX against Notion-inspired design principles, and implement improvements using our design tokens."\n</example>
