constructs

package
v0.0.0-...-13c8b96 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2017 License: BSD-3-Clause Imports: 25 Imported by: 0

Documentation

Overview

Example
package main

import (
	"crypto/aes"
	"fmt"
	"os"

	"github.com/kr/pretty"
	"github.com/pierrec/construct"
	"github.com/pierrec/construct/constructs"
)

func init() {
	key := []byte("this is a private key for aes256")
	var err error
	constructs.PasswordBlock, err = aes.NewCipher(key)
	if err != nil {
		panic(err)
	}
}

type Server struct {
	constructs.ConfigFileINI

	Host     string
	Port     int
	Login    string
	Password constructs.Password
}

var _ construct.Config = (*Server)(nil)
var _ construct.FromFlags = (*Server)(nil)

func (c *Server) DoConfig() {}

// UsageConfig returns the usage for the Server struct fields.
// The UsageConfig method for the embedded struct is automatically called by construct.
func (c *Server) UsageConfig(name string) string {
	switch name {
	case "host":
		return "host to connect to"
	case "port":
		return "listening port to connect to"
	case "login":
		return "login username"
	case "password":
		return "password for the user"
	}
	return ""
}

func main() {
	Server := &Server{
		ConfigFileINI: constructs.ConfigFileINI{
			Name:   "config.ini",
			Backup: ".bak",
			Save:   true},
		Host:     "localhost",
		Port:     80,
		Login:    "xxlogin",
		Password: "xxpwd",
	}
	defer func() {
		os.Remove("config.ini")
		os.Remove("config.bak")
	}()

	err := construct.Load(Server)
	if err != nil {
		fmt.Println(err)
		return
	}

	pretty.Println(Server)

}
Output:

	&construct_test.Server{
    ConfigFileINI: constructs.ConfigFileINI{
        Name:       "config.ini",
        Backup:     ".bak",
        Save:       true,
        configFile: constructs.configFile{},
    },
    Host:     "localhost",
    Port:     80,
    Login:    "xxlogin",
    Password: "xxpwd",
}

Index

Examples

Constants

View Source
const BuildInfoMessage = "version {{.Version}} commit {{.Commit}} built on {{.BuildTime}}\n"

BuildInfoMessage is the default template used to display the BuildInfo.

Variables

View Source
var ConfigLogDefault = ConfigLog{
	Level:      "error",
	MaxSize:    10 << 20,
	MaxAge:     30,
	MaxBackups: 3,
	LocalTime:  true,
}

ConfigLogDefault represents sensible values for a default ConfigLog.

View Source
var ErrInvalidPassword = errors.New("invalid password")

ErrInvalidPassword is returned when extracting an encrypted password fails.

View Source
var PasswordBlock cipher.Block

PasswordBlock is the cipher block used by the Password type to encrypt/decrypt a password.

It must be set for the Password type to be functional.

Functions

func NewStoreINI

func NewStoreINI(lookup construct.LookupFn) construct.Store

NewStoreINI returns a Store based on the INI format.

func NewStoreJSON

func NewStoreJSON(lookup construct.LookupFn) construct.Store

NewStoreJSON returns a Store based on the JSON format.

func NewStoreTOML

func NewStoreTOML(lookup construct.LookupFn) construct.Store

NewStoreTOML returns a Store based on the TOML format.

func NewStoreYAML

func NewStoreYAML(lookup construct.LookupFn) construct.Store

NewStoreYAML returns a Store based on the YAML format.

Types

type BuildInfo

type BuildInfo struct {
	Show    bool   `cfg:"version"`
	Message string `cfg:"-"`
	Data    struct {
		Version   string
		Commit    string
		BuildTime string
	}
}

BuildInfo provides a way to display a binary build information. The Data part must be set during the binary initialization, typically by providing the info with the go linker into custom string variables and setting them to the Data fields.

func (*BuildInfo) Init

func (bi *BuildInfo) Init() (err error)

Init displays the build information to os.Stdout and exits.

func (*BuildInfo) Usage

func (bi *BuildInfo) Usage(name string) string

