confdir

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2022 License: MIT Imports: 5 Imported by: 0

README

ConfDir

ConfDir provides an easy interface, to store conf into your home dir.

How to Use

Can see this example file: (the file also at examples/load_conf/main.go)

package main

import (
	"log"
	
	_ "embed"

	"github.com/flyfy1/confdir"
)

// secret is your config object
type secret struct {
	Username string `yaml:"username"`
	Password string `yaml:"password"`
}

// config is another kind of your config object 
type config struct {
	StayHappy bool   `yaml:"stay_happy"`
	Path      string `yaml:"path"`
}

// you can prepare an example `secret_config.yml`, and use `go:embed` to assign its content to a variable
//go:embed secret_config.yml
var exampleSecretContent string

func main() {
	// use `~/.tools` as the config folder; files registered would be put under this config folder
	loader := confdir.NewYamlLoader(".tools", confdir.WithLogFunc(log.Printf))

	// register config object with its corresponding file; use `config.RegWithExampleContent` to define the content of 
	// the example config when the file doesn't exist
	secretCfg := &secret{}
	loader.RegisterFile(".secret.yml", secretCfg, confdir.RegWithExampleContent(exampleSecretContent))

	// you can assign default configs. If `confdir.RegWithExampleContent` isn't passed into RegisterFile, it simply 
	// ignores it (unless, `confdir.RegErrorOnNoFile` is set, then `loader.LoadAll()` would return error instead)
	normalCfg := &config{
		StayHappy: true,
		Path:      "/var/etc/sshd",
	}
	loader.RegisterFile("config.yml", normalCfg)

	// loader.LoadAll() would: 1. check if folder exist in home directory; 2. load config files defined
	err := loader.LoadAll()

	// if `confdir.RegErrorOnNoFile` is set, and no ExampleContent provided, err would be `os.ErrNoFile` when 
	// ConfigFile not exist
	log.Println("load err: ", err)      // load err: nil
	
	log.Println("secret config loaded: ", secretCfg)    // secret config loaded:  &{example }
	log.Println("normal config not touched: ", normalCfg)   // normal config not touched:  &{true /var/etc/sshd}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type OptionFunc

type OptionFunc func(dir *YamlLoader)

func WithLogFunc

func WithLogFunc(logger logFunc) OptionFunc

type RegisterOptionFunc

type RegisterOptionFunc func(loader *cfgLoader)

func RegErrorOnNoFile

func RegErrorOnNoFile() RegisterOptionFunc

RegErrorOnNoFile if registered file not exist, and no exampleContent provided, return error (i.e., loading failed)

func RegWithExampleContent

func RegWithExampleContent(content string) RegisterOptionFunc

type YamlLoader

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

func NewYamlLoader

func NewYamlLoader(configFolder string, options ...OptionFunc) *YamlLoader

NewYamlLoader creates YamlLoader with configFolder relative path to $Home. If configFolder is empty, the config file would be put under $HOME directly

func (*YamlLoader) LoadAll

func (loader *YamlLoader) LoadAll() error

LoadAll loads all configs registered

func (*YamlLoader) RegisterFile

func (loader *YamlLoader) RegisterFile(filename string, cfg interface{}, options ...RegisterOptionFunc)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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