-
Notifications
You must be signed in to change notification settings - Fork 4
/
conflag.go
186 lines (151 loc) · 3.48 KB
/
conflag.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package conflag
import (
"bufio"
"flag"
"io"
"os"
"path/filepath"
"strings"
)
// A Conflag represents the state of a conflag.
type Conflag struct {
// embedded the standard FlagSet so we can use all its methods.
*flag.FlagSet
app string
osArgs []string
cfgFile string
args []string
includes []string
// TODO: add shorthand? or just use pflag?
// shorthand map[byte]string
}
// New parses os args and returns a new Conflag instance.
func New(args ...string) *Conflag {
if args == nil {
args = os.Args
}
c := &Conflag{app: args[0], osArgs: args[1:]}
c.FlagSet = flag.NewFlagSet(c.app, flag.ExitOnError)
c.StringVar(&c.cfgFile, "config", "", "config file path")
c.StringSliceUniqVar(&c.includes, "include", nil, "include file")
return c
}
// NewFromFile parses cfgFile and returns a new Conflag instance.
func NewFromFile(app, cfgFile string) *Conflag {
if app == "" {
app = os.Args[0]
}
c := &Conflag{app: app, cfgFile: cfgFile}
c.FlagSet = flag.NewFlagSet(c.app, flag.ExitOnError)
c.StringSliceUniqVar(&c.includes, "include", nil, "include file")
return c
}
// Parse parses config file and flags.
func (c *Conflag) Parse() (err error) {
// parse 1st time and see whether there is a conf file.
err = c.FlagSet.Parse(c.osArgs)
if err != nil {
return err
}
// if there is no args, just try to load the app.conf file.
if c.cfgFile == "" && len(c.osArgs) == 0 {
// trim app exetension
for i := len(c.app) - 1; i >= 0 && c.app[i] != '/' && c.app[i] != '\\'; i-- {
if c.app[i] == '.' {
c.cfgFile = c.app[:i]
break
}
}
if c.cfgFile == "" {
c.cfgFile = c.app
}
c.cfgFile += ".conf"
}
if c.cfgFile == "" {
return nil
}
fargs, err := parseFile(c.cfgFile)
if err != nil {
return err
}
c.args = fargs
c.args = append(c.args, c.osArgs...)
// parse 2nd time to get the include file values
err = c.FlagSet.Parse(c.args)
if err != nil {
return err
}
dir := filepath.Dir(c.cfgFile)
// parse 3rd time to parse flags in include file
for _, include := range c.includes {
include = filepath.Join(dir, include)
args, er := parseFile(include)
if er != nil {
return er
}
c.args = args
c.args = append(c.args, c.osArgs...)
er = c.FlagSet.Parse(c.args)
if er != nil {
return er
}
}
return err
}
// AppDir returns the app dir.
func (c *Conflag) AppDir() string {
return filepath.Dir(os.Args[0])
}
// ConfDir returns the config file dir.
func (c *Conflag) ConfDir() string {
return filepath.Dir(c.cfgFile)
}
func parseFile(cfgFile string) ([]string, error) {
var s []string
f, err := os.Open(cfgFile)
if err != nil {
return nil, err
}
defer f.Close()
scanner := bufio.NewScanner(bomReader(f))
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if len(line) == 0 || line[:1] == "#" {
continue
}
line = parseEnvs(line)
s = append(s, "-"+line)
}
return s, nil
}
func parseEnvs(line string) string {
for {
s := parseEnv(line)
if s == line {
break
}
line = s
}
return line
}
func parseEnv(line string) string {
if i := strings.Index(line, "{$"); i != -1 {
if j := strings.IndexByte(line[i+1:], '}'); j != -1 {
param := line[i+2 : i+j+1]
return line[:i] + os.Getenv(param) + line[i+j+2:]
}
}
return line
}
// bomReader returns a Reader that discards the UTF-8 BOM header.
func bomReader(ir io.Reader) io.Reader {
r := bufio.NewReader(ir)
b, err := r.Peek(3)
if err != nil {
return r
}
if b[0] == 0xef && b[1] == 0xbb && b[2] == 0xbf {
r.Discard(3)
}
return r
}