goconf

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2025 License: MIT Imports: 7 Imported by: 0

README

goconf

a quick and simple library for managing dynamic configs.

made this primarily for myself for simplifying the making of my projects.

installation

you can install the lib from go get:

go get codeberg.org/dura5ka/goconf

...and include "codeberg.org/dura5ka/goconf" in your imports.

or, you can just include the main.go file somewhere in your application.

usage

// main.go
package main

import (
	"fmt"
	"log"
	"codeberg.org/dura5ka/goconf"
)

// struct options MUST be capitalized; you should use
// struct tags to identify the options in the config.
type section1 struct {
	Value1 string `mapstructure:"value1"`
	Value2 int `mapstructure:"value2"`
}

type dbConf struct {
	Address  string `mapstructure:"address"`
	Port     int `mapstructure:"port"`
	User     string `mapstructure:"user"`
	Password string `mapstructure:"password"`
}

func main() {
	// first, initialize the config manager.
	cfgManager := goconf.New()

	// second, create pointers to instances of the structs.
	sect1 := &section1{}
	db := &dbConf{}

	// third, register the config section(s) you'd like.
	cfgManager.Register("section1", sect1)
	cfgManager.Register("db", db)

	// finally, load the config.
	if err := cfgManager.Load("config.toml"); err != nil {
		log.Printf("failed to load the config: %s\n", err)
	}

	// now, you can access options like this:
	fmt.Printf(
		"started db on %s:%d with user %s and password %s\n",
		db.address,
		db.port,
		db.user,
		db.password
	)
}
# config.toml
[section1]
value1 = "hi"
value2 = 123

[db]
address = "127.0.0.1"
port = 8888
user = "database"
password = "database"

license

the project is licensed under MIT.

Documentation

Overview

package goconf provides a quick and simple library for managing dynamic configs from toml, json, or yaml files.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Manager

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

Manager holds all the registered config structs and handles loading.

func Init

func Init() *Manager

Init creates and returns the initialized Manager. this is the starting point for the library.

func (*Manager) Load

func (m *Manager) Load(path string) error

Load reads the specified config file, detects its type, and populates all registered structs with their corresponding data. it returns an error if the file cannot be found, read, or unmarshaled.

func (*Manager) Register

func (m *Manager) Register(section string, cfgStruct any)

Register adds a configuration struct to the Manager. the section parameter is the top-level key in your config file (e.g., "database"). the cfgStruct parameter must be a pointer to a struct that will hold the data.

Jump to

Keyboard shortcuts

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