config

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2026 License: MIT Imports: 6 Imported by: 0

README

config

Configuration loading from YAML files and environment variables with automatic resolution and validation.

Install

go get github.com/kbukum/gokit

Quick Start

package main

import (
    "fmt"
    "github.com/kbukum/gokit/config"
)

type AppConfig struct {
    config.ServiceConfig `yaml:",inline" mapstructure:",squash"`
    Port                 int    `yaml:"port" mapstructure:"port"`
    DatabaseURL          string `yaml:"database_url" mapstructure:"database_url"`
}

func main() {
    var cfg AppConfig
    if err := config.LoadConfig("my-service", &cfg); err != nil {
        panic(err)
    }
    fmt.Printf("Running %s in %s mode\n", cfg.Name, cfg.Environment)
}

Key Types & Functions

Name Description
ServiceConfig Common fields: Name, Environment, Version, Debug, Logging
LoadConfig() Load config from YAML + env with auto-resolution
ConfigResolver Resolves config and .env file paths
WithConfigFile() Option to specify config file path
WithEnvFile() Option to specify .env file path
WithFileSystem() Option to inject custom filesystem
FileSystem Interface for file existence and env loading

⬅ Back to main README

Documentation

Overview

Package config provides configuration loading and validation for gokit applications.

It uses Viper to load configuration from files and environment variables, supporting multiple formats (YAML, JSON, TOML) and environment-specific overrides.

Usage

cfg, err := config.Load[MyConfig]("config.yaml")

Environment variables override file values using the APP_ prefix with underscore-separated paths (e.g., APP_DATABASE_HOST).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadConfig

func LoadConfig(serviceName string, cfg interface{}, opts ...LoaderOption) error

LoadConfig loads configuration for a service into the provided cfg struct. It searches for config.yml and .env files in standard locations, binds environment variables, and unmarshals the result into cfg.

Types

type FileSystem

type FileSystem interface {
	Exists(path string) bool
	LoadEnv(path string) error
	Getwd() (string, error)
}

FileSystem interface for file operations (useful for testing).

type LoaderConfig

type LoaderConfig struct {
	FileSystem FileSystem
	ConfigFile string // Direct config file path (optional)
	EnvFile    string // Direct env file path (optional)
}

LoaderConfig holds dependencies and optional file overrides.

type LoaderOption

type LoaderOption func(*LoaderConfig)

LoaderOption is a functional option for LoadConfig.

func WithConfigFile

func WithConfigFile(path string) LoaderOption

WithConfigFile sets an explicit config file path.

func WithEnvFile

func WithEnvFile(path string) LoaderOption

WithEnvFile sets an explicit .env file path.

func WithFileSystem

func WithFileSystem(fs FileSystem) LoaderOption

WithFileSystem sets a custom filesystem for the loader.

type RealFileSystem

type RealFileSystem struct{}

RealFileSystem implements FileSystem using actual file operations.

func (*RealFileSystem) Exists

func (rfs *RealFileSystem) Exists(path string) bool

func (*RealFileSystem) Getwd

func (rfs *RealFileSystem) Getwd() (string, error)

func (*RealFileSystem) LoadEnv

func (rfs *RealFileSystem) LoadEnv(path string) error

type ResolvedFiles

type ResolvedFiles struct {
	ConfigFile string
	EnvFile    string
}

ResolvedFiles contains the resolved config and env file paths.

type Resolver

type Resolver struct {
	FileSystem FileSystem
}

Resolver handles finding and resolving config and env files.

func (*Resolver) ResolveFiles

func (cr *Resolver) ResolveFiles(serviceName string, opts LoaderConfig) ResolvedFiles

ResolveFiles finds config and env files for a service. Returns explicit paths if provided, otherwise searches for them.

type ServiceConfig

type ServiceConfig struct {
	Name        string        `yaml:"name" mapstructure:"name"`
	Environment string        `yaml:"environment" mapstructure:"environment"`
	Version     string        `yaml:"version" mapstructure:"version"`
	Debug       bool          `yaml:"debug" mapstructure:"debug"`
	Logging     logger.Config `yaml:"logging" mapstructure:"logging"`
}

ServiceConfig contains the essential configuration fields every service needs. Projects extend this by embedding it in their own config structs.

Example:

type MyConfig struct {
    gkconfig.ServiceConfig `yaml:",inline" mapstructure:",squash"`
    Database database.Config `yaml:"database" mapstructure:"database"`
}

func (*ServiceConfig) ApplyDefaults

func (c *ServiceConfig) ApplyDefaults()

ApplyDefaults applies default values to the base configuration. Override this in embedding structs and call c.ServiceConfig.ApplyDefaults() first.

func (*ServiceConfig) GetServiceConfig

func (c *ServiceConfig) GetServiceConfig() *ServiceConfig

GetServiceConfig returns the base ServiceConfig. When embedded in a larger config struct, this method is promoted so the embedding struct automatically satisfies the Config interface.

func (*ServiceConfig) Validate

func (c *ServiceConfig) Validate() error

Validate validates the base configuration fields. Override this in embedding structs and call c.ServiceConfig.Validate() first.

Jump to

Keyboard shortcuts

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