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 ¶
- Constants
- Variables
- func NewStoreINI(lookup construct.LookupFn) construct.Store
- func NewStoreJSON(lookup construct.LookupFn) construct.Store
- func NewStoreTOML(lookup construct.LookupFn) construct.Store
- func NewStoreYAML(lookup construct.LookupFn) construct.Store
- type BuildInfo
- type BytesSize
- type ConfigFile
- type ConfigFileINI
- type ConfigFileJSON
- type ConfigFileTOML
- type ConfigFileYAML
- type ConfigLog
- type Password
Examples ¶
Constants ¶
const BuildInfoMessage = "version {{.Version}} commit {{.Commit}} built on {{.BuildTime}}\n"
BuildInfoMessage is the default template used to display the BuildInfo.
Variables ¶
var ConfigLogDefault = ConfigLog{ Level: "error", MaxSize: 10 << 20, MaxAge: 30, MaxBackups: 3, LocalTime: true, }
ConfigLogDefault represents sensible values for a default ConfigLog.
var ErrInvalidPassword = errors.New("invalid password")
ErrInvalidPassword is returned when extracting an encrypted password fails.
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 ¶
NewStoreINI returns a Store based on the INI format.
func NewStoreJSON ¶
NewStoreJSON returns a Store based on the JSON format.
func NewStoreTOML ¶
NewStoreTOML returns a Store based on the TOML 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.
type BytesSize ¶
type BytesSize uint64
BytesSize implements reading and writing bytes sizes.
func (BytesSize) MarshalText ¶
MarshalText makes BytesSize implement encoding.TextMarshaler.
func (*BytesSize) UnmarshalText ¶
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) 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.
type ConfigFileJSON ¶
type ConfigFileJSON struct {
ConfigFile `cfg:",inline"`
}
ConfigFileJSON implements the FromIO interface for JSON formatted files.
type ConfigFileTOML ¶
type ConfigFileTOML struct {
ConfigFile `cfg:",inline"`
}
ConfigFileTOML implements the FromIO interface for TOML formatted files.
type ConfigFileYAML ¶
type ConfigFileYAML struct {
ConfigFile `cfg:",inline"`
}
ConfigFileYAML implements the FromIO interface for JSON formatted files.
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/).
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 ¶
MarshalText makes Password implement encoding.TextMarshaler.
func (*Password) UnmarshalText ¶
UnmarshalText makes Password implement encoding.TextUnmarshaler.