confl

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2018 License: MIT Imports: 7 Imported by: 0

README

Confl

Load or reload configuration!

Build Status Coverage Status License GoDoc

Features

  • Simple API for use
  • Used as a library
  • Care about updates of configuration
  • Support Auto-Reload

Getting Started

Install
Install confl
go get -u -v github.com/teambition/confl
Usage
Watch a configuration file
package main

import (
	"fmt"

	"github.com/teambition/confl"
	"github.com/teambition/confl/examples/config"
	"gopkg.in/yaml.v2"
)

func main() {
	watcher, err := confl.NewFileWatcher(&config.Config{}, "./default.yaml", yaml.Unmarshal)
	if err != nil {
		panic(err)
	}
	defer watcher.Close()

	watcher.OnError(func(err error) {
		fmt.Println("your error handler start")
		fmt.Println(err)
	})

	// add hook for update events
	// perhaps you need reload something that depends the configuration
	watcher.AddHook(func(oc, nc interface{}) {
		ocfg := oc.(config.Config)
		ncfg := nc.(config.Config)
		// use cfg
		fmt.Printf("old config: %#v\n", ocfg)
		fmt.Printf("new config: %#v\n", ncfg)
	})

	// get configuration from watcher
	cfg := watcher.Config().(config.Config)
	// use cfg
	fmt.Printf("load config: %#v\n", cfg)

	// start watch
	// it is a blocking method choose run with `go` by situation
	watcher.Watch()
}

More example

Documentation

Index

Constants

View Source
const Version = "1.4.0"

Version -

Variables

This section is empty.

Functions

This section is empty.

Types

type Hook

type Hook func(oldCfg, newCfg interface{})

Hook the handler of update events pass the value of configuration(not ptr) to avoiding changing it

type Unmarshal added in v1.4.0

type Unmarshal func([]byte, interface{}) error

Unmarshal - unmarshal function for FileWatcher

type Watcher

type Watcher interface {
	// Config return the copy of configuration struct
	// Example:
	//   cfg := Config().(MyConfigStruct)
	Config() interface{}
	// Close close the watcher
	Close() error
	// AddHook add hooks for the update events of configuration
	AddHook(...Hook)
	// OnError add error handle for error cases
	OnError(func(error))
	// Watch start watch the update events
	// It is blocked until the watcher is closed
	Watch()
}

Watcher concern about the changes of configuration

func NewFileWatcher added in v1.3.0

func NewFileWatcher(c interface{}, confPath string, fns ...Unmarshal) (Watcher, error)

NewFileWatcher returns new a Watcher for file system

func NewMockWatcher added in v1.4.0

func NewMockWatcher(c interface{}) (Watcher, error)

NewMockWatcher new watcher with pointer of config stucture

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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