Skip to content

Instantly share code, notes, and snippets.

View cocap10's full-sized avatar

Martin Piegay cocap10

View GitHub Profile
// Create staert object
s:=staert.NewStaert(command)
// Adding sources
s.AddSource(toml)
s.AddSource(f)
s.AddSource(kv)
// Parse and merge sources
loadedConfig, err := s.LoadConfig()
# This is a TOML document.
LogLevel = "DEBUG"
Timeout = "2s"
[owner]
name = "Tom Preston-Werner"
DateOfBirth = 1979-05-27T07:32:00-08:00
Rate = 0.75
// TOML source
toml:=staert.NewTomlSource("example", []string{"./toml/", "/any/other/path"})
// Flaeg source
f:=flaeg.New(command, os.Args[1:])
// Key-Value Store source
kv, err :=staert.NewKvSource(backend, options, “prefix”)
$ ./flaegdemo version
Version Rebloch
f := flaeg.New(flaegCmd, os.Args[1:])
if err := f.Run(); err != nil {
fmt.Println(err.Error())
}
$ ./flaegdemo
{"VersionName":"Rebloch","LogLevel":"INFO","Timeout":1000000000,"Db":null,"Owner":{"Name":"default owner","DateOfBirth":"2016-08-03T17:36:20.85900754+02:00","Rate":0.5}}
$ ./flaegdemo --db
{"VersionName":"Rebloch","LogLevel":"INFO","Timeout":1000000000,"Db":{"ConnectionMax":100,"ConnectionMax64":6400000000000000000,"Watch":true,"IP":"192.168.0.1"},"Owner":{"Name":"default owner","DateOfBirth":"2016-08-03T17:36:33.683130019+02:00","Rate":0.5}}
$ ./flaegdemo --db.ip=127.0.0.1 --owner.dob=1979-05-27T07:32:00Z
{"VersionName":"Rebloch","LogLevel":"INFO","Timeout":1000000000,"Db":{"ConnectionMax":100,"ConnectionMax64":6400000000000000000,"Watch":true,"IP":"127.0.0.1"},"Owner":{"Name":"default owner","DateOfBirth":"1979-05-27T07:32:00Z","Rate":0.5}}
f.AddCommand(flaegVersionCmd)
$ ./flaegdemo --help
flaegdemo is a golang program...(here the program description)
Usage: flaegdemo [--flag=flag_argument] [-f[flag_argument]] ... set flag_argument to flag(s)
or: flaegdemo [--flag[=true|false| ]] [-f[true|false| ]] ... set true/false to boolean flag(s)
Available Commands:
version Print version
Use "flaegdemo [command] --help" for more information about a command.
//Configuration is a struct which contains all differents type to field
//using parsers on string, time.Duration, pointer, bool, int, int64, time.Time, float64
type Configuration struct {
VersionName string //no description struct tag, it will not be flaged
LogLevel string `short:"l" description:"Log level"` //string type field, short flag "-l"
Timeout time.Duration `description:"Timeout duration"` //time.Duration type field
Db *DatabaseInfo `description:"Enable database"` //pointer type field (on DatabaseInfo)
Owner *OwnerInfo `description:"Enable Owner description"` //another pointer type field (on OwnerInfo)
}
@cocap10
cocap10 / flaegdemo.cmd.go
Last active August 4, 2016 15:25
Golang Configuration Made Easy With Flæg And Stært
flaegCmd := &flaeg.Command{
Name: "flaegdemo",
Description: "flaegdemo is a golang program...(here the program description)",
Config: &config,
DefaultPointersConfig: &defaultConfigOfPointerFields,
Run: func() error {
printableConfig, _ := json.Marshal(config)
fmt.Printf("%s\n", printableConfig)
return nil
},