env

package
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 7, 2025 License: MIT Imports: 4 Imported by: 0

README

Environment Package

Flexible environment variable access with interface-based design and functional configuration.

Features

  • Interface-based design - Custom environment providers for testing and flexibility
  • Functional configuration - Clean configuration with functional option pattern
  • Type safety - Strongly typed environment variable access
  • Mock support - Mock providers for unit testing
  • Performance - Efficient string parsing and caching

Quick Start

package main

import (
    "time"
    
    "github.com/Okja-Engineering/go-service-kit/pkg/env"
)

func main() {
    // Create environment instance
    env := env.NewEnvironment()
    
    // Get environment variables with defaults
    port := env.GetString("PORT", "8080")
    timeout := env.GetDuration("TIMEOUT", 30*time.Second)
    debug := env.GetBool("DEBUG", false)
    
    // Use in your application
    config := &Config{
        Port:     env.GetInt("PORT", 8080),
        Timeout:  env.GetDuration("TIMEOUT", 30*time.Second),
        Debug:    env.GetBool("DEBUG", false),
        LogLevel: env.GetString("LOG_LEVEL", "info"),
    }
}

Configuration

Functional Options
// Basic configuration
env := env.NewEnvironment()

// Advanced configuration
env := env.NewEnvironment(
    env.WithProvider(customProvider),
    env.WithTrimSpaces(true),
    env.WithCaseSensitive(false),
)
Available Options
func WithProvider(provider EnvironmentProvider) EnvironmentOption
func WithTrimSpaces(trim bool) EnvironmentOption
func WithCaseSensitive(sensitive bool) EnvironmentOption
Custom Environment Providers
// Implement the EnvironmentProvider interface
type CustomProvider struct {
    values map[string]string
}

func (p *CustomProvider) Get(key string) string {
    return p.values[key]
}

func (p *CustomProvider) Lookup(key string) (string, bool) {
    value, exists := p.values[key]
    return value, exists
}

// Use custom provider
provider := &CustomProvider{
    values: map[string]string{
        "PORT": "9090",
        "DEBUG": "true",
    },
}

env := env.NewEnvironment(env.WithProvider(provider))
port := env.GetString("PORT", "8080") // Returns "9090"

API Reference

Core Interfaces
type EnvironmentProvider interface {
    Get(key string) string
    Lookup(key string) (string, bool)
}
Environment Instance
type Environment struct {
    config *EnvironmentConfig
}

func NewEnvironment(options ...EnvironmentOption) *Environment
func (e *Environment) GetString(key, defaultVal string) string
func (e *Environment) GetInt(key string, defaultVal int) int
func (e *Environment) GetFloat(key string, defaultVal float64) float64
func (e *Environment) GetBool(key string, defaultVal bool) bool
func (e *Environment) GetDuration(key string, defaultVal time.Duration) time.Duration
Configuration
type EnvironmentConfig struct {
    Provider     EnvironmentProvider
    TrimSpaces   bool
    CaseSensitive bool
}

func DefaultEnvironmentConfig() *EnvironmentConfig
func NewEnvironmentConfig(options ...EnvironmentOption) *EnvironmentConfig

Examples

Basic Usage
// Simple usage with defaults
env := env.NewEnvironment()
port := env.GetString("PORT", "8080")
timeout := env.GetDuration("TIMEOUT", 30*time.Second)
debug := env.GetBool("DEBUG", false)
Advanced Configuration
// Custom configuration for testing
env := env.NewEnvironment(
    env.WithProvider(mockProvider),
    env.WithTrimSpaces(false),
    env.WithCaseSensitive(false),
)

// Use in your application
config := &Config{
    Port:     env.GetInt("PORT", 8080),
    Timeout:  env.GetDuration("TIMEOUT", 30*time.Second),
    Debug:    env.GetBool("DEBUG", false),
    LogLevel: env.GetString("LOG_LEVEL", "info"),
}
Application Configuration
package main

import (
    "time"
    
    "github.com/Okja-Engineering/go-service-kit/pkg/env"
)

type Config struct {
    Port     int
    Timeout  time.Duration
    Debug    bool
    LogLevel string
    Database DatabaseConfig
}

type DatabaseConfig struct {
    Host     string
    Port     int
    Username string
    Password string
}

func loadConfig() *Config {
    env := env.NewEnvironment()
    
    return &Config{
        Port:     env.GetInt("PORT", 8080),
        Timeout:  env.GetDuration("TIMEOUT", 30*time.Second),
        Debug:    env.GetBool("DEBUG", false),
        LogLevel: env.GetString("LOG_LEVEL", "info"),
        Database: DatabaseConfig{
            Host:     env.GetString("DB_HOST", "localhost"),
            Port:     env.GetInt("DB_PORT", 5432),
            Username: env.GetString("DB_USERNAME", "postgres"),
            Password: env.GetString("DB_PASSWORD", ""),
        },
    }
}

