conf

package module
v1.1.5 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2023 License: BSD-3-Clause Imports: 6 Imported by: 3

README

What?

Conf just stores a struct to a TOML file, and have functions to read it again.

Why this package?

I didn't need anything fancy, but kept rewriting the same kind of read and write functions in various projects - so finally I have decided to make a small package that I can import.

Default locations

The 'LoadConfig' function tries to read from (in order):

  1. Current dir
  2. User config dir
  3. System config dir

If you want custom paths, then use the 'ReadConfig' function.

Documentation

https://pkg.go.dev/git.sr.ht/~uid/conf

Bugs, feature requests, etc.

https://todo.sr.ht/~uid/conf

Import

import (
	"git.sr.ht/~uid/conf"
)

Usage

package main

import (
	"fmt"

	"git.sr.ht/~uid/conf"
)

type MyConfig struct {
	Field1 string
	Field2 string
}

func main() {
	err := conf.SaveToUserConfigDir("MyApp", "config.toml", MyConfig{"Hey", "hey"})
	if err != nil {
		panic(err)
	}

	config := MyConfig{}
	loadPath, err := conf.LoadConfig("MyApp", "config.toml", &config)
	if err != nil {
		panic(err)
	}

	fmt.Println("Loaded config from:", loadPath)
	fmt.Println("Config data", config)
}

Documentation

Overview

For saving struct to dir, use one of the following functions: SaveToCurrentDir, SaveToUserConfigDir, SaveToSystemConfigDir, WriteConfig

For loading a config file, use one of the following functions: LoadConfig, ReadConfig

To get the various standard paths, use these functions: PathCurrentDir, PathUserConfigDir, PathSystemConfigDir

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadConfig added in v1.1.0

func LoadConfig(appName, configFile string, v any) (configPath string, err error)

Tries to read TOML config into v (pointer to struct). Paths to check (in order): 1. Current dir, 2. User config dir, 3. System config dir. Attempts to read file with provided configFile

func LoadFromCurrentDir added in v1.1.5

func LoadFromCurrentDir(configFile string, v any) (configPath string, err error)

Load Config from currentDir/appName

func LoadFromSytemConfigDir added in v1.1.5

func LoadFromSytemConfigDir(appName, configFile string, v any) (configPath string, err error)

Load Config from sytemConfigDir/appName

func LoadFromUserConfigDir added in v1.1.5

func LoadFromUserConfigDir(appName, configFile string, v any) (configPath string, err error)

Load Config from userConfigDir/appName

func PathCurrentDir

func PathCurrentDir(configFile string) (configPath string)

Return absolute path to config file in current dir with provided configFile. If current work dir cannot be determined, the configFile will be returned.

func PathSystemConfigDir

func PathSystemConfigDir(appName, configFile string) (configPath string, err error)

Return absolute path to config file in system config dir with provided configFile, or error if it fails.

func PathUserConfigDir

func PathUserConfigDir(appName, configFile string) (configPath string, err error)

Return absolute path to config file in user config dir with provided configFile, or error if it fails.

func ReadConfig

func ReadConfig(configPath string, v any) error

Read TOML file from a specific path into v.

func SaveToCurrentDir added in v1.1.1

func SaveToCurrentDir(configFile string, v any) error

Marshall struct to TOML and save in current dir with provided configFile

func SaveToSystemConfigDir added in v1.1.0

func SaveToSystemConfigDir(appName, configFile string, v any) error

Marshall struct to TOML and save in 'system config' dir with provided configFile

func SaveToUserConfigDir added in v1.1.0

func SaveToUserConfigDir(appName, configFile string, v any) error

Marshall struct to TOML and save in 'user config' dir with provided configFile

func WriteConfig

func WriteConfig(configPath string, v any) error

Marshall struct to TOML and write to provided path. WriteConfig attempts to create parent dirs, if they do not exist.

Types

This section is empty.

Jump to

Keyboard shortcuts

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