/
utils.go
126 lines (108 loc) · 2.45 KB
/
utils.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package bootstrap
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"reflect"
"strconv"
btoml "github.com/hydronica/toml"
ptoml "github.com/pelletier/go-toml"
)
type Utility struct {
name string
description string
version string
config interface{}
}
func NewUtility(name string, config interface{}) *Utility {
return &Utility{
name: name,
config: config,
}
}
func (u *Utility) Initialize() {
setHelpOutput(u.name, u.description)
u.checkFlags()
}
func (u *Utility) Version(version string) *Utility {
u.version = version
return u
}
func (u *Utility) Description(description string) *Utility {
u.description = description
return u
}
func (u *Utility) AddInfo(info func() interface{}, port int) *Utility {
if port == 0 {
log.Println("http status server has been disabled")
return u
}
fn := func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
b, err := json.MarshalIndent(info(), "", " ")
if b != nil && err == nil {
// Replace the first { in the json string with the { + application name
b = bytes.Replace(b, []byte(`{`), []byte(`{
"app_name":"`+u.name+`",`), 1)
}
w.Write(b)
}
log.Printf("starting http status server on port %d", port)
http.HandleFunc("/", fn)
go func() {
err := http.ListenAndServe(":"+strconv.Itoa(port), nil)
log.Fatal("http health service failed", err)
}()
return u
}
func setHelpOutput(name, description string) {
// custom help screen
flag.Usage = func() {
if name != "" {
fmt.Fprintln(os.Stderr, name)
fmt.Fprintln(os.Stderr, "")
}
if description != "" {
fmt.Fprintln(os.Stderr, description)
fmt.Fprintln(os.Stderr, "")
}
fmt.Fprintln(os.Stderr, "Flag options:")
flag.PrintDefaults()
}
}
func (u *Utility) checkFlags() {
if !flag.Parsed() {
flag.Parse()
}
if *showVersion || *ver && u.version != "" {
fmt.Println(u.version)
os.Exit(0)
}
// gen config (sent to stdout)
if *genConfig || *g {
cfg := u.config
if v := reflect.ValueOf(cfg); v.Kind() == reflect.Ptr {
cfg = v.Elem().Interface()
}
b, _ := ptoml.Marshal(cfg)
fmt.Println(string(b))
os.Exit(0)
}
var path string
// configPth required
if *configPth == "" && *c == "" {
log.Fatal("-config (-c) config file path required")
} else if *configPth != "" {
path = *configPth
} else {
path = *c
}
_, err := btoml.DecodeFile(path, u.config)
if err != nil {
log.Fatal("Error parsing config file", err)
}
}