-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
36 lines (31 loc) · 1.01 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package main
import (
"os"
"github.com/jessevdk/go-flags"
"github.com/ukane-philemon/bob/db/mongodb"
"github.com/ukane-philemon/bob/webserver"
)
type Config struct {
WebServerCfg webserver.Config `group:"Web server" namespace:"webserver"`
MongoDBCfg mongodb.Config `group:"MongoDB" namespace:"mongodb"`
DevMode bool `long:"dev" env:"DEV_MODE" description:"Enable development mode"`
}
// parseCLIConfig parses the command-line arguments into the provided struct
// with go-flags tags. If the --help flag has been passed, the struct is
// described back to the terminal and the program exits using os.Exit.
func parseCLIConfig(cfg *Config) error {
preParser := flags.NewParser(cfg, flags.HelpFlag|flags.PassDoubleDash)
_, flagerr := preParser.Parse()
if flagerr != nil {
e, ok := flagerr.(*flags.Error)
if !ok || e.Type != flags.ErrHelp {
preParser.WriteHelp(os.Stderr)
}
if ok && e.Type == flags.ErrHelp {
preParser.WriteHelp(os.Stdout)
os.Exit(0)
}
return flagerr
}
return nil
}