forked from robfig/config
-
Notifications
You must be signed in to change notification settings - Fork 14
/
context.go
169 lines (146 loc) · 4.33 KB
/
context.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
// Copyright 2016 The "config" Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package config
import (
"fmt"
"os"
"path/filepath"
"strings"
)
// Context structure handles the parsing of app.conf
// It has a "preferred" section that is checked first for option queries.
// If the preferred section does not have the option, the DEFAULT section is
// checked fallback.
type Context struct {
config *Config
section string // Check this section first, then fall back to DEFAULT
}
// NewContext creates a default section and returns config context
func NewContext() *Context {
return &Context{config: NewDefault()}
}
// LoadContext loads the ini config from gives multiple conf paths
func LoadContext(confName string, confPaths []string) (*Context, error) {
ctx := NewContext()
for _, confPath := range confPaths {
path := filepath.Join(confPath, confName)
conf, err := ReadDefault(path)
if err != nil {
if _, isPathErr := err.(*os.PathError); !isPathErr {
return nil, fmt.Errorf("%v: %v", path, err)
}
continue
}
ctx.config.Merge(conf)
}
return ctx, nil
}
// Raw returns raw config instance
func (c *Context) Raw() *Config {
return c.config
}
// SetSection the section scope of ini config
// For e.g.: dev or prod
func (c *Context) SetSection(section string) {
c.section = section
}
// SetOption sets the value for the given key
func (c *Context) SetOption(name, value string) {
c.config.AddOption(c.section, name, value)
}
// Int returns `int` config value and if found returns true
// otherwise false
func (c *Context) Int(option string) (result int, found bool) {
result, err := c.config.Int(c.section, option)
if err == nil {
return result, true
}
if _, ok := err.(OptionError); ok {
return 0, false
}
// If it wasn't an OptionError, it must have failed to parse.
return 0, false
}
// IntDefault returns `int` config value if found otherwise
// returns given default int value
func (c *Context) IntDefault(option string, dfault int) int {
if r, found := c.Int(option); found {
return r
}
return dfault
}
// Bool returns `bool` config value and if found returns true
// otherwise false
func (c *Context) Bool(option string) (result, found bool) {
result, err := c.config.Bool(c.section, option)
if err == nil {
return result, true
}
if _, ok := err.(OptionError); ok {
return false, false
}
// If it wasn't an OptionError, it must have failed to parse.
return false, false
}
// BoolDefault returns `bool` config value if found otherwise
// returns given default bool value
func (c *Context) BoolDefault(option string, dfault bool) bool {
if r, found := c.Bool(option); found {
return r
}
return dfault
}
// String returns `string` config value and if found returns true
// otherwise false
func (c *Context) String(option string) (result string, found bool) {
if r, err := c.config.String(c.section, option); err == nil {
return stripQuotes(r), true
}
return "", false
}
// StringDefault returns `string` config value if found otherwise
// returns given default string value
func (c *Context) StringDefault(option, dfault string) string {
if r, found := c.String(option); found {
return r
}
return dfault
}
// HasSection checks if the configuration has the given section.
// (The default section always exists.)
func (c *Context) HasSection(section string) bool {
return c.config.HasSection(section)
}
// Options returns all configuration option keys.
// If a prefix is provided, then that is applied as a filter.
func (c *Context) Options(prefix string) []string {
var options []string
keys, _ := c.config.Options(c.section)
for _, key := range keys {
if strings.HasPrefix(key, prefix) {
options = append(options, key)
}
}
return options
}
// Helpers
func stripQuotes(s string) string {
if s == "" {
return s
}
if s[0] == '"' && s[len(s)-1] == '"' {
return s[1 : len(s)-1]
}
return s
}