goconf

package module
v0.0.0-...-fa53f8e Latest Latest
Warning

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

Go to latest
Published: May 27, 2026 License: AGPL-3.0 Imports: 6 Imported by: 0

README

Tests Made with Golang

goconf

A wrapper for a few modules I use for TOML-parsed configuration in my Go projects

Modules Used

You should really read the documentation for the following modules, as you will define the struct tags for your config struct using these modules.


Usage

First, install the module:

go get -u github.com/z46-dev/goconf@latest

The most simple usage is just to load a config file into a struct, panicking if there are any errors:

package main

import "github.com/z46-dev/goconf"

type Configuration struct {
    Name string `toml:"name" default:"John Doe" validate:"required"`
    Age  int    `toml:"age" default:"30" validate:"required,gte=21"`
}

func main() {
    var cfg Configuration = goconf.MustLoadConfig[Configuration]("./config.toml")

    // ...
}

You can also pass options to LoadConfig or MustLoadConfig to customize certain behaviors. For example, you can specify what to do if the config file doesn't exist:

if cfg, err := goconf.LoadConfig[Configuration]("./config.toml", goconf.WithNewFileBehavior(goconf.NewFileBehaviorCreateAndTry)); err != nil {
    // handle error
} else {
    // use cfg
}

Currently, the WithNewFileBehavior option has three behaviors:

  • NewFileBehaviorReject: if the file doesn't exist, return an error (this is the default behavior)
  • NewFileBehaviorCreateAndReject: if the file doesn't exist, create it with default values based on the struct tags, but still return an error (so the user can fill it out before trying again)
  • NewFileBehaviorCreateAndTry: if the file doesn't exist, create it with default values based on the struct tags, and then try to load it again immediately (so the user doesn't have to do anything on the first run, but it will still error if the defaults don't pass validation)

The other option is WithIndentSpaces, which allows you to specify how many spaces to use for indentation when encoding the config struct back to a file. By default, it uses 4 spaces.

if cfg, err := goconf.LoadConfig[Configuration]("./config.toml", goconf.WithIndentSpaces(2)); err != nil {
    // handle error
} else {
    // use cfg
}

Example

You can run the example in example/main.go to see how it works.

go run github.com/z46-dev/goconf/example@latest

Documentation

Index

Constants

View Source
const (
	NewFileBehaviorReject          newFileBehavior = iota // If the file doesn't exist, return an error
	NewFileBehaviorCreateAndReject                        // If the file doesn't exist, create it with any defaults, but still return an error
	NewFileBehaviorCreateAndTry                           // If the file doesn't exist, create it with any defaults, and then try to load it (which may still error if required fields are missing)

)

Variables

This section is empty.

Functions

func LoadConfig

func LoadConfig[ConfigType any](path string, options ...Option) (config ConfigType, err error)

LoadConfig loads a TOML config file into a struct of type ConfigType.

func MustLoadConfig

func MustLoadConfig[ConfigType any](path string, options ...Option) (config ConfigType)

MustLoadConfig is a helper that panics if LoadConfig returns an error. Useful for cases where you want to load a config at startup and want the program to fail fast if the config is invalid.

Types

type Option

type Option func(*_options)

func WithIndentSpaces

func WithIndentSpaces(spaces int) Option

Set the indentation spaces for the generated TOML file (default: 4)

func WithNewFileBehavior

func WithNewFileBehavior(behavior newFileBehavior) Option

Set the behavior for when the config file doesn't exist (default: Reject)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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