Usage returns the BuildInfo usage for each of its options.

type BytesSize

type BytesSize uint64

BytesSize implements reading and writing bytes sizes.

func (BytesSize) MarshalText

func (sz BytesSize) MarshalText() ([]byte, error)

MarshalText makes BytesSize implement encoding.TextMarshaler.

func (*BytesSize) UnmarshalText

func (sz *BytesSize) UnmarshalText(text []byte) error

UnmarshalText makes BytesSize implement encoding.TextUnmarshaler.

type ConfigFile

type ConfigFile struct {
	// Name of the config file.
	// If no name is specified, the file is not loaded by LoadConfig()
	// and stdout is used if Save is true.
	Name string `ini:"-" toml:"-" json:"-" yaml:"-"`
	// Backup file extension.
	// The config file is first copied before being overwritten using this value.
	// Leave empty to disable.
	Backup string `ini:"-" toml:"-" json:"-" yaml:"-"`
	// ToSave the config file once the whole config has been loaded.
	ToSave bool `cfg:"Save" ini:"-" toml:"-" json:"-" yaml:"-"`
}

ConfigFile implements most of FromIO except New and should be embedded into another type that provides it.

func (*ConfigFile) Init

func (*ConfigFile) Init() error

Init initializes the ConfigFile.

func (*ConfigFile) Load

func (c *ConfigFile) Load() (io.ReadCloser, error)

Load returns an io.ReadCloser if the Name is set and the file exists.

func (*ConfigFile) Save

func (c *ConfigFile) Save() (io.WriteCloser, error)

Save returns an io.WriteCloser if the Save flag is set to true. If the Name is empty, it defaults to stdout. If the backup extension is set, the file is first renamed with it, then a new one is created and returned.

func (*ConfigFile) Usage

func (c *ConfigFile) Usage(name string) string

Usage returns the ConfigFile usage for each of its options.

type ConfigFileINI

type ConfigFileINI struct {
	ConfigFile `cfg:",inline"`
}

ConfigFileINI implements the FromIO interface for INI formatted files.

func (*ConfigFileINI) New

New returns the Store for an INI formatted file.

type ConfigFileJSON

type ConfigFileJSON struct {
	ConfigFile `cfg:",inline"`
}

ConfigFileJSON implements the FromIO interface for JSON formatted files.

func (*ConfigFileJSON) New

New returns the Store for a JSON formatted file.

type ConfigFileTOML

type ConfigFileTOML struct {
	ConfigFile `cfg:",inline"`
}

ConfigFileTOML implements the FromIO interface for TOML formatted files.

func (*ConfigFileTOML) New

New returns the Store for a TOML formatted file.

type ConfigFileYAML

type ConfigFileYAML struct {
	ConfigFile `cfg:",inline"`
}

ConfigFileYAML implements the FromIO interface for JSON formatted files.

func (*ConfigFileYAML) New

New returns the Store for a YAML formatted file.

type ConfigLog

type ConfigLog struct {
	Filename   string
	Level      string
	MaxSize    BytesSize
	MaxAge     int
	MaxBackups int
	LocalTime  bool
	// contains filtered or unexported fields
}

ConfigLog provides the options for the logging facility. The logger is based on CoLog (https://texlution.com/post/colog-prefix-based-logging-in-golang/).

func (*ConfigLog) Init

func (lg *ConfigLog) Init() error

Init makes ConfigLog implement Config.

func (*ConfigLog) Usage

func (lg *ConfigLog) Usage(name string) string

Usage makes ConfigLog implement Config.

type Password

type Password string

Password implements encrypting and decrypting a password when serialized.

PasswordBlock must be set for the Password type to be functional.

func (Password) MarshalText

func (p Password) MarshalText() ([]byte, error)

MarshalText makes Password implement encoding.TextMarshaler.

func (*Password) UnmarshalText

func (p *Password) UnmarshalText(text []byte) error

UnmarshalText makes Password implement encoding.TextUnmarshaler.

Jump to

Keyboard shortcuts

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