goconf

package module
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2025 License: MIT Imports: 7 Imported by: 4

README

Go Config

Library to load env configuration in Golang

Features
  • Load Env Configuration
  • Print configuration
  • Validate Env Configuration
How to use it
package main

import (
	"errors"
	"github.com/wgarunap/goconf"
	"log"
)

type Conf struct {
	Name        string `env:"MY_NAME" validate:"required"`
	ExampleHost string `env:"EXAMPLE_HOST" validate:"required,uri"`
	Port        int    `env:"EXAMPLE_PORT" validate:"gte=8080,lte=9000"`
	Password    string `env:"MY_PASSWORD" secret:"true"`
}

var Config Conf

func (Conf) Register() error {
	return goconf.ParseEnv(&Config)
}

func (Conf) Validate() error {
	return goconf.StructValidator(Config)
}

func (Conf) Print() interface{} {
	return Config
}

func main() {
    if err := goconf.Load(new(Conf));err!=nil{
        log.Fatal(err)
    }
    
    log.Println(`configuration successfully loaded`)
}

Documentation

Index

Constants

View Source
const SensitiveDataMaskString = "***************"

Variables

This section is empty.

Functions

func Load

func Load(configs ...Configer) error

func ParseEnv added in v0.9.0

func ParseEnv(config interface{}) error

ParseEnv parse the env values to given struct fields env variables are defined using struct tags. It utilizes the "github.com/caarlos0/env/v11" package for parse.

Parameters:

  • config (interface{}): The struct to be populated by env variables. The struct should have env variables defined using tags such as `env:"USERNAME"`.

Returns:

  • error: Returns nil if the struct created using env variables. if fails, it returns an error which provides details information about the error

Usage Example:

type Config struct {
    Name string `env:"MY_NAME"`
    Age  int    `env:"MY_AGE"`
}

var conf Config
if err := Register(&conf); err != nil {
    // Handle env pass error, e.g., log or return
}

Note:

  • The function will panic if the `config` parameter is not a pointer to struct

More env package information https://github.com/caarlos0/env/v11

func StructValidator added in v0.8.0

func StructValidator(config interface{}) error

StructValidator validates a struct's fields against the validation rules defined using struct tags. It utilizes the "github.com/go-playground/validator/v10" package for validation.

Parameters:

  • config (interface{}): The struct to be validated. The struct should have validation rules defined using tags such as `validate:"required"`.

Returns:

  • error: Returns nil if the struct passes validation. If validation fails, it returns an error of type `validator.ValidationErrors` which provides detailed information about validation failures.

Usage Example:

type Config struct {
    Name string `validate:"required"`
    Age  int    `validate:"gte=0"`
}

config := Config{
    Name: "",
    Age: -1,
}

if err := StructValidator(config); err != nil {
    // Handle validation errors, e.g., log or return
}

Note:

  • The validator is initialized with the `WithRequiredStructEnabled` option, which ensures that nil struct fields are treated as invalid.
  • The function will panic if the `config` parameter is not a struct or pointer to a struct.

More validator information https://github.com/go-playground/validator

Types

type Configer

type Configer interface {
	Register() error
}

type Printer

type Printer interface {
	Print() interface{}
}

type Validater

type Validater interface {
	Validate() error
}

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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