forked from vevsatechnologies/dcrextdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
43 lines (36 loc) · 1.08 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
37
38
39
40
41
42
43
package main
import (
"log"
"os"
flags "github.com/jessevdk/go-flags"
)
type config struct {
DBHost string `long:"dbhost" description:"Database host"`
DBPort int `long:"dbport" description:"Database port"`
DBUser string `long:"dbuser" description:"Database username"`
DBPass string `long:"dbpass" description:"Database password"`
DBName string `long:"dbname" description:"Database name"`
DropTables bool `short:"D" long:"droptables" descripton:"Drop all database tables"`
Quiet bool `short:"q" long:"quiet"`
}
var configfile = "dcrextdata.conf"
func loadConfig() (*config, error) {
var cfg config
parser := flags.NewParser(&cfg, flags.Default)
err := flags.NewIniParser(parser).ParseFile(configfile)
if err != nil {
if _, ok := err.(*os.PathError); ok {
log.Printf("Missing config file %s in current directory", configfile)
} else {
return nil, err
}
}
_, err = parser.Parse()
if err != nil {
if e, ok := err.(*flags.Error); !ok || e.Type != flags.ErrHelp {
parser.WriteHelp(os.Stderr)
}
return nil, err
}
return &cfg, nil
}