goconf

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2025 License: MIT Imports: 7 Imported by: 0

README

CI CodeQL Coverage Status Open Issues Go Report Card GitHub release (latest by date)

Go Config

Library to load env configuration

Note: This is a fork of wgarunap/goconf

Features

  • Load configuration from environment variables
  • Validate configuration
  • Print configuration in a table format
  • Mask or hide sensitive information

Installation

go get github.com/tlmanz/goconf

How to use it

  1. Define your configuration struct:
type Conf struct {
	Name  string `env:"MY_NAME"`
	OAuth OAuth
}

type OAuth struct {
	Enabled     bool   `env:"OAUTH_ENABLED" envDefault:"False"`
	JwtKey      string `env:"JWT_KEY" envDefault:"asdasd" hush:"mask"`
	OAuthConfig OAuthConfig
}

type OAuthConfig struct {
	ClientID     string `env:"CLIENT_ID" envDefault:"234234234-23234kjh23jk4g2j3h4gjh23g4f.apps.googleusercontent.com" hush:"hide"`
	ClientSecret string `env:"CLIENT_SECRET" envDefault:"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10U_" hush:"mask"`
}

var Config Conf

func (Conf) Register() error {
    return env.Parse(&Config)
}

func (Conf) Validate() error {
    if Config.Name == "" {
        return errors.New(`MY_NAME environmental variable cannot be empty`)
    }
    return nil
}

func (Conf) Print() interface{} {
    return Config
}
  1. Load the configuration:
func main() {
    _ = os.Setenv("MY_NAME", "My First Configuration")

    err := goconf.Load(
        new(Conf),
    )
    if err != nil {
        log.Fatal(err)
    }
    if Config.Name != `My First Configuration` {
        log.Fatal(`error while comparing config`)
    }

    log.Info(`goconf successfully loaded`)
}

Example Output

alt Sample

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(configs ...Configer) error

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

Jump to

Keyboard shortcuts

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