http urls monitor.

ini.go 7.1KB

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