-
Notifications
You must be signed in to change notification settings - Fork 2
/
register.go
67 lines (55 loc) · 1.08 KB
/
register.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package goconf
import (
"fmt"
"os"
"github.com/oleiade/reflections"
"github.com/olekukonko/tablewriter"
"github.com/tryfix/log"
)
type Configer interface {
Register() error
}
type Validater interface {
Validate() error
}
type Printer interface {
Print() interface{}
}
func Load(configs ...Configer) error {
for _, c := range configs {
err := c.Register()
if err != nil {
return err
}
v, ok := c.(Validater)
if ok {
err = v.Validate()
if err != nil {
return err
}
}
p, ok := c.(Printer)
if ok {
printTable(p)
}
}
return nil
}
func printTable(p Printer) {
table := tablewriter.NewWriter(os.Stdout)
var data = [][]string{}
pr := p.Print()
var fields []string
fields, _ = reflections.Fields(pr)
for _, field := range fields {
value, err := reflections.GetField(pr, field)
if err != nil {
log.Error("error printing the goconf table", err)
}
data = append(data, []string{field, fmt.Sprint(value)})
}
table.SetHeader([]string{"Config", "Value"})
table.AppendBulk(data)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.Render()
}