conf

package
v0.13.2 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package conf contains tools for managing app configuration.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConfigDir added in v0.2.0

func ConfigDir(app string) string

ConfigDir returns the path to the config dir for the app. Path precedence:

  • $XDG_CONFIG_HOME/$name
  • $AppData/$name (windows only)
  • $HOME/.config/$name

func ConfigFile added in v0.2.0

func ConfigFile(app string) string

ConfigFile returns the default config path for the app.

func ConfigFileFor added in v0.2.0

func ConfigFileFor(app string, filename string) string

ConfigFileFor returns the config path for the app and file name.

func DataDir added in v0.2.0

func DataDir(app string) string

DataDir returns the path to the local state dir for the app. Path precedence:

  • $XDG_DATA_HOME/$name
  • $LocalAppData/$name (windows only)
  • $HOME/.local/share/$name

func StateDir added in v0.2.0

func StateDir(app string) string

StateDir returns the path to the local state dir for the app. Path precedence:

  • $XDG_STATE_HOME/$name
  • $LocalAppData/$name (windows only)
  • $HOME/.local/state/$name

Types

type Loader

type Loader[C any] struct {
	Config C
	Path   string
	// contains filtered or unexported fields
}

Loader populates and validates config data from a file.

Example
type Config struct {
	DatabaseURL string `default:"postgres://0.0.0.0:5432/db" validate:"required,url"`
	Debug       bool   `default:"true"`
}

loader := NewLoader(&Config{}, ConfigFile("termite"))
config, err := loader.Load()
if err != nil {
	panic(err)
}

fmt.Printf("DatabaseURL: %v, Debug: %v", config.DatabaseURL, config.Debug)
Output:

DatabaseURL: postgres://0.0.0.0:5432/db, Debug: true

func NewLoader

func NewLoader[C any](config C, path string) *Loader[C]

NewLoader returns a new Loader.

func (*Loader[C]) Load

func (l *Loader[C]) Load() (C, error)

Load reads the config file at Path into the Config struct and returns it. Subsequent calls will bypass the read and just return the cached struct.

func (*Loader[C]) Reload

func (l *Loader[C]) Reload() (C, error)

Reload forces a reread of the config file at Path.

Jump to

Keyboard shortcuts

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