config

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2024 License: MIT Imports: 8 Imported by: 0

README

Efficient Configuration and Environment Variable Management for Golang

License Go Report Card GoDoc

Config is a lightweight and flexible Golang package designed to simplify configuration management in your applications. It seamlessly handles configuration files and environment variables, providing a unified interface for accessing and managing your application's settings.

Features

  • Simple and easy to use
  • Supports environment variables
  • Supports configuration files
  • Supports nested configuration
  • Supports default values
  • Supports validation
  • Supports custom configuration sources

Installation

go get -u github.com/josemukorivo/config

Usage

Configuration File

main.go

package main

import (
	"fmt"
	"log"

	"github.com/josemukorivo/config"
)

type Config struct {
	Host string
	Port int
}

func main() {
	var cfg Config

	// Load configuration from environment variables file
	if err := config.Parse("app", &cfg, ".env.local"); err != nil {
		log.Fatal(err)
	}

	fmt.Println(cfg.Host)
	fmt.Println(cfg.Port)
}

.env.local

APP_HOST=localhost
APP_PORT=8080
Default Values
package main

import (
	"fmt"
	"log"

	"github.com/josemukorivo/config"
)

type Config struct {
	Host string `default:"localhost"`
	Port int    `default:"8080"`
}

func main() {
	var cfg Config

	// Looks for a file named .env by default
	if err := config.Parse("app", &cfg); err != nil {
		log.Fatal(err)
	}

	fmt.Println(cfg.Host)
	fmt.Println(cfg.Port)
}
Nested Configuration

package main

import (
	"fmt"
	"log"

	"github.com/josemukorivo/config"
)

type Config struct {
	Host string
	Port int
	DB   struct {
		Host     string
		Port     int
		Username string
		Password string
	}
}

func main() {
	var cfg Config

	if err := config.Parse("app", &cfg); err != nil {
		log.Fatal(err)
	}

	fmt.Println(cfg.Host)
	fmt.Println(cfg.Port)
	fmt.Println(cfg.DB.Host)
	fmt.Println(cfg.DB.Port)
	fmt.Println(cfg.DB.Username)
	fmt.Println(cfg.DB.Password)
}
Validation
package main

import (
	"fmt"
	"log"

	"github.com/josemukorivo/config"
)

type Config struct {
	Host string `required:"true"`
	Port int    `required:"true"`
}


func main() {
	var cfg Config

	if err := config.Parse("app", &cfg); err != nil {
		log.Fatal(err) // Missing required configuration parameters if Host or Port are not provided in the environment
	}

	fmt.Println(cfg.Host)
	fmt.Println(cfg.Port)
}

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidConfig = errors.New("config: invalid config must be a pointer to struct")
)

ErrInvalidConfig is returned when the config is not a pointer to struct.

Functions

func MustParse

func MustParse(prefix string, cfg any, envFiles ...string)

MustParse parses the config and panics if an error occurs. See Parse for more information. MustParse is a wrapper around Parse.

func Parse

func Parse(prefix string, cfg any, envFiles ...string) error

Parse parses the config, the config must be a pointer to struct and the struct can contain nested structs. The prefix is used to prefix the environment variables. For example, if the prefix is "app" and the struct contains a field named "Host", the environment variable will be "APP_HOST". If the struct contains a nested struct, the prefix will be the original prefix plus the nested struct name. For example, if the prefix is "app" and the nested struct is named "DB", the environment variable will be "APP_DB_HOST". Parse take an optional list of .env files to load. If the .env file exists, it will be loaded before parsing the config. By default, Parse will look for a .env file and parse it.

Types

type Field added in v1.0.3

type Field struct {
	Name     string
	Field    reflect.Value
	Key      string // The key used to look up the value in the environment.
	EnvKey   string // The environment variable name used when overriding the default key.
	Tags     reflect.StructTag
	Required bool
	Default  string
}

Field represents a field in a struct.

type FieldError

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

FieldError is returned when a field cannot be parsed.

func (*FieldError) Error

func (e *FieldError) Error() string

Error returns the error message for the FieldError. It includes the field name, the field value, the field type and the error returned by the parser.

type Setter added in v1.0.3

type Setter interface {
	Set(value string) error
}

Setter is the interface that wraps the Decode method. A type that implements the Setter interface can set it's value from a string value passed to the Set method.

Jump to

Keyboard shortcuts

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