Remove BPA from Makefile
[icn.git] / cmd / bpa-operator / vendor / gopkg.in / ini.v1 / ini.go
1 // +build go1.6
2
3 // Copyright 2014 Unknwon
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License"): you may
6 // not use this file except in compliance with the License. You may obtain
7 // a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 // License for the specific language governing permissions and limitations
15 // under the License.
16
17 // Package ini provides INI file read and write functionality in Go.
18 package ini
19
20 import (
21         "bytes"
22         "fmt"
23         "io"
24         "io/ioutil"
25         "os"
26         "regexp"
27         "runtime"
28 )
29
30 const (
31         // DefaultSection is the name of default section. You can use this constant or the string literal.
32         // In most of cases, an empty string is all you need to access the section.
33         DefaultSection = "DEFAULT"
34         // Deprecated: Use "DefaultSection" instead.
35         DEFAULT_SECTION = DefaultSection
36
37         // Maximum allowed depth when recursively substituing variable names.
38         depthValues = 99
39         version     = "1.46.0"
40 )
41
42 // Version returns current package version literal.
43 func Version() string {
44         return version
45 }
46
47 var (
48         // LineBreak is the delimiter to determine or compose a new line.
49         // This variable will be changed to "\r\n" automatically on Windows at package init time.
50         LineBreak = "\n"
51
52         // DefaultFormatLeft places custom spaces on the left when PrettyFormat and PrettyEqual are both disabled.
53         DefaultFormatLeft = ""
54         // DefaultFormatRight places custom spaces on the right when PrettyFormat and PrettyEqual are both disabled.
55         DefaultFormatRight = ""
56
57         // Variable regexp pattern: %(variable)s
58         varPattern = regexp.MustCompile(`%\(([^\)]+)\)s`)
59
60         // PrettyFormat indicates whether to align "=" sign with spaces to produce pretty output
61         // or reduce all possible spaces for compact format.
62         PrettyFormat = true
63
64         // PrettyEqual places spaces around "=" sign even when PrettyFormat is false.
65         PrettyEqual = false
66
67         // DefaultHeader explicitly writes default section header.
68         DefaultHeader = false
69
70         // PrettySection indicates whether to put a line between sections.
71         PrettySection = true
72 )
73
74 func init() {
75         if runtime.GOOS == "windows" {
76                 LineBreak = "\r\n"
77         }
78 }
79
80 func inSlice(str string, s []string) bool {
81         for _, v := range s {
82                 if str == v {
83                         return true
84                 }
85         }
86         return false
87 }
88
89 // dataSource is an interface that returns object which can be read and closed.
90 type dataSource interface {
91         ReadCloser() (io.ReadCloser, error)
92 }
93
94 // sourceFile represents an object that contains content on the local file system.
95 type sourceFile struct {
96         name string
97 }
98
99 func (s sourceFile) ReadCloser() (_ io.ReadCloser, err error) {
100         return os.Open(s.name)
101 }
102
103 // sourceData represents an object that contains content in memory.
104 type sourceData struct {
105         data []byte
106 }
107
108 func (s *sourceData) ReadCloser() (io.ReadCloser, error) {
109         return ioutil.NopCloser(bytes.NewReader(s.data)), nil
110 }
111
112 // sourceReadCloser represents an input stream with Close method.
113 type sourceReadCloser struct {
114         reader io.ReadCloser
115 }
116
117 func (s *sourceReadCloser) ReadCloser() (io.ReadCloser, error) {
118         return s.reader, nil
119 }
120
121 func parseDataSource(source interface{}) (dataSource, error) {
122         switch s := source.(type) {
123         case string:
124                 return sourceFile{s}, nil
125         case []byte:
126                 return &sourceData{s}, nil
127         case io.ReadCloser:
128                 return &sourceReadCloser{s}, nil
129         default:
130                 return nil, fmt.Errorf("error parsing data source: unknown type '%s'", s)
131         }
132 }
133
134 // LoadOptions contains all customized options used for load data source(s).
135 type LoadOptions struct {
136         // Loose indicates whether the parser should ignore nonexistent files or return error.
137         Loose bool
138         // Insensitive indicates whether the parser forces all section and key names to lowercase.
139         Insensitive bool
140         // IgnoreContinuation indicates whether to ignore continuation lines while parsing.
141         IgnoreContinuation bool
142         // IgnoreInlineComment indicates whether to ignore comments at the end of value and treat it as part of value.
143         IgnoreInlineComment bool
144         // SkipUnrecognizableLines indicates whether to skip unrecognizable lines that do not conform to key/value pairs.
145         SkipUnrecognizableLines bool
146         // AllowBooleanKeys indicates whether to allow boolean type keys or treat as value is missing.
147         // This type of keys are mostly used in my.cnf.
148         AllowBooleanKeys bool
149         // AllowShadows indicates whether to keep track of keys with same name under same section.
150         AllowShadows bool
151         // AllowNestedValues indicates whether to allow AWS-like nested values.
152         // Docs: http://docs.aws.amazon.com/cli/latest/topic/config-vars.html#nested-values
153         AllowNestedValues bool
154         // AllowPythonMultilineValues indicates whether to allow Python-like multi-line values.
155         // Docs: https://docs.python.org/3/library/configparser.html#supported-ini-file-structure
156         // Relevant quote:  Values can also span multiple lines, as long as they are indented deeper
157         // than the first line of the value.
158         AllowPythonMultilineValues bool
159         // SpaceBeforeInlineComment indicates whether to allow comment symbols (\# and \;) inside value.
160         // Docs: https://docs.python.org/2/library/configparser.html
161         // Quote: Comments may appear on their own in an otherwise empty line, or may be entered in lines holding values or section names.
162         // In the latter case, they need to be preceded by a whitespace character to be recognized as a comment.
163         SpaceBeforeInlineComment bool
164         // UnescapeValueDoubleQuotes indicates whether to unescape double quotes inside value to regular format
165         // when value is surrounded by double quotes, e.g. key="a \"value\"" => key=a "value"
166         UnescapeValueDoubleQuotes bool
167         // UnescapeValueCommentSymbols indicates to unescape comment symbols (\# and \;) inside value to regular format
168         // when value is NOT surrounded by any quotes.
169         // Note: UNSTABLE, behavior might change to only unescape inside double quotes but may noy necessary at all.
170         UnescapeValueCommentSymbols bool
171         // UnparseableSections stores a list of blocks that are allowed with raw content which do not otherwise
172         // conform to key/value pairs. Specify the names of those blocks here.
173         UnparseableSections []string
174         // KeyValueDelimiters is the sequence of delimiters that are used to separate key and value. By default, it is "=:".
175         KeyValueDelimiters string
176         // PreserveSurroundedQuote indicates whether to preserve surrounded quote (single and double quotes).
177         PreserveSurroundedQuote bool
178 }
179
180 // LoadSources allows caller to apply customized options for loading from data source(s).
181 func LoadSources(opts LoadOptions, source interface{}, others ...interface{}) (_ *File, err error) {
182         sources := make([]dataSource, len(others)+1)
183         sources[0], err = parseDataSource(source)
184         if err != nil {
185                 return nil, err
186         }
187         for i := range others {
188                 sources[i+1], err = parseDataSource(others[i])
189                 if err != nil {
190                         return nil, err
191                 }
192         }
193         f := newFile(sources, opts)
194         if err = f.Reload(); err != nil {
195                 return nil, err
196         }
197         return f, nil
198 }
199
200 // Load loads and parses from INI data sources.
201 // Arguments can be mixed of file name with string type, or raw data in []byte.
202 // It will return error if list contains nonexistent files.
203 func Load(source interface{}, others ...interface{}) (*File, error) {
204         return LoadSources(LoadOptions{}, source, others...)
205 }
206
207 // LooseLoad has exactly same functionality as Load function
208 // except it ignores nonexistent files instead of returning error.
209 func LooseLoad(source interface{}, others ...interface{}) (*File, error) {
210         return LoadSources(LoadOptions{Loose: true}, source, others...)
211 }
212
213 // InsensitiveLoad has exactly same functionality as Load function
214 // except it forces all section and key names to be lowercased.
215 func InsensitiveLoad(source interface{}, others ...interface{}) (*File, error) {
216         return LoadSources(LoadOptions{Insensitive: true}, source, others...)
217 }
218
219 // ShadowLoad has exactly same functionality as Load function
220 // except it allows have shadow keys.
221 func ShadowLoad(source interface{}, others ...interface{}) (*File, error) {
222         return LoadSources(LoadOptions{AllowShadows: true}, source, others...)
223 }