Testing

Using Mock Providers
func TestMyFunction(t *testing.T) {
    // Create mock provider
    mockProvider := &MockEnvironmentProvider{
        values: map[string]string{
            "TEST_PORT": "9090",
            "TEST_DEBUG": "true",
        },
    }

    // Create environment with mock
    env := env.NewEnvironment(env.WithProvider(mockProvider))

    // Test your function
    result := myFunction(env)
    // ... assertions
}
Mock Provider Implementation
type MockEnvironmentProvider struct {
    values map[string]string
}

func (m *MockEnvironmentProvider) Get(key string) string {
    return m.values[key]
}

func (m *MockEnvironmentProvider) Lookup(key string) (string, bool) {
    value, exists := m.values[key]
    return value, exists
}

Best Practices

  1. Create once, reuse - Create environment instance once and reuse throughout your application
  2. Provide sensible defaults - Always provide default values for environment variables
  3. Use mock providers for testing - Test your configuration loading with mock providers
  4. Handle errors gracefully - Invalid values fall back to defaults automatically
  5. Group related configuration - Use structs to group related environment variables

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetEnvBool

func GetEnvBool(key string, defaultVal bool) bool

func GetEnvDuration

func GetEnvDuration(key string, defaultVal time.Duration) time.Duration

func GetEnvFloat

func GetEnvFloat(key string, defaultVal float64) float64

func GetEnvInt

func GetEnvInt(key string, defaultVal int) int

func GetEnvString

func GetEnvString(key, defaultVal string) string

Types

type DefaultEnvironmentProvider added in v0.0.3

type DefaultEnvironmentProvider struct{}

DefaultEnvironmentProvider implements EnvironmentProvider using os.LookupEnv

func (*DefaultEnvironmentProvider) Get added in v0.0.3

Get returns the environment variable value or empty string if not found

func (*DefaultEnvironmentProvider) Lookup added in v0.0.3

func (p *DefaultEnvironmentProvider) Lookup(key string) (string, bool)

Lookup returns the environment variable value and whether it exists

type Environment added in v0.0.3

type Environment struct {
	// contains filtered or unexported fields
}

Environment handles environment variable access with configuration

func NewEnvironment added in v0.0.3

func NewEnvironment(options ...EnvironmentOption) *Environment

NewEnvironment creates a new Environment instance with options

func (*Environment) GetBool added in v0.0.3

func (e *Environment) GetBool(key string, defaultVal bool) bool

GetBool gets a boolean environment variable

func (*Environment) GetDuration added in v0.0.3

func (e *Environment) GetDuration(key string, defaultVal time.Duration) time.Duration

GetDuration gets a duration environment variable

func (*Environment) GetFloat added in v0.0.3

func (e *Environment) GetFloat(key string, defaultVal float64) float64

GetFloat gets a float environment variable

func (*Environment) GetInt added in v0.0.3

func (e *Environment) GetInt(key string, defaultVal int) int

GetInt gets an integer environment variable

func (*Environment) GetString added in v0.0.3

func (e *Environment) GetString(key, defaultVal string) string

GetString gets a string environment variable

type EnvironmentConfig added in v0.0.3

type EnvironmentConfig struct {
	Provider      EnvironmentProvider
	TrimSpaces    bool
	CaseSensitive bool
}

EnvironmentConfig holds configuration for environment variable handling

func DefaultEnvironmentConfig added in v0.0.3

func DefaultEnvironmentConfig() *EnvironmentConfig

DefaultEnvironmentConfig provides sensible defaults

func NewEnvironmentConfig added in v0.0.3

func NewEnvironmentConfig(options ...EnvironmentOption) *EnvironmentConfig

NewEnvironmentConfig creates a new environment config with options

type EnvironmentOption added in v0.0.3

type EnvironmentOption func(*EnvironmentConfig)

EnvironmentOption is a functional option for environment configuration

func WithCaseSensitive added in v0.0.3

func WithCaseSensitive(sensitive bool) EnvironmentOption

WithCaseSensitive enables/disables case sensitivity

func WithProvider added in v0.0.3

func WithProvider(provider EnvironmentProvider) EnvironmentOption

WithProvider sets a custom environment provider

func WithTrimSpaces added in v0.0.3

func WithTrimSpaces(trim bool) EnvironmentOption

WithTrimSpaces enables/disables trimming of whitespace

type EnvironmentProvider added in v0.0.3

type EnvironmentProvider interface {
	Get(key string) string
	Lookup(key string) (string, bool)
}

EnvironmentProvider defines the interface for environment variable access

